From: Martin Ling Date: Tue, 2 Sep 2014 19:30:50 +0000 (+0100) Subject: C++: Make StructureWrapper a recurring template, eliminating lots of casts. X-Git-Tag: libsigrok-0.4.0~1048 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=bf52cc8cf278ff73a0ace5b4472a276d76651232 C++: Make StructureWrapper a recurring template, eliminating lots of casts. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 55eba8e4..0aade4b1 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -108,8 +108,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; } @@ -121,8 +120,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; } @@ -134,8 +132,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; } @@ -315,8 +312,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; } @@ -417,15 +413,13 @@ vector> Device::get_channels() { vector> result; for (auto entry : channels) - result.push_back(static_pointer_cast( - entry.second->get_shared_pointer(get_shared_from_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(get_shared_from_this())); + return channels[ptr]->get_shared_pointer(get_shared_from_this()); } map> @@ -436,8 +430,7 @@ Device::get_channel_groups() { auto name = entry.first; auto channel_group = entry.second; - result[name] = static_pointer_cast( - channel_group->get_shared_pointer(get_shared_from_this())); + result[name] = channel_group->get_shared_pointer(get_shared_from_this()); } return result; } @@ -465,13 +458,12 @@ HardwareDevice::~HardwareDevice() shared_ptr HardwareDevice::get_shared_from_this() { - return static_pointer_cast( - static_pointer_cast(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) : @@ -536,8 +528,7 @@ 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; } @@ -565,8 +556,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; } @@ -574,8 +564,7 @@ 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) : @@ -598,8 +587,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; } @@ -1018,9 +1006,7 @@ Header::~Header() shared_ptr Header::get_shared_pointer(Packet *parent) { - return static_pointer_cast( - static_pointer_cast
( - StructureWrapper::get_shared_pointer(parent))); + return static_pointer_cast(get_shared_pointer(parent)); } int Header::get_feed_version() @@ -1047,9 +1033,7 @@ Meta::~Meta() shared_ptr Meta::get_shared_pointer(Packet *parent) { - return static_pointer_cast( - static_pointer_cast( - StructureWrapper::get_shared_pointer(parent))); + return static_pointer_cast(get_shared_pointer(parent)); } map Meta::get_config() @@ -1075,9 +1059,7 @@ Logic::~Logic() shared_ptr Logic::get_shared_pointer(Packet *parent) { - return static_pointer_cast( - static_pointer_cast( - StructureWrapper::get_shared_pointer(parent))); + return static_pointer_cast(get_shared_pointer(parent)); } void *Logic::get_data_pointer() @@ -1107,9 +1089,7 @@ Analog::~Analog() shared_ptr Analog::get_shared_pointer(Packet *parent) { - return static_pointer_cast( - static_pointer_cast( - StructureWrapper::get_shared_pointer(parent))); + return static_pointer_cast(get_shared_pointer(parent)); } float *Analog::get_data_pointer() @@ -1204,8 +1184,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) @@ -1237,8 +1216,7 @@ InputDevice::~InputDevice() shared_ptr InputDevice::get_shared_from_this() { - return static_pointer_cast( - static_pointer_cast(shared_from_this())); + return static_pointer_cast(shared_from_this()); } Option::Option(const struct sr_option *structure, @@ -1315,9 +1293,7 @@ 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()); } diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index ca9c119b..62608582 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -125,7 +125,8 @@ public: }; /* Base template for most classes which wrap a struct type from libsigrok. */ -template class SR_API StructureWrapper +template +class SR_API StructureWrapper { protected: /* Parent object which owns this child object's underlying structure. @@ -144,26 +145,24 @@ protected: shared_ptr parent; /* Weak pointer for shared_from_this() implementation. */ - weak_ptr > weak_this; + weak_ptr weak_this; public: /* Note, this implementation will create a new smart_ptr if none exists. */ - shared_ptr > shared_from_this() + shared_ptr shared_from_this() { - shared_ptr > shared; + shared_ptr shared; if (!(shared = weak_this.lock())) { - shared = shared_ptr >( - this, reset_parent); + shared = shared_ptr((Class *) this, reset_parent); weak_this = shared; } return shared; } - shared_ptr > - get_shared_pointer(shared_ptr parent) + shared_ptr get_shared_pointer(shared_ptr parent) { if (!parent) throw Error(SR_ERR_BUG); @@ -171,16 +170,14 @@ public: return shared_from_this(); } - shared_ptr > - get_shared_pointer(Parent *parent) + shared_ptr get_shared_pointer(Parent *parent) { if (!parent) throw Error(SR_ERR_BUG); - return get_shared_pointer(static_pointer_cast( - parent->shared_from_this())); + return get_shared_pointer(parent->shared_from_this()); } protected: - static void reset_parent(StructureWrapper *object) + static void reset_parent(Class *object) { if (!object->parent) throw Error(SR_ERR_BUG); @@ -189,7 +186,7 @@ protected: Struct *structure; - StructureWrapper(Struct *structure) : + StructureWrapper(Struct *structure) : structure(structure) { } @@ -264,7 +261,8 @@ protected: }; /** A hardware driver provided by the library */ -class SR_API Driver : public StructureWrapper +class SR_API Driver : + public StructureWrapper { public: /** Name of this driver. */ @@ -354,7 +352,7 @@ protected: /** A real hardware device, connected via a driver */ class SR_API HardwareDevice : - public StructureWrapper, + public StructureWrapper, public Device { public: @@ -370,7 +368,8 @@ protected: }; /** A channel on a device */ -class SR_API Channel : public StructureWrapper +class SR_API Channel : + public StructureWrapper { public: /** Current name of this channel. */ @@ -399,7 +398,7 @@ protected: /** A group of channels on a device, which share some configuration */ class SR_API ChannelGroup : - public StructureWrapper, + public StructureWrapper, public Configurable { public: @@ -441,7 +440,8 @@ protected: }; /** A stage in a trigger configuration */ -class SR_API TriggerStage : public StructureWrapper +class SR_API TriggerStage : + public StructureWrapper { public: /** Index number of this stage. */ @@ -465,7 +465,8 @@ protected: }; /** A match condition in a trigger configuration */ -class SR_API TriggerMatch : public StructureWrapper +class SR_API TriggerMatch : + public StructureWrapper { public: /** Channel this condition matches on. */ @@ -681,7 +682,7 @@ protected: /** Payload of a datafeed header packet */ class SR_API Header : - public StructureWrapper, + public StructureWrapper, public PacketPayload { public: @@ -698,7 +699,7 @@ protected: /** Payload of a datafeed metadata packet */ class SR_API Meta : - public StructureWrapper, + public StructureWrapper, public PacketPayload { public: @@ -714,7 +715,7 @@ protected: /** Payload of a datafeed packet with logic data */ class SR_API Logic : - public StructureWrapper, + public StructureWrapper, public PacketPayload { public: @@ -733,7 +734,7 @@ protected: /** Payload of a datafeed packet with analog data */ class SR_API Analog : - public StructureWrapper, + public StructureWrapper, public PacketPayload { public: @@ -758,7 +759,7 @@ protected: /** An input format supported by the library */ class SR_API InputFormat : - public StructureWrapper + public StructureWrapper { public: /** Name of this input format. */ @@ -805,7 +806,7 @@ protected: /** A virtual device associated with an input */ class SR_API InputDevice : - public StructureWrapper, + public StructureWrapper, public Device { protected: @@ -856,7 +857,7 @@ protected: /** An output format supported by the library */ class SR_API OutputFormat : - public StructureWrapper + public StructureWrapper { public: /** Name of this output format. */