C++: Add UserOwned base template for objects with resources owned by user.
This commit is contained in:
parent
541c855e1d
commit
90e89c2a42
|
@ -71,9 +71,11 @@ shared_ptr<Context> 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++)
|
||||
|
@ -533,7 +535,8 @@ vector<shared_ptr<Channel>> ChannelGroup::get_channels()
|
|||
}
|
||||
|
||||
Trigger::Trigger(shared_ptr<Context> 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));
|
||||
|
@ -690,6 +693,7 @@ EventSource::~EventSource()
|
|||
}
|
||||
|
||||
Session::Session(shared_ptr<Context> context) :
|
||||
UserOwned(structure),
|
||||
context(context), saving(false)
|
||||
{
|
||||
check(sr_session_new(&structure));
|
||||
|
@ -697,6 +701,7 @@ Session::Session(shared_ptr<Context> context) :
|
|||
}
|
||||
|
||||
Session::Session(shared_ptr<Context> context, string filename) :
|
||||
UserOwned(structure),
|
||||
context(context), saving(false)
|
||||
{
|
||||
check(sr_session_load(filename.c_str(), &structure));
|
||||
|
@ -936,7 +941,7 @@ void Session::set_trigger(shared_ptr<Trigger> trigger)
|
|||
|
||||
Packet::Packet(shared_ptr<Device> device,
|
||||
const struct sr_datafeed_packet *structure) :
|
||||
structure(structure),
|
||||
UserOwned(structure),
|
||||
device(device)
|
||||
{
|
||||
switch (structure->type)
|
||||
|
@ -1168,7 +1173,7 @@ shared_ptr<Input> InputFormat::create_input(
|
|||
}
|
||||
|
||||
Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
|
||||
structure(structure),
|
||||
UserOwned(structure),
|
||||
context(context),
|
||||
device(nullptr)
|
||||
{
|
||||
|
@ -1221,7 +1226,7 @@ shared_ptr<Device> InputDevice::get_shared_from_this()
|
|||
|
||||
Option::Option(const struct sr_option *structure,
|
||||
shared_ptr<const struct sr_option *> structure_array) :
|
||||
structure(structure),
|
||||
UserOwned(structure),
|
||||
structure_array(structure_array)
|
||||
{
|
||||
}
|
||||
|
@ -1299,7 +1304,7 @@ shared_ptr<Output> OutputFormat::create_output(
|
|||
|
||||
Output::Output(shared_ptr<OutputFormat> format,
|
||||
shared_ptr<Device> device, map<string, Glib::VariantBase> 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)
|
||||
{
|
||||
|
|
|
@ -192,11 +192,32 @@ protected:
|
|||
}
|
||||
};
|
||||
|
||||
/* Base template for classes whose resources are owned by the user. */
|
||||
template <class Class, typename Struct>
|
||||
class SR_API UserOwned : public enable_shared_from_this<Class>
|
||||
{
|
||||
public:
|
||||
shared_ptr<Class> shared_from_this()
|
||||
{
|
||||
auto shared = enable_shared_from_this<Class>::shared_from_this();
|
||||
if (!shared)
|
||||
throw Error(SR_ERR_BUG);
|
||||
return shared;
|
||||
}
|
||||
protected:
|
||||
Struct *structure;
|
||||
|
||||
UserOwned<Class, Struct>(Struct *structure) :
|
||||
structure(structure)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/** Type of log callback */
|
||||
typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
|
||||
|
||||
/** The global libsigrok context */
|
||||
class SR_API Context : public enable_shared_from_this<Context>
|
||||
class SR_API Context : public UserOwned<Context, struct sr_context>
|
||||
{
|
||||
public:
|
||||
/** Create new context */
|
||||
|
@ -241,7 +262,6 @@ public:
|
|||
* @param header Initial data from stream. */
|
||||
shared_ptr<Input> open_stream(string header);
|
||||
protected:
|
||||
struct sr_context *structure;
|
||||
map<string, Driver *> drivers;
|
||||
map<string, InputFormat *> input_formats;
|
||||
map<string, OutputFormat *> output_formats;
|
||||
|
@ -414,7 +434,7 @@ protected:
|
|||
};
|
||||
|
||||
/** A trigger configuration */
|
||||
class SR_API Trigger : public enable_shared_from_this<Trigger>
|
||||
class SR_API Trigger : public UserOwned<Trigger, struct sr_trigger>
|
||||
{
|
||||
public:
|
||||
/** Name of this trigger configuration. */
|
||||
|
@ -426,7 +446,6 @@ public:
|
|||
protected:
|
||||
Trigger(shared_ptr<Context> context, string name);
|
||||
~Trigger();
|
||||
struct sr_trigger *structure;
|
||||
shared_ptr<Context> context;
|
||||
vector<TriggerStage *> stages;
|
||||
/** Deleter needed to allow shared_ptr use with protected destructor. */
|
||||
|
@ -566,7 +585,7 @@ protected:
|
|||
};
|
||||
|
||||
/** A sigrok session */
|
||||
class SR_API Session
|
||||
class SR_API Session : public UserOwned<Session, struct sr_session>
|
||||
{
|
||||
public:
|
||||
/** Add a device to this session.
|
||||
|
@ -610,7 +629,6 @@ protected:
|
|||
Session(shared_ptr<Context> context);
|
||||
Session(shared_ptr<Context> context, string filename);
|
||||
~Session();
|
||||
struct sr_session *structure;
|
||||
const shared_ptr<Context> context;
|
||||
map<const struct sr_dev_inst *, shared_ptr<Device> > devices;
|
||||
vector<DatafeedCallbackData *> datafeed_callbacks;
|
||||
|
@ -632,7 +650,7 @@ protected:
|
|||
};
|
||||
|
||||
/** A packet on the session datafeed */
|
||||
class SR_API Packet : public enable_shared_from_this<Packet>
|
||||
class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
|
||||
{
|
||||
public:
|
||||
/** Type of this packet. */
|
||||
|
@ -643,7 +661,6 @@ protected:
|
|||
Packet(shared_ptr<Device> device,
|
||||
const struct sr_datafeed_packet *structure);
|
||||
~Packet();
|
||||
const struct sr_datafeed_packet *structure;
|
||||
shared_ptr<Device> device;
|
||||
PacketPayload *payload;
|
||||
/** Deleter needed to allow shared_ptr use with protected destructor. */
|
||||
|
@ -779,7 +796,7 @@ protected:
|
|||
};
|
||||
|
||||
/** An input instance (an input format applied to a file or stream) */
|
||||
class SR_API Input : public enable_shared_from_this<Input>
|
||||
class SR_API Input : public UserOwned<Input, const struct sr_input>
|
||||
{
|
||||
public:
|
||||
/** Virtual device associated with this input. */
|
||||
|
@ -790,7 +807,6 @@ public:
|
|||
protected:
|
||||
Input(shared_ptr<Context> context, const struct sr_input *structure);
|
||||
~Input();
|
||||
const struct sr_input *structure;
|
||||
shared_ptr<Context> context;
|
||||
InputDevice *device;
|
||||
/** Deleter needed to allow shared_ptr use with protected destructor. */
|
||||
|
@ -825,7 +841,7 @@ protected:
|
|||
};
|
||||
|
||||
/** An option used by an output format */
|
||||
class SR_API Option
|
||||
class SR_API Option : public UserOwned<Option, const struct sr_option>
|
||||
{
|
||||
public:
|
||||
/** Short name of this option suitable for command line usage. */
|
||||
|
@ -842,7 +858,6 @@ protected:
|
|||
Option(const struct sr_option *structure,
|
||||
shared_ptr<const struct sr_option *> structure_array);
|
||||
~Option();
|
||||
const struct sr_option *structure;
|
||||
shared_ptr<const struct sr_option *> structure_array;
|
||||
/** Deleter needed to allow shared_ptr use with protected destructor. */
|
||||
class Deleter
|
||||
|
@ -879,7 +894,7 @@ protected:
|
|||
};
|
||||
|
||||
/** An output instance (an output format applied to a device) */
|
||||
class SR_API Output
|
||||
class SR_API Output : public UserOwned<Output, const struct sr_output>
|
||||
{
|
||||
public:
|
||||
/** Update output with data from the given packet.
|
||||
|
@ -890,7 +905,6 @@ protected:
|
|||
Output(shared_ptr<OutputFormat> format,
|
||||
shared_ptr<Device> device, map<string, Glib::VariantBase> options);
|
||||
~Output();
|
||||
const struct sr_output *structure;
|
||||
const shared_ptr<OutputFormat> format;
|
||||
const shared_ptr<Device> device;
|
||||
const map<string, Glib::VariantBase> options;
|
||||
|
|
Loading…
Reference in New Issue