C++: Fix shared pointer handling for PacketPayload base class.

This commit is contained in:
Martin Ling 2014-08-29 20:06:49 +01:00
parent b31581f8a8
commit 4cd883a7f3
2 changed files with 64 additions and 24 deletions

View File

@ -973,6 +973,9 @@ Packet::Packet(shared_ptr<Device> device,
static_cast<const struct sr_datafeed_analog *>( static_cast<const struct sr_datafeed_analog *>(
structure->payload)); structure->payload));
break; break;
default:
payload = nullptr;
break;
} }
} }
@ -989,7 +992,10 @@ const PacketType *Packet::get_type()
shared_ptr<PacketPayload> Packet::get_payload() shared_ptr<PacketPayload> 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() PacketPayload::PacketPayload()
@ -1001,8 +1007,8 @@ PacketPayload::~PacketPayload()
} }
Header::Header(const struct sr_datafeed_header *structure) : Header::Header(const struct sr_datafeed_header *structure) :
PacketPayload(), StructureWrapper<Packet, const struct sr_datafeed_header>(structure),
StructureWrapper<Packet, const struct sr_datafeed_header>(structure) PacketPayload()
{ {
} }
@ -1010,6 +1016,14 @@ Header::~Header()
{ {
} }
shared_ptr<PacketPayload> Header::get_shared_pointer(Packet *parent)
{
return static_pointer_cast<PacketPayload>(
static_pointer_cast<Header>(
StructureWrapper<Packet, const struct sr_datafeed_header>::
get_shared_pointer(parent)));
}
int Header::get_feed_version() int Header::get_feed_version()
{ {
return structure->feed_version; return structure->feed_version;
@ -1023,8 +1037,8 @@ Glib::TimeVal Header::get_start_time()
} }
Meta::Meta(const struct sr_datafeed_meta *structure) : Meta::Meta(const struct sr_datafeed_meta *structure) :
PacketPayload(), StructureWrapper<Packet, const struct sr_datafeed_meta>(structure),
StructureWrapper<Packet, const struct sr_datafeed_meta>(structure) PacketPayload()
{ {
} }
@ -1032,6 +1046,14 @@ Meta::~Meta()
{ {
} }
shared_ptr<PacketPayload> Meta::get_shared_pointer(Packet *parent)
{
return static_pointer_cast<PacketPayload>(
static_pointer_cast<Meta>(
StructureWrapper<Packet, const struct sr_datafeed_meta>::
get_shared_pointer(parent)));
}
map<const ConfigKey *, Glib::VariantBase> Meta::get_config() map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
{ {
map<const ConfigKey *, Glib::VariantBase> result; map<const ConfigKey *, Glib::VariantBase> result;
@ -1044,8 +1066,8 @@ map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
} }
Logic::Logic(const struct sr_datafeed_logic *structure) : Logic::Logic(const struct sr_datafeed_logic *structure) :
PacketPayload(), StructureWrapper<Packet, const struct sr_datafeed_logic>(structure),
StructureWrapper<Packet, const struct sr_datafeed_logic>(structure) PacketPayload()
{ {
} }
@ -1053,6 +1075,14 @@ Logic::~Logic()
{ {
} }
shared_ptr<PacketPayload> Logic::get_shared_pointer(Packet *parent)
{
return static_pointer_cast<PacketPayload>(
static_pointer_cast<Logic>(
StructureWrapper<Packet, const struct sr_datafeed_logic>::
get_shared_pointer(parent)));
}
void *Logic::get_data_pointer() void *Logic::get_data_pointer()
{ {
return structure->data; return structure->data;
@ -1069,8 +1099,8 @@ unsigned int Logic::get_unit_size()
} }
Analog::Analog(const struct sr_datafeed_analog *structure) : Analog::Analog(const struct sr_datafeed_analog *structure) :
PacketPayload(), StructureWrapper<Packet, const struct sr_datafeed_analog>(structure),
StructureWrapper<Packet, const struct sr_datafeed_analog>(structure) PacketPayload()
{ {
} }
@ -1078,6 +1108,14 @@ Analog::~Analog()
{ {
} }
shared_ptr<PacketPayload> Analog::get_shared_pointer(Packet *parent)
{
return static_pointer_cast<PacketPayload>(
static_pointer_cast<Analog>(
StructureWrapper<Packet, const struct sr_datafeed_analog>::
get_shared_pointer(parent)));
}
float *Analog::get_data_pointer() float *Analog::get_data_pointer()
{ {
return structure->data; return structure->data;

View File

@ -645,9 +645,7 @@ class SR_API PacketPayload
protected: protected:
PacketPayload(); PacketPayload();
virtual ~PacketPayload() = 0; virtual ~PacketPayload() = 0;
shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) { virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
}
/** Deleter needed to allow shared_ptr use with protected destructor. */ /** Deleter needed to allow shared_ptr use with protected destructor. */
class Deleter class Deleter
{ {
@ -660,8 +658,9 @@ protected:
}; };
/** Payload of a datafeed header packet */ /** Payload of a datafeed header packet */
class SR_API Header : public PacketPayload, class SR_API Header :
public StructureWrapper<Packet, const struct sr_datafeed_header> public StructureWrapper<Packet, const struct sr_datafeed_header>,
public PacketPayload
{ {
public: public:
/* Feed version number. */ /* Feed version number. */
@ -671,13 +670,14 @@ public:
protected: protected:
Header(const struct sr_datafeed_header *structure); Header(const struct sr_datafeed_header *structure);
~Header(); ~Header();
const struct sr_datafeed_header *structure; shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
friend class Packet; friend class Packet;
}; };
/** Payload of a datafeed metadata packet */ /** Payload of a datafeed metadata packet */
class SR_API Meta : public PacketPayload, class SR_API Meta :
public StructureWrapper<Packet, const struct sr_datafeed_meta> public StructureWrapper<Packet, const struct sr_datafeed_meta>,
public PacketPayload
{ {
public: public:
/* Mapping of (ConfigKey, value) pairs. */ /* Mapping of (ConfigKey, value) pairs. */
@ -685,14 +685,15 @@ public:
protected: protected:
Meta(const struct sr_datafeed_meta *structure); Meta(const struct sr_datafeed_meta *structure);
~Meta(); ~Meta();
const struct sr_datafeed_meta *structure; shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
map<const ConfigKey *, Glib::VariantBase> config; map<const ConfigKey *, Glib::VariantBase> config;
friend class Packet; friend class Packet;
}; };
/** Payload of a datafeed packet with logic data */ /** Payload of a datafeed packet with logic data */
class SR_API Logic : public PacketPayload, class SR_API Logic :
public StructureWrapper<Packet, const struct sr_datafeed_logic> public StructureWrapper<Packet, const struct sr_datafeed_logic>,
public PacketPayload
{ {
public: public:
/* Pointer to data. */ /* Pointer to data. */
@ -704,13 +705,14 @@ public:
protected: protected:
Logic(const struct sr_datafeed_logic *structure); Logic(const struct sr_datafeed_logic *structure);
~Logic(); ~Logic();
const struct sr_datafeed_logic *structure; shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
friend class Packet; friend class Packet;
}; };
/** Payload of a datafeed packet with analog data */ /** Payload of a datafeed packet with analog data */
class SR_API Analog : public PacketPayload, class SR_API Analog :
public StructureWrapper<Packet, const struct sr_datafeed_analog> public StructureWrapper<Packet, const struct sr_datafeed_analog>,
public PacketPayload
{ {
public: public:
/** Pointer to data. */ /** Pointer to data. */
@ -728,7 +730,7 @@ public:
protected: protected:
Analog(const struct sr_datafeed_analog *structure); Analog(const struct sr_datafeed_analog *structure);
~Analog(); ~Analog();
const struct sr_datafeed_analog *structure; shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
friend class Packet; friend class Packet;
}; };