]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
C++: Add Configurable::config_check() method.
[libsigrok.git] / bindings / cxx / classes.cpp
index 0aade4b1b2043fccc475a441df5fac33c0a653f3..dc8eded7b4aac6e010702cf5e49a41d92947e2cb 100644 (file)
@@ -71,9 +71,11 @@ shared_ptr<Context> Context::create()
 }
 
 Context::Context() :
+       UserOwned(structure),
        session(NULL)
 {
        check(sr_init(&structure));
+
        struct sr_dev_driver **driver_list = sr_driver_list();
        if (driver_list)
                for (int i = 0; driver_list[i]; i++)
@@ -245,7 +247,8 @@ shared_ptr<Input> Context::open_stream(string header)
 }
 
 Driver::Driver(struct sr_dev_driver *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
+       Configurable(structure, NULL, NULL),
        initialized(false)
 {
 }
@@ -355,6 +358,56 @@ Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
        return Glib::VariantContainerBase(data);
 }
 
+vector<const ConfigKey *> Configurable::config_keys(const ConfigKey *key)
+{
+       GVariant *gvar_opts;
+       gsize num_opts;
+       const int32_t *opts;
+       vector<const ConfigKey *> result;
+
+       check(sr_config_list(
+               config_driver, config_sdi, config_channel_group,
+               key->get_id(), &gvar_opts));
+
+       opts = (const int32_t *) g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(int32_t));
+
+       for (gsize i = 0; i < num_opts; i++)
+               result.push_back(ConfigKey::get(opts[i]));
+
+       g_variant_unref(gvar_opts);
+
+       return result;
+}
+
+bool Configurable::config_check(const ConfigKey *key,
+       const ConfigKey *index_key)
+{
+       GVariant *gvar_opts;
+       gsize num_opts;
+       const int32_t *opts;
+
+       if (sr_config_list(config_driver, config_sdi, config_channel_group,
+                       index_key->get_id(), &gvar_opts) != SR_OK)
+               return false;
+
+       opts = (const int32_t *) g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(int32_t));
+
+       for (gsize i = 0; i < num_opts; i++)
+       {
+               if (opts[i] == key->get_id())
+               {
+                       g_variant_unref(gvar_opts);
+                       return true;
+               }
+       }
+
+       g_variant_unref(gvar_opts);
+
+       return false;
+}
+
 Device::Device(struct sr_dev_inst *structure) :
        Configurable(structure->driver, structure, NULL),
        structure(structure)
@@ -446,7 +499,7 @@ void Device::close()
 }
 
 HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        Device(structure),
        driver(driver)
 {
@@ -467,7 +520,7 @@ shared_ptr<Driver> HardwareDevice::get_driver()
 }
 
 Channel::Channel(struct sr_channel *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        type(ChannelType::get(structure->type))
 {
 }
@@ -508,7 +561,7 @@ unsigned int Channel::get_index()
 
 ChannelGroup::ChannelGroup(Device *device,
                struct sr_channel_group *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        Configurable(device->structure->driver, device->structure, structure)
 {
        for (GSList *entry = structure->channels; entry; entry = entry->next)
@@ -533,7 +586,8 @@ vector<shared_ptr<Channel>> ChannelGroup::get_channels()
 }
 
 Trigger::Trigger(shared_ptr<Context> context, string name) : 
-       structure(sr_trigger_new(name.c_str())), context(context)
+       UserOwned(sr_trigger_new(name.c_str())),
+       context(context)
 {
        for (auto stage = structure->stages; stage; stage = stage->next)
                stages.push_back(new TriggerStage((struct sr_trigger_stage *) stage->data));
@@ -568,7 +622,7 @@ shared_ptr<TriggerStage> Trigger::add_stage()
 }
 
 TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : 
-       StructureWrapper(structure)
+       ParentOwned(structure)
 {
 }
 
@@ -604,7 +658,7 @@ void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType
 }
 
 TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel) :
-       StructureWrapper(structure), channel(channel)
+       ParentOwned(structure), channel(channel)
 {
 }
 
@@ -690,6 +744,7 @@ EventSource::~EventSource()
 }
 
 Session::Session(shared_ptr<Context> context) :
