]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
C++: Whitespace fix.
[libsigrok.git] / bindings / cxx / classes.cpp
index 0aade4b1b2043fccc475a441df5fac33c0a653f3..8ecffd720677cb4abff40610f3d37512adf5c655 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++)
@@ -227,7 +229,7 @@ shared_ptr<Input> Context::open_file(string filename)
 {
        const struct sr_input *input;
 
-       check( sr_input_scan_file(filename.c_str(), &input));
+       check(sr_input_scan_file(filename.c_str(), &input));
        return shared_ptr<Input>(
                new Input(shared_from_this(), input), Input::Deleter());
 }
@@ -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)
@@ -412,8 +465,10 @@ string Device::get_version()
 vector<shared_ptr<Channel>> Device::get_channels()
 {
        vector<shared_ptr<Channel>> result;
-       for (auto entry : channels)
-               result.push_back(entry.second->get_shared_pointer(get_shared_from_this()));
+       for (auto channel = structure->channels; channel; channel = channel->next)
+               result.push_back(
+                       channels[(struct sr_channel *) channel->data]->get_shared_pointer(
+                               get_shared_from_this()));
        return result;
 }
 
@@ -446,7 +501,7 @@ void Device::close()
 }
 
 HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        Device(structure),
        driver(driver)
 {
@@ -467,7 +522,7 @@ shared_ptr<Driver> HardwareDevice::get_driver()
 }
 
 Channel::Channel(struct sr_channel *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        type(ChannelType::get(structure->type))
 {
 }
@@ -508,7 +563,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 +588,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 +624,7 @@ shared_ptr<TriggerStage> Trigger::add_stage()
 }
 
 TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : 
-       StructureWrapper(structure)
+       ParentOwned(structure)
 {
 }
 
@@ -604,7 +660,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)
 {
 }
 
@@ -689,7 +745,23 @@ EventSource::~EventSource()
 {
 }
 
+SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
+       ParentOwned(structure),
+       Device(structure)
+{
+}
+
+SessionDevice::~SessionDevice()
+{
+}
+
+shared_ptr<Device> SessionDevice::get_shared_from_this()
+{
+       return static_pointer_cast<Device>(shared_from_this());
+}
+
 Session::Session(shared_ptr<Context> context) :
+       UserOwned(structure),
        context(context), saving(false)
 {
        check(sr_session_new(&structure));
@@ -697,9 +769,19 @@ 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));
+       GSList *dev_list;
+       check(sr_session_dev_list(structure, &dev_list));
+       for (GSList *dev = dev_list; dev; dev = dev->next)
+       {
+               auto sdi = (struct sr_dev_inst *) dev->data;
+               auto device = new SessionDevice(sdi);
+               devices[sdi] = shared_ptr<SessionDevice>(device,
+                       SessionDevice::Deleter());
+       }
        context->session = this;
 }
 
@@ -936,7 +1018,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 +1077,7 @@ PacketPayload::~PacketPayload()
 }
 
 Header::Header(const struct sr_datafeed_header *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1006,7 +1088,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 +1105,7 @@ Glib::TimeVal Header::get_start_time()
 }
 
 Meta::Meta(const struct sr_datafeed_meta *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1033,7 +1116,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 +1132,7 @@ map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
 }
 
 Logic::Logic(const struct sr_datafeed_logic *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1059,7 +1143,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 +1163,7 @@ unsigned int Logic::get_unit_size()
 }
 
 Analog::Analog(const struct sr_datafeed_analog *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        PacketPayload()
 {
 }
@@ -1089,7 +1174,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 +1213,7 @@ vector<const QuantityFlag *> Analog::get_mq_flags()
 }
 
 InputFormat::InputFormat(const struct sr_input_module *structure) :
-       StructureWrapper(structure)
+       ParentOwned(structure)
 {
 }
 
@@ -1168,7 +1254,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 +1290,7 @@ Input::~Input()
 
 InputDevice::InputDevice(shared_ptr<Input> input,
                struct sr_dev_inst *structure) :
-       StructureWrapper(structure),
+       ParentOwned(structure),
        Device(structure),
        input(input)
 {
@@ -1221,7 +1307,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 +1345,7 @@ vector<Glib::VariantBase> Option::get_values()
 }
 
 OutputFormat::OutputFormat(const struct sr_output_module *structure) :
-       StructureWrapper(structure)
+       ParentOwned(structure)
 {
 }
 
@@ -1299,7 +1385,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)
 {