Update bindings for new input API.

This commit is contained in:
Martin Ling 2014-08-24 01:40:19 +01:00 committed by Bert Vermeulen
parent b84cba4dbf
commit ca3291e3ee
5 changed files with 139 additions and 77 deletions

View File

@ -40,18 +40,6 @@ static const char *valid_string(const char *input)
return ""; return "";
} }
/** Helper function to convert between map<string, string> and GHashTable */
static GHashTable *map_to_hash_string(map<string, string> input)
{
auto output = g_hash_table_new_full(
g_str_hash, g_str_equal, g_free, g_free);
for (auto entry : input)
g_hash_table_insert(output,
g_strdup(entry.first.c_str()),
g_strdup(entry.second.c_str()));
return output;
}
/** Helper function to convert between map<string, VariantBase> and GHashTable */ /** Helper function to convert between map<string, VariantBase> and GHashTable */
static GHashTable *map_to_hash_variant(map<string, Glib::VariantBase> input) static GHashTable *map_to_hash_variant(map<string, Glib::VariantBase> input)
{ {
@ -92,10 +80,10 @@ Context::Context() :
for (int i = 0; driver_list[i]; i++) for (int i = 0; driver_list[i]; i++)
drivers[driver_list[i]->name] = drivers[driver_list[i]->name] =
new Driver(driver_list[i]); new Driver(driver_list[i]);
struct sr_input_format **input_list = sr_input_list(); const struct sr_input_module **input_list = sr_input_list();
if (input_list) if (input_list)
for (int i = 0; input_list[i]; i++) for (int i = 0; input_list[i]; i++)
input_formats[input_list[i]->id] = input_formats[sr_input_id_get(input_list[i])] =
new InputFormat(input_list[i]); new InputFormat(input_list[i]);
const struct sr_output_module **output_list = sr_output_list(); const struct sr_output_module **output_list = sr_output_list();
if (output_list) if (output_list)
@ -239,6 +227,26 @@ shared_ptr<Trigger> Context::create_trigger(string name)
new Trigger(shared_from_this(), name), Trigger::Deleter()); new Trigger(shared_from_this(), name), Trigger::Deleter());
} }
shared_ptr<Input> Context::open_file(string filename)
{
auto input = sr_input_scan_file(filename.c_str());
if (!input)
throw Error(SR_ERR_NA);
return shared_ptr<Input>(
new Input(shared_from_this(), input), Input::Deleter());
}
shared_ptr<Input> Context::open_stream(string header)
{
auto gstr = g_string_new(header.c_str());
auto input = sr_input_scan_buffer(gstr);
g_string_free(gstr, false);
if (!input)
throw Error(SR_ERR_NA);
return shared_ptr<Input>(
new Input(shared_from_this(), input), Input::Deleter());
}
Driver::Driver(struct sr_dev_driver *structure) : Driver::Driver(struct sr_dev_driver *structure) :
StructureWrapper<Context, struct sr_dev_driver>(structure), StructureWrapper<Context, struct sr_dev_driver>(structure),
initialized(false) initialized(false)
@ -1100,8 +1108,8 @@ vector<const QuantityFlag *> Analog::get_mq_flags()
return QuantityFlag::flags_from_mask(structure->mqflags); return QuantityFlag::flags_from_mask(structure->mqflags);
} }
InputFormat::InputFormat(struct sr_input_format *structure) : InputFormat::InputFormat(const struct sr_input_module *structure) :
StructureWrapper<Context, struct sr_input_format>(structure) StructureWrapper<Context, const struct sr_input_module>(structure)
{ {
} }
@ -1111,52 +1119,68 @@ InputFormat::~InputFormat()
string InputFormat::get_name() string InputFormat::get_name()
{ {
return valid_string(structure->id); return valid_string(sr_input_id_get(structure));
} }
string InputFormat::get_description() string InputFormat::get_description()
{ {
return valid_string(structure->description); return valid_string(sr_input_description_get(structure));
} }
bool InputFormat::format_match(string filename) shared_ptr<Input> InputFormat::create_input(
map<string, Glib::VariantBase> options)
{ {
return structure->format_match(filename.c_str()); auto input = sr_input_new(structure, map_to_hash_variant(options));
if (!input)
throw Error(SR_ERR_ARG);
return shared_ptr<Input>(
new Input(parent->shared_from_this(), input), Input::Deleter());
} }
shared_ptr<InputFileDevice> InputFormat::open_file(string filename, Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
map<string, string> options) structure(structure),
{ context(context),
auto input = g_new(struct sr_input, 1); device(nullptr)
input->param = map_to_hash_string(options);
/** Run initialisation. */
check(structure->init(input, filename.c_str()));
/** Create virtual device. */
return shared_ptr<InputFileDevice>(new InputFileDevice(
static_pointer_cast<InputFormat>(shared_from_this()), input, filename),
InputFileDevice::Deleter());
}
InputFileDevice::InputFileDevice(shared_ptr<InputFormat> format,
struct sr_input *input, string filename) :
Device(input->sdi),
input(input),
format(format),
filename(filename)
{ {
} }
InputFileDevice::~InputFileDevice() shared_ptr<InputDevice> Input::get_device()
{ {
g_hash_table_unref(input->param); if (!device)
g_free(input); {
auto sdi = sr_input_dev_inst_get(structure);
if (!sdi)
throw Error(SR_ERR_NA);
device = new InputDevice(shared_from_this(), sdi);
}
return static_pointer_cast<InputDevice>(
device->get_shared_pointer(context->shared_from_this()));
} }
void InputFileDevice::load() void Input::send(string data)
{
auto gstr = g_string_new(data.c_str());
auto ret = sr_input_send(structure, gstr);
g_string_free(gstr, false);
check(ret);
}
Input::~Input()
{
if (device)
delete device;
check(sr_input_free(structure));
}
InputDevice::InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi) :
Device(sdi),
input(input)
{
}
InputDevice::~InputDevice()
{ {
check(format->structure->loadfile(input, filename.c_str()));
} }
Option::Option(const struct sr_option *structure, Option::Option(const struct sr_option *structure,

View File

@ -108,7 +108,8 @@ class SR_API PacketType;
class SR_API Quantity; class SR_API Quantity;
class SR_API Unit; class SR_API Unit;
class SR_API QuantityFlag; class SR_API QuantityFlag;
class SR_API InputFileDevice; class SR_API Input;
class SR_API InputDevice;
class SR_API Output; class SR_API Output;
class SR_API DataType; class SR_API DataType;
class SR_API Option; class SR_API Option;
@ -214,6 +215,12 @@ public:
/** Create a new trigger. /** Create a new trigger.
* @param name Name string for new trigger. */ * @param name Name string for new trigger. */
shared_ptr<Trigger> create_trigger(string name); shared_ptr<Trigger> create_trigger(string name);
/** Open an input file.
* @param filename File name string. */
shared_ptr<Input> open_file(string filename);
/** Open an input stream based on header data.
* @param header Initial data from stream. */
shared_ptr<Input> open_stream(string header);
protected: protected:
struct sr_context *structure; struct sr_context *structure;
map<string, Driver *> drivers; map<string, Driver *> drivers;
@ -724,51 +731,68 @@ protected:
/** An input format supported by the library */ /** An input format supported by the library */
class SR_API InputFormat : class SR_API InputFormat :
public StructureWrapper<Context, struct sr_input_format> public StructureWrapper<Context, const struct sr_input_module>
{ {
public: public:
/** Name of this input format. */ /** Name of this input format. */
string get_name(); string get_name();
/** Description of this input format. */ /** Description of this input format. */
string get_description(); string get_description();
/** Check whether a given file matches this input format. /** Options supported by this input format. */
* @param filename File name string. */ map<string, shared_ptr<Option> > get_options();
bool format_match(string filename); /** Create an input using this input format.
/** Open a file using this input format. * @param options Mapping of (option name, value) pairs. */
* @param filename File name string. shared_ptr<Input> create_input(map<string, Glib::VariantBase> options = {});
* @param options Mapping of (option name, value) strings. */
shared_ptr<InputFileDevice> open_file(string filename,
map<string, string> options = {});
protected: protected:
InputFormat(struct sr_input_format *structure); InputFormat(const struct sr_input_module *structure);
~InputFormat(); ~InputFormat();
friend class Context; friend class Context;
friend class InputFileDevice; friend class InputDevice;
}; };
/** A virtual device associated with an input file */ /** An input instance (an input format applied to a file or stream) */
class SR_API InputFileDevice : public Device class SR_API Input : public enable_shared_from_this<Input>
{ {
public: public:
/** Load data from file. */ /** Virtual device associated with this input. */
void load(); shared_ptr<InputDevice> get_device();
/** Send next stream data.
* @param data Next stream data. */
void send(string data);
protected: protected:
InputFileDevice(shared_ptr<InputFormat> format, Input(shared_ptr<Context> context, const struct sr_input *structure);
struct sr_input *input, string filename); ~Input();
~InputFileDevice(); const struct sr_input *structure;
struct sr_input *input; shared_ptr<Context> context;
shared_ptr<InputFormat> format; InputDevice *device;
string filename;
/** Deleter needed to allow shared_ptr use with protected destructor. */ /** Deleter needed to allow shared_ptr use with protected destructor. */
class Deleter class Deleter
{ {
public: public:
void operator()(InputFileDevice *device) { delete device; } void operator()(Input *input) { delete input; }
}; };
friend class Deleter; friend class Deleter;
friend class Context;
friend class InputFormat; friend class InputFormat;
}; };
/** A virtual device associated with an input */
class SR_API InputDevice : public Device
{
protected:
InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
~InputDevice();
shared_ptr<Input> input;
/** Deleter needed to allow shared_ptr use with protected destructor. */
class Deleter
{
public:
void operator()(InputDevice *device) { delete device; }
};
friend class Deleter;
friend class Input;
};
/** An option used by an output format */ /** An option used by an output format */
class SR_API Option class SR_API Option
{ {

View File

@ -224,6 +224,15 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
} }
} }
/* Support InputFormat.create_input() with no options. */
%extend sigrok::InputFormat {
std::shared_ptr<sigrok::Input> create_input()
{
std::map<std::string, Glib::VariantBase> options;
return $self->create_input(options);
}
}
/* Support OutputFormat.create_output(device) with no options. */ /* Support OutputFormat.create_output(device) with no options. */
%extend sigrok::OutputFormat { %extend sigrok::OutputFormat {
std::shared_ptr<sigrok::Output> create_output( std::shared_ptr<sigrok::Output> create_output(

View File

@ -354,7 +354,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
/* Ignore these methods, we will override them below. */ /* Ignore these methods, we will override them below. */
%ignore sigrok::Driver::scan; %ignore sigrok::Driver::scan;
%ignore sigrok::InputFormat::open_file; %ignore sigrok::InputFormat::create_input;
%ignore sigrok::OutputFormat::create_output; %ignore sigrok::OutputFormat::create_output;
%include "doc.i" %include "doc.i"
@ -394,21 +394,22 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
Driver.scan = _Driver_scan Driver.scan = _Driver_scan
} }
/* Support InputFormat.open_file() with keyword arguments. */ /* Support InputFormat.create_input() with keyword arguments. */
%extend sigrok::InputFormat %extend sigrok::InputFormat
{ {
std::shared_ptr<sigrok::InputFileDevice> _open_file_kwargs(std::string filename, PyObject *dict) std::shared_ptr<sigrok::Input> _create_input_kwargs(PyObject *dict)
{ {
return $self->open_file(filename, dict_to_map_string(dict)); return $self->create_input(
dict_to_map_options(dict, $self->get_options()));
} }
} }
%pythoncode %pythoncode
{ {
def _InputFormat_open_file(self, filename, **kwargs): def _InputFormat_create_input(self, **kwargs):
return self._open_file_kwargs(filename, kwargs) return self._create_input(kwargs)
InputFormat.open_file = _InputFormat_open_file InputFormat.create_input = _InputFormat_create_input
} }
/* Support OutputFormat.create_output() with keyword arguments. */ /* Support OutputFormat.create_output() with keyword arguments. */

View File

@ -75,7 +75,8 @@ template< class T > class enable_shared_from_this;
%shared_ptr(sigrok::Analog); %shared_ptr(sigrok::Analog);
%shared_ptr(sigrok::Logic); %shared_ptr(sigrok::Logic);
%shared_ptr(sigrok::InputFormat); %shared_ptr(sigrok::InputFormat);
%shared_ptr(sigrok::InputFileDevice); %shared_ptr(sigrok::Input);
%shared_ptr(sigrok::InputDevice);
%shared_ptr(sigrok::Option); %shared_ptr(sigrok::Option);
%shared_ptr(sigrok::OutputFormat); %shared_ptr(sigrok::OutputFormat);
%shared_ptr(sigrok::Output); %shared_ptr(sigrok::Output);
@ -190,6 +191,9 @@ typedef std::map<const sigrok::ConfigKey *, Glib::VariantBase>
%attributestring(sigrok::InputFormat, %attributestring(sigrok::InputFormat,
std::string, description, get_description); std::string, description, get_description);
%attributestring(sigrok::Input,
std::shared_ptr<sigrok::InputDevice>, device, get_device);
%attributestring(sigrok::Option, %attributestring(sigrok::Option,
std::string, id, get_id); std::string, id, get_id);
%attributestring(sigrok::Option, %attributestring(sigrok::Option,