}
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++)
}
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));
}
Session::Session(shared_ptr<Context> context) :
+ UserOwned(structure),
context(context), saving(false)
{
check(sr_session_new(&structure));
}
Session::Session(shared_ptr<Context> context, string filename) :
+ UserOwned(structure),
context(context), saving(false)
{
check(sr_session_load(filename.c_str(), &structure));
Packet::Packet(shared_ptr<Device> device,
const struct sr_datafeed_packet *structure) :
- structure(structure),
+ UserOwned(structure),
device(device)
{
switch (structure->type)
}
Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
- structure(structure),
+ UserOwned(structure),
context(context),
device(nullptr)
{
Option::Option(const struct sr_option *structure,
shared_ptr<const struct sr_option *> structure_array) :
- structure(structure),
+ UserOwned(structure),
structure_array(structure_array)
{
}
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)
{
}
};
+/* 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 */
* @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;
};
/** 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. */
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. */
};
/** A sigrok session */
-class SR_API Session
+class SR_API Session : public UserOwned<Session, struct sr_session>
{
public:
/** Add a device to this session.
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;
};
/** 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. */
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. */
};
/** 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. */
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. */
};
/** 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. */
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
};
/** 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.
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;