]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
C++: Move C struct pointers out of ownership classes
[libsigrok.git] / bindings / cxx / classes.cpp
index 960ac39bc0524a5f0ae7f59604f1fa24eeefcee6..7f5b3b2407e164d3f985bf7ef26578d5964ea162 100644 (file)
@@ -120,7 +120,7 @@ shared_ptr<Context> Context::create()
 }
 
 Context::Context() :
-       UserOwned(_structure),
+       _structure(nullptr),
        _session(nullptr)
 {
        check(sr_init(&_structure));
@@ -159,7 +159,7 @@ map<string, shared_ptr<Driver>> Context::drivers()
        {
                auto name = entry.first;
                auto driver = entry.second;
-               result[name] = driver->get_shared_pointer(this);
+               result[name] = driver->share_owned_by(shared_from_this());
        }
        return result;
 }
@@ -171,7 +171,7 @@ map<string, shared_ptr<InputFormat>> Context::input_formats()
        {
                auto name = entry.first;
                auto input_format = entry.second;
-               result[name] = input_format->get_shared_pointer(this);
+               result[name] = input_format->share_owned_by(shared_from_this());
        }
        return result;
 }
@@ -183,7 +183,7 @@ map<string, shared_ptr<OutputFormat>> Context::output_formats()
        {
                auto name = entry.first;
                auto output_format = entry.second;
-               result[name] = output_format->get_shared_pointer(this);
+               result[name] = output_format->share_owned_by(shared_from_this());
        }
        return result;
 }
@@ -384,8 +384,8 @@ map<string, string> Context::serials(shared_ptr<Driver> driver) const
 }
 
 Driver::Driver(struct sr_dev_driver *structure) :
-       ParentOwned(structure),
        Configurable(structure, nullptr, nullptr),
+       _structure(structure),
        _initialized(false)
 {
 }
@@ -603,14 +603,14 @@ vector<shared_ptr<Channel>> Device::channels()
        vector<shared_ptr<Channel>> result;
        for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) {
                auto *const ch = static_cast<struct sr_channel *>(channel->data);
-               result.push_back(_channels[ch]->get_shared_pointer(get_shared_from_this()));
+               result.push_back(_channels[ch]->share_owned_by(get_shared_from_this()));
        }
        return result;
 }
 
 shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
 {
-       return _channels[ptr]->get_shared_pointer(get_shared_from_this());
+       return _channels[ptr]->share_owned_by(get_shared_from_this());
 }
 
 map<string, shared_ptr<ChannelGroup>>
@@ -621,7 +621,7 @@ Device::channel_groups()
        {
                auto name = entry.first;
                auto channel_group = entry.second;
-               result[name] = channel_group->get_shared_pointer(get_shared_from_this());
+               result[name] = channel_group->share_owned_by(get_shared_from_this());
        }
        return result;
 }
@@ -638,7 +638,6 @@ void Device::close()
 
 HardwareDevice::HardwareDevice(shared_ptr<Driver> driver,
                struct sr_dev_inst *structure) :
-       UserOwned(structure),
        Device(structure),
        _driver(move(driver))
 {
@@ -659,9 +658,8 @@ shared_ptr<Driver> HardwareDevice::driver()
 }
 
 UserDevice::UserDevice(string vendor, string model, string version) :
-       UserOwned(sr_dev_inst_user_new(
-               vendor.c_str(), model.c_str(), version.c_str())),
-       Device(UserOwned::_structure)
+       Device(sr_dev_inst_user_new(
+               vendor.c_str(), model.c_str(), version.c_str()))
 {
 }
 
@@ -686,7 +684,7 @@ shared_ptr<Channel> UserDevice::add_channel(unsigned int index,
 }
 
 Channel::Channel(struct sr_channel *structure) :
-       ParentOwned(structure),
+       _structure(structure),
        _type(ChannelType::get(_structure->type))
 {
 }
@@ -727,10 +725,9 @@ unsigned int Channel::index() const
 
 ChannelGroup::ChannelGroup(Device *device,
                struct sr_channel_group *structure) :
