From: Martin Ling Date: Fri, 29 Aug 2014 19:06:49 +0000 (+0100) Subject: C++: Fix shared pointer handling for PacketPayload base class. X-Git-Tag: libsigrok-0.4.0~1081 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4cd883a7f36136ba9fbac4ed824dee4c6ce2b044;p=libsigrok.git C++: Fix shared pointer handling for PacketPayload base class. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 403499de..c90cac3f 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -973,6 +973,9 @@ Packet::Packet(shared_ptr device, static_cast( structure->payload)); break; + default: + payload = nullptr; + break; } } @@ -989,7 +992,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() @@ -1001,8 +1007,8 @@ PacketPayload::~PacketPayload() } Header::Header(const struct sr_datafeed_header *structure) : - PacketPayload(), - StructureWrapper(structure) + StructureWrapper(structure), + PacketPayload() { } @@ -1010,6 +1016,14 @@ Header::~Header() { } +shared_ptr Header::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + static_pointer_cast
( + StructureWrapper:: + get_shared_pointer(parent))); +} + int Header::get_feed_version() { return structure->feed_version; @@ -1023,8 +1037,8 @@ Glib::TimeVal Header::get_start_time() } Meta::Meta(const struct sr_datafeed_meta *structure) : - PacketPayload(), - StructureWrapper(structure) + StructureWrapper(structure), + PacketPayload() { } @@ -1032,6 +1046,14 @@ Meta::~Meta() { } +shared_ptr Meta::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + static_pointer_cast( + StructureWrapper:: + get_shared_pointer(parent))); +} + map Meta::get_config() { map result; @@ -1044,8 +1066,8 @@ map Meta::get_config() } Logic::Logic(const struct sr_datafeed_logic *structure) : - PacketPayload(), - StructureWrapper(structure) + StructureWrapper(structure), + PacketPayload() { } @@ -1053,6 +1075,14 @@ Logic::~Logic() { } +shared_ptr Logic::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + static_pointer_cast( + StructureWrapper:: + get_shared_pointer(parent))); +} + void *Logic::get_data_pointer() { return structure->data; @@ -1069,8 +1099,8 @@ unsigned int Logic::get_unit_size() } Analog::Analog(const struct sr_datafeed_analog *structure) : - PacketPayload(), - StructureWrapper(structure) + StructureWrapper(structure), + PacketPayload() { } @@ -1078,6 +1108,14 @@ Analog::~Analog() { } +shared_ptr Analog::get_shared_pointer(Packet *parent) +{ + return static_pointer_cast( + static_pointer_cast( + StructureWrapper:: + get_shared_pointer(parent))); +} + float *Analog::get_data_pointer() { return structure->data; diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 6595802e..e16a9536 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -645,9 +645,7 @@ class SR_API PacketPayload protected: PacketPayload(); virtual ~PacketPayload() = 0; - shared_ptr get_shared_pointer(Packet *parent) { - return static_pointer_cast(get_shared_pointer(parent)); - } + virtual shared_ptr get_shared_pointer(Packet *parent) = 0; /** Deleter needed to allow shared_ptr use with protected destructor. */ class Deleter { @@ -660,8 +658,9 @@ protected: }; /** Payload of a datafeed header packet */ -class SR_API Header : public PacketPayload, - public StructureWrapper +class SR_API Header : + public StructureWrapper, + public PacketPayload { public: /* Feed version number. */ @@ -671,13 +670,14 @@ public: protected: Header(const struct sr_datafeed_header *structure); ~Header(); - const struct sr_datafeed_header *structure; + shared_ptr get_shared_pointer(Packet *parent); friend class Packet; }; /** Payload of a datafeed metadata packet */ -class SR_API Meta : public PacketPayload, - public StructureWrapper +class SR_API Meta : + public StructureWrapper, + public PacketPayload { public: /* Mapping of (ConfigKey, value) pairs. */ @@ -685,14 +685,15 @@ public: protected: Meta(const struct sr_datafeed_meta *structure); ~Meta(); - const struct sr_datafeed_meta *structure; + shared_ptr get_shared_pointer(Packet *parent); map config; friend class Packet; }; /** Payload of a datafeed packet with logic data */ -class SR_API Logic : public PacketPayload, - public StructureWrapper +class SR_API Logic : + public StructureWrapper, + public PacketPayload { public: /* Pointer to data. */ @@ -704,13 +705,14 @@ public: protected: Logic(const struct sr_datafeed_logic *structure); ~Logic(); - const struct sr_datafeed_logic *structure; + shared_ptr get_shared_pointer(Packet *parent); friend class Packet; }; /** Payload of a datafeed packet with analog data */ -class SR_API Analog : public PacketPayload, - public StructureWrapper +class SR_API Analog : + public StructureWrapper, + public PacketPayload { public: /** Pointer to data. */ @@ -728,7 +730,7 @@ public: protected: Analog(const struct sr_datafeed_analog *structure); ~Analog(); - const struct sr_datafeed_analog *structure; + shared_ptr get_shared_pointer(Packet *parent); friend class Packet; };