X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Fclasses.cpp;h=c515a303104d233986f13aebf812ac28acf265db;hb=4f7bcf0ec35c18f10da51530767efcff62ddc88f;hp=2b26f1a88d90f6aa20c614216da9be293d020d6b;hpb=f88c73732cbef5dad58788d1555bd66742001192;p=libsigrok.git diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 2b26f1a8..c515a303 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -44,8 +44,7 @@ static const char *valid_string(const char *input) static GHashTable *map_to_hash_variant(map input) { auto output = g_hash_table_new_full( - g_variant_hash, g_variant_equal, g_free, - (void (*)(void *))g_variant_unref); + g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref); for (auto entry : input) g_hash_table_insert(output, g_strdup(entry.first.c_str()), @@ -72,9 +71,11 @@ shared_ptr 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++) @@ -109,8 +110,7 @@ map> Context::get_drivers() { auto name = entry.first; auto driver = entry.second; - result[name] = static_pointer_cast( - driver->get_shared_pointer(this)); + result[name] = driver->get_shared_pointer(this); } return result; } @@ -122,8 +122,7 @@ map> Context::get_input_formats() { auto name = entry.first; auto input_format = entry.second; - result[name] = static_pointer_cast( - input_format->get_shared_pointer(this)); + result[name] = input_format->get_shared_pointer(this); } return result; } @@ -135,8 +134,7 @@ map> Context::get_output_formats() { auto name = entry.first; auto output_format = entry.second; - result[name] = static_pointer_cast( - output_format->get_shared_pointer(this)); + result[name] = output_format->get_shared_pointer(this); } return result; } @@ -249,7 +247,7 @@ shared_ptr Context::open_stream(string header) } Driver::Driver(struct sr_dev_driver *structure) : - StructureWrapper(structure), + ParentOwned(structure), initialized(false) { } @@ -316,8 +314,7 @@ vector> Driver::scan( /* Create list of shared pointers to device instances for return. */ vector> result; for (auto device : devices) - result.push_back(static_pointer_cast( - device->get_shared_pointer(parent))); + result.push_back(device->get_shared_pointer(parent)); return result; } @@ -418,15 +415,13 @@ vector> Device::get_channels() { vector> result; for (auto entry : channels) - result.push_back(static_pointer_cast( - entry.second->get_shared_pointer(this))); + result.push_back(entry.second->get_shared_pointer(get_shared_from_this())); return result; } shared_ptr Device::get_channel(struct sr_channel *ptr) { - return static_pointer_cast( - channels[ptr]->get_shared_pointer(this)); + return channels[ptr]->get_shared_pointer(get_shared_from_this()); } map> @@ -437,8 +432,7 @@ Device::get_channel_groups() { auto name = entry.first; auto channel_group = entry.second; - result[name] = static_pointer_cast( - channel_group->get_shared_pointer(this)); + result[name] = channel_group->get_shared_pointer(get_shared_from_this()); } return result; } @@ -454,7 +448,7 @@ void Device::close() } HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) : - StructureWrapper(structure), + ParentOwned(structure), Device(structure), driver(driver) { @@ -464,13 +458,18 @@ HardwareDevice::~HardwareDevice() { } +shared_ptr HardwareDevice::get_shared_from_this() +{ + return static_pointer_cast(shared_from_this()); +} + shared_ptr HardwareDevice::get_driver() { - return static_pointer_cast(driver->get_shared_pointer(parent)); + return driver->get_shared_pointer(parent); } Channel::Channel(struct sr_channel *structure) : - StructureWrapper(structure), + ParentOwned(structure), type(ChannelType::get(structure->type)) { } @@ -511,7 +510,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) @@ -531,13 +530,13 @@ vector> ChannelGroup::get_channels() { vector> result; for (auto channel : channels) - result.push_back(static_pointer_cast( - channel->get_shared_pointer(parent))); + result.push_back(channel->get_shared_pointer(parent)); return result; } Trigger::Trigger(shared_ptr 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)); @@ -560,8 +559,7 @@ vector> Trigger::get_stages() { vector> result; for (auto stage : stages) - result.push_back(static_pointer_cast( - stage->get_shared_pointer(this))); + result.push_back(stage->get_shared_pointer(this)); return result; } @@ -569,12 +567,11 @@ shared_ptr Trigger::add_stage() { auto stage = new TriggerStage(sr_trigger_stage_add(structure)); stages.push_back(stage); - return static_pointer_cast( - stage->get_shared_pointer(this)); + return stage->get_shared_pointer(this); } TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : - StructureWrapper(structure) + ParentOwned(structure) { } @@ -593,8 +590,7 @@ vector> TriggerStage::get_matches() { vector> result; for (auto match : matches) - result.push_back(static_pointer_cast( - match->get_shared_pointer(this))); + result.push_back(match->get_shared_pointer(this)); return result; } @@ -611,7 +607,7 @@ void TriggerStage::add_match(shared_ptr channel, const TriggerMatchType } TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, shared_ptr channel) : - StructureWrapper(structure), channel(channel) + ParentOwned(structure), channel(channel) { } @@ -697,6 +693,7 @@ EventSource::~EventSource() } Session::Session(shared_ptr context) : + UserOwned(structure), context(context), saving(false) { check(sr_session_new(&structure)); @@ -704,6 +701,7 @@ Session::Session(shared_ptr context) : } Session::Session(shared_ptr context, string filename) : + UserOwned(structure), context(context), saving(false) { check(sr_session_load(filename.c_str(), &structure)); @@ -735,9 +733,6 @@ vector> Session::get_devices() for (GSList *dev = dev_list; dev; dev = dev->next) { auto sdi = (struct sr_dev_inst *) dev->data; - if (devices.count(sdi) == 0) - devices[sdi] = shared_ptr( - new Device(sdi), Device::Deleter()); result.push_back(devices[sdi]); } return result; @@ -946,7 +941,7 @@ void Session::set_trigger(shared_ptr trigger) Packet::Packet(shared_ptr device, const struct sr_datafeed_packet *structure) : - structure(structure), + UserOwned(structure), device(device) { switch (structure->type) @@ -971,6 +966,9 @@ Packet::Packet(shared_ptr device, static_cast( structure->payload)); break; + default: + payload = nullptr; + break; } } @@ -987,7 +985,10 @@ const PacketType *Packet::get_type() shared_ptr Packet::get_payload() { - return payload->get_shared_pointer(this); + if (payload) + return payload->get_shared_pointer(this); + else + throw Error(SR_ERR_NA); } PacketPayload::PacketPayload() @@ -999,8 +1000,8 @@ PacketPayload::~PacketPayload() } Header::Header(const struct sr_datafeed_header *structure) : - PacketPayload(), - StructureWrapper(structure) + ParentOwned(structure), + PacketPayload() { } @@ -1008,6 +1009,12 @@ Header::~Header() { } +shared_ptr Header::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + ParentOwned::get_shared_pointer(parent)); +} + int Header::get_feed_version() { return structure->feed_version; @@ -1021,8 +1028,8 @@ Glib::TimeVal Header::get_start_time() } Meta::Meta(const struct sr_datafeed_meta *structure) : - PacketPayload(), - StructureWrapper(structure) + ParentOwned(structure), + PacketPayload() { } @@ -1030,6 +1037,12 @@ Meta::~Meta() { } +shared_ptr Meta::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + ParentOwned::get_shared_pointer(parent)); +} + map Meta::get_config() { map result; @@ -1042,8 +1055,8 @@ map Meta::get_config() } Logic::Logic(const struct sr_datafeed_logic *structure) : - PacketPayload(), - StructureWrapper(structure) + ParentOwned(structure), + PacketPayload() { } @@ -1051,6 +1064,12 @@ Logic::~Logic() { } +shared_ptr Logic::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + ParentOwned::get_shared_pointer(parent)); +} + void *Logic::get_data_pointer() { return structure->data; @@ -1067,8 +1086,8 @@ unsigned int Logic::get_unit_size() } Analog::Analog(const struct sr_datafeed_analog *structure) : - PacketPayload(), - StructureWrapper(structure) + ParentOwned(structure), + PacketPayload() { } @@ -1076,6 +1095,12 @@ Analog::~Analog() { } +shared_ptr Analog::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + ParentOwned::get_shared_pointer(parent)); +} + float *Analog::get_data_pointer() { return structure->data; @@ -1111,7 +1136,7 @@ vector Analog::get_mq_flags() } InputFormat::InputFormat(const struct sr_input_module *structure) : - StructureWrapper(structure) + ParentOwned(structure) { } @@ -1152,7 +1177,7 @@ shared_ptr InputFormat::create_input( } Input::Input(shared_ptr context, const struct sr_input *structure) : - structure(structure), + UserOwned(structure), context(context), device(nullptr) { @@ -1168,8 +1193,7 @@ shared_ptr Input::get_device() device = new InputDevice(shared_from_this(), sdi); } - return static_pointer_cast( - device->get_shared_pointer(shared_from_this())); + return device->get_shared_pointer(shared_from_this()); } void Input::send(string data) @@ -1189,7 +1213,7 @@ Input::~Input() InputDevice::InputDevice(shared_ptr input, struct sr_dev_inst *structure) : - StructureWrapper(structure), + ParentOwned(structure), Device(structure), input(input) { @@ -1199,9 +1223,14 @@ InputDevice::~InputDevice() { } +shared_ptr InputDevice::get_shared_from_this() +{ + return static_pointer_cast(shared_from_this()); +} + Option::Option(const struct sr_option *structure, shared_ptr structure_array) : - structure(structure), + UserOwned(structure), structure_array(structure_array) { } @@ -1239,7 +1268,7 @@ vector Option::get_values() } OutputFormat::OutputFormat(const struct sr_output_module *structure) : - StructureWrapper(structure) + ParentOwned(structure) { } @@ -1273,15 +1302,13 @@ shared_ptr OutputFormat::create_output( shared_ptr device, map options) { return shared_ptr( - new Output( - static_pointer_cast(shared_from_this()), - device, options), + new Output(shared_from_this(), device, options), Output::Deleter()); } Output::Output(shared_ptr format, shared_ptr device, map 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) {