-       ParentOwned(structure),
        Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure)
 {
-       for (GSList *entry = structure->channels; entry; entry = entry->next) {
+       for (GSList *entry = config_channel_group->channels; entry; entry = entry->next) {
                auto *const ch = static_cast<struct sr_channel *>(entry->data);
                _channels.push_back(device->_channels[ch]);
        }
@@ -742,19 +739,19 @@ ChannelGroup::~ChannelGroup()
 
 string ChannelGroup::name() const
 {
-       return valid_string(_structure->name);
+       return valid_string(config_channel_group->name);
 }
 
 vector<shared_ptr<Channel>> ChannelGroup::channels()
 {
        vector<shared_ptr<Channel>> result;
        for (auto channel : _channels)
-               result.push_back(channel->get_shared_pointer(_parent));
+               result.push_back(channel->share_owned_by(_parent));
        return result;
 }
 
 Trigger::Trigger(shared_ptr<Context> context, string name) : 
-       UserOwned(sr_trigger_new(name.c_str())),
+       _structure(sr_trigger_new(name.c_str())),
        _context(move(context))
 {
        for (auto stage = _structure->stages; stage; stage = stage->next)
@@ -779,7 +776,7 @@ vector<shared_ptr<TriggerStage>> Trigger::stages()
 {
        vector<shared_ptr<TriggerStage>> result;
        for (auto stage : _stages)
-               result.push_back(stage->get_shared_pointer(this));
+               result.push_back(stage->share_owned_by(shared_from_this()));
        return result;
 }
 
@@ -787,11 +784,11 @@ shared_ptr<TriggerStage> Trigger::add_stage()
 {
        auto stage = new TriggerStage(sr_trigger_stage_add(_structure));
        _stages.push_back(stage);
-       return stage->get_shared_pointer(this);
+       return stage->share_owned_by(shared_from_this());
 }
 
-TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : 
-       ParentOwned(structure)
+TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
+       _structure(structure)
 {
 }
 
@@ -810,7 +807,7 @@ vector<shared_ptr<TriggerMatch>> TriggerStage::matches()
 {
        vector<shared_ptr<TriggerMatch>> result;
        for (auto match : _matches)
-               result.push_back(match->get_shared_pointer(this));
+               result.push_back(match->share_owned_by(shared_from_this()));
        return result;
 }
 
@@ -833,7 +830,7 @@ void TriggerStage::add_match(shared_ptr<Channel> channel,
 
 TriggerMatch::TriggerMatch(struct sr_trigger_match *structure,
                shared_ptr<Channel> channel) :
-       ParentOwned(structure),
+       _structure(structure),
        _channel(move(channel))
 {
 }
@@ -873,7 +870,6 @@ void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
 }
 
 SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
-       ParentOwned(structure),
        Device(structure)
 {
 }
@@ -888,7 +884,7 @@ shared_ptr<Device> SessionDevice::get_shared_from_this()
 }
 
 Session::Session(shared_ptr<Context> context) :
-       UserOwned(_structure),
+       _structure(nullptr),
        _context(move(context))
 {
        check(sr_session_new(_context->_structure, &_structure));
@@ -896,7 +892,7 @@ Session::Session(shared_ptr<Context> context) :
 }
 
 Session::Session(shared_ptr<Context> context, string filename) :
-       UserOwned(_structure),
+       _structure(nullptr),
        _context(move(context)),
        _filename(move(filename))
 {
@@ -925,7 +921,7 @@ shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
 {
        if (_owned_devices.count(sdi))
                return static_pointer_cast<Device>(
-                       _owned_devices[sdi]->get_shared_pointer(this));
+                       _owned_devices[sdi]->share_owned_by(shared_from_this()));
        else if (_other_devices.count(sdi))
                return _other_devices[sdi];
        else
@@ -1047,7 +1043,7 @@ shared_ptr<Context> Session::context()
 
 Packet::Packet(shared_ptr<Device> device,
        const struct sr_datafeed_packet *structure) :
-       UserOwned(structure),
+       _structure(structure),
        _device(move(device))
 {
        switch (structure->type)
@@ -1092,7 +1088,7 @@ const PacketType *Packet::type() const
 shared_ptr<PacketPayload> Packet::payload()
 {
        if (_payload)
-               return _payload->get_shared_pointer(this);
+               return _payload->share_owned_by(shared_from_this());
        else
                throw Error(SR_ERR_NA);
 }
@@ -1106,8 +1102,8 @@ PacketPayload::~PacketPayload()
 }
 
 Header::Header(const struct sr_datafeed_header *structure) :
-       ParentOwned(structure),
-       PacketPayload()
+       PacketPayload(),
+       _structure(structure)
 {
 }
 
@@ -1115,10 +1111,10 @@ Header::~Header()
 {
 }
 
-shared_ptr<PacketPayload> Header::get_shared_pointer(Packet *_parent)
+shared_ptr<PacketPayload> Header::share_owned_by(shared_ptr<Packet> _parent)
 {
        return static_pointer_cast<PacketPayload>(
-               ParentOwned::get_shared_pointer(_parent));
+               ParentOwned::share_owned_by(_parent));
 }
 
 int Header::feed_version() const
@@ -1134,8 +1130,8 @@ Glib::TimeVal Header::start_time() const
 }
 
 Meta::Meta(const struct sr_datafeed_meta *structure) :
-       ParentOwned(structure),
-       PacketPayload()
+       PacketPayload(),
+       _structure(structure)
 {
 }
 
@@ -1143,10 +1139,10 @@ Meta::~Meta()
 {
 }
 
-shared_ptr<PacketPayload> Meta::get_shared_pointer(Packet *_parent)
+shared_ptr<PacketPayload> Meta::share_owned_by(shared_ptr<Packet> _parent)
 {
        return static_pointer_cast<PacketPayload>(
-               ParentOwned::get_shared_pointer(_parent));
+               ParentOwned::share_owned_by(_parent));
 }
 
 map<const ConfigKey *, Glib::VariantBase> Meta::config() const