+       UserOwned(structure),
        context(context), saving(false)
 {
        check(sr_session_new(&structure));
@@ -697,6 +752,7 @@ Session::Session(shared_ptr<Context> context) :
 }
 
 Session::Session(shared_ptr<Context> context, string filename) :
+       UserOwned(structure),
        context(context), saving(false)
 {
        check(sr_session_load(filename.c_str(), &structure));
@@ -936,7 +992,7 @@ void Session::set_trigger(shared_ptr<Trigger> trigger)
 
 Packet::Packet(shared_ptr<Device> device,
        const struct sr_datafeed_packet *structure) :
-       structure(structure),
+       UserOwned(structure),
        device(device)
 {
        switch (structure->type)
@@ -995,7 +1051,7 @@ PacketPayload::~PacketPayload()
 }
 
 Header::Header(const struct sr_datafeed_header *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1006,7 +1062,8 @@ Header::~Header()
 
 shared_ptr<PacketPayload> Header::get_shared_pointer(Packet *parent)
 {
-       return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
+       return static_pointer_cast<PacketPayload>(
+               ParentOwned::get_shared_pointer(parent));
 }
 
 int Header::get_feed_version()
@@ -1022,7 +1079,7 @@ Glib::TimeVal Header::get_start_time()
 }
 
 Meta::Meta(const struct sr_datafeed_meta *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1033,7 +1090,8 @@ Meta::~Meta()
 
 shared_ptr<PacketPayload> Meta::get_shared_pointer(Packet *parent)
 {
-       return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
+       return static_pointer_cast<PacketPayload>(
+               ParentOwned::get_shared_pointer(parent));
 }
 
 map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
@@ -1048,7 +1106,7 @@ map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
 }
 
 Logic::Logic(const struct sr_datafeed_logic *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1059,7 +1117,8 @@ Logic::~Logic()
 
 shared_ptr<PacketPayload> Logic::get_shared_pointer(Packet *parent)
 {
-       return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
+       return static_pointer_cast<PacketPayload>(
+               ParentOwned::get_shared_pointer(parent));
 }
 
 void *Logic::get_data_pointer()
@@ -1078,7 +1137,7 @@ unsigned int Logic::get_unit_size()
 }
 
 Analog::Analog(const struct sr_datafeed_analog *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1089,7 +1148,8 @@ Analog::~Analog()
 
 shared_ptr<PacketPayload> Analog::get_shared_pointer(Packet *parent)
 {
-       return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
+       return static_pointer_cast<PacketPayload>(
+               ParentOwned::get_shared_pointer(parent));
 }
 
 float *Analog::get_data_pointer()
@@ -1127,7 +1187,7 @@ vector<const QuantityFlag *> Analog::get_mq_flags()
 }
 
 InputFormat::InputFormat(const struct sr_input_module *structure) :
-       StructureWrapper(structure)
+       ParentOwned(structure)
 {
 }
 
@@ -1168,7 +1228,7 @@ shared_ptr<Input> InputFormat::create_input(
 }
 
 Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
-       structure(structure),
+       UserOwned(structure),
        context(context),
        device(nullptr)
 {
@@ -1204,7 +1264,7 @@ Input::~Input()
 
 InputDevice::InputDevice(shared_ptr<Input> input,
                struct sr_dev_inst *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        Device(structure),
        input(input)
 {
@@ -1221,7 +1281,7 @@ shared_ptr<Device> InputDevice::get_shared_from_this()
 
 Option::Option(const struct sr_option *structure,
                shared_ptr<const struct sr_option *> structure_array) :
-       structure(structure),
+       UserOwned(structure),
        structure_array(structure_array)
 {
 }
@@ -1259,7 +1319,7 @@ vector<Glib::VariantBase> Option::get_values()
 }
 
 OutputFormat::OutputFormat(const struct sr_output_module *structure) :
-       StructureWrapper(structure)
+       ParentOwned(structure)
 {
 }
 
@@ -1299,7 +1359,7 @@ shared_ptr<Output> OutputFormat::create_output(
 
 Output::Output(shared_ptr<OutputFormat> format,
                shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
-       structure(sr_output_new(format->structure,
+       UserOwned(sr_output_new(format->structure,
                map_to_hash_variant(options), device->structure)),
        format(format), device(device), options(options)
 {