@@ -1160,8 +1156,8 @@ map<const ConfigKey *, Glib::VariantBase> Meta::config() const
 }
 
 Logic::Logic(const struct sr_datafeed_logic *structure) :
-       ParentOwned(structure),
-       PacketPayload()
+       PacketPayload(),
+       _structure(structure)
 {
 }
 
@@ -1169,10 +1165,10 @@ Logic::~Logic()
 {
 }
 
-shared_ptr<PacketPayload> Logic::get_shared_pointer(Packet *_parent)
+shared_ptr<PacketPayload> Logic::share_owned_by(shared_ptr<Packet> _parent)
 {
        return static_pointer_cast<PacketPayload>(
-               ParentOwned::get_shared_pointer(_parent));
+               ParentOwned::share_owned_by(_parent));
 }
 
 void *Logic::data_pointer()
@@ -1191,8 +1187,8 @@ unsigned int Logic::unit_size() const
 }
 
 Analog::Analog(const struct sr_datafeed_analog *structure) :
-       ParentOwned(structure),
-       PacketPayload()
+       PacketPayload(),
+       _structure(structure)
 {
 }
 
@@ -1200,10 +1196,10 @@ Analog::~Analog()
 {
 }
 
-shared_ptr<PacketPayload> Analog::get_shared_pointer(Packet *_parent)
+shared_ptr<PacketPayload> Analog::share_owned_by(shared_ptr<Packet> _parent)
 {
        return static_pointer_cast<PacketPayload>(
-               ParentOwned::get_shared_pointer(_parent));
+               ParentOwned::share_owned_by(_parent));
 }
 
 void *Analog::data_pointer()
@@ -1242,7 +1238,7 @@ vector<const QuantityFlag *> Analog::mq_flags() const
 }
 
 InputFormat::InputFormat(const struct sr_input_module *structure) :
-       ParentOwned(structure)
+       _structure(structure)
 {
 }
 
@@ -1290,12 +1286,11 @@ shared_ptr<Input> InputFormat::create_input(
        auto input = sr_input_new(_structure, map_to_hash_variant(options));
        if (!input)
                throw Error(SR_ERR_ARG);
-       return shared_ptr<Input>(
-               new Input(_parent->shared_from_this(), input), Input::Deleter());
+       return shared_ptr<Input>(new Input(_parent, input), Input::Deleter());
 }
 
 Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
-       UserOwned(structure),
+       _structure(structure),
        _context(move(context)),
        _device(nullptr)
 {
@@ -1311,7 +1306,7 @@ shared_ptr<InputDevice> Input::device()
                _device = new InputDevice(shared_from_this(), sdi);
        }
 
-       return _device->get_shared_pointer(shared_from_this());
+       return _device->share_owned_by(shared_from_this());
 }
 
 void Input::send(void *data, size_t length)
@@ -1336,7 +1331,6 @@ Input::~Input()
 
 InputDevice::InputDevice(shared_ptr<Input> input,
                struct sr_dev_inst *structure) :
-       ParentOwned(structure),
        Device(structure),
        _input(move(input))
 {
@@ -1353,7 +1347,7 @@ shared_ptr<Device> InputDevice::get_shared_from_this()
 
 Option::Option(const struct sr_option *structure,
                shared_ptr<const struct sr_option *> structure_array) :
-       UserOwned(structure),
+       _structure(structure),
        _structure_array(move(structure_array))
 {
 }
@@ -1393,7 +1387,7 @@ vector<Glib::VariantBase> Option::values() const
 }
 
 OutputFormat::OutputFormat(const struct sr_output_module *structure) :
-       ParentOwned(structure)
+       _structure(structure)
 {
 }
 
@@ -1458,7 +1452,7 @@ bool OutputFormat::test_flag(const OutputFlag *flag) const
 
 Output::Output(shared_ptr<OutputFormat> format,
                shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) :
-       UserOwned(sr_output_new(format->_structure,
+       _structure(sr_output_new(format->_structure,
                map_to_hash_variant(options), device->_structure, nullptr)),
        _format(move(format)),
        _device(move(device)),
@@ -1468,7 +1462,7 @@ Output::Output(shared_ptr<OutputFormat> format,
 
 Output::Output(string filename, shared_ptr<OutputFormat> format,
                shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) :
-       UserOwned(sr_output_new(format->_structure,
+       _structure(sr_output_new(format->_structure,
                map_to_hash_variant(options), device->_structure, filename.c_str())),
        _format(move(format)),
        _device(move(device)),