X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=inline;f=bindings%2Fcxx%2Fenums.py;h=3711334e74f740135a94f58ef70e78e7a8570268;hb=aedbf89d85cc7efa1263f5e210c3b78097ad471a;hp=9eddd1ad9631fe61344b7ec13068bf5cc0adb29c;hpb=174bf146e5973cb7f11892c54972bc99e4a74eae;p=libsigrok.git
diff --git a/bindings/cxx/enums.py b/bindings/cxx/enums.py
index 9eddd1ad..3711334e 100644
--- a/bindings/cxx/enums.py
+++ b/bindings/cxx/enums.py
@@ -17,6 +17,7 @@
## along with this program. If not, see .
##
+from __future__ import print_function
from xml.etree import ElementTree
from collections import OrderedDict
import sys, os, re
@@ -26,9 +27,11 @@ index_file = sys.argv[1]
# Get directory this script is in.
dirname = os.path.dirname(os.path.realpath(__file__))
-outdirname = "bindings/cxx"
-if not os.path.exists(os.path.join(outdirname, 'include/libsigrok')):
- os.makedirs(os.path.join(outdirname, 'include/libsigrok'))
+outdirname = "bindings"
+if not os.path.exists(os.path.join(outdirname, 'cxx/include/libsigrokcxx')):
+ os.makedirs(os.path.join(outdirname, 'cxx/include/libsigrokcxx'))
+if not os.path.exists(os.path.join(outdirname, 'swig')):
+ os.makedirs(os.path.join(outdirname, 'swig'))
mapping = dict([
('sr_loglevel', ('LogLevel', 'Log verbosity level')),
@@ -37,9 +40,11 @@ mapping = dict([
('sr_unit', ('Unit', 'Unit of measurement')),
('sr_mqflag', ('QuantityFlag', 'Flag applied to measured quantity')),
('sr_configkey', ('ConfigKey', 'Configuration key')),
+ ('sr_configcap', ('Capability', 'Configuration capability')),
('sr_datatype', ('DataType', 'Configuration data type')),
('sr_channeltype', ('ChannelType', 'Channel type')),
- ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type'))])
+ ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type')),
+ ('sr_output_flag', ('OutputFlag', 'Flag applied to output modules'))])
index = ElementTree.parse(index_file)
@@ -64,42 +69,29 @@ for compound in index.findall('compound'):
if name in mapping:
classes[member] = mapping[name]
-header = open(os.path.join(outdirname, 'include/libsigrok/enums.hpp'), 'w')
-code = open(os.path.join(outdirname, 'enums.cpp'), 'w')
+header = open(os.path.join(outdirname, 'cxx/include/libsigrokcxx/enums.hpp'), 'w')
+code = open(os.path.join(outdirname, 'cxx/enums.cpp'), 'w')
+swig = open(os.path.join(outdirname, 'swig/enums.i'), 'w')
for file in (header, code):
- print >> file, "/* Generated file - edit enums.py instead! */"
+ print("/* Generated file - edit enums.py instead! */", file=file)
+
+print("namespace sigrok {", file=header)
# Template for beginning of class declaration and public members.
header_public_template = """
+template<> const SR_API std::map EnumValue<{classname}, enum {enumname}>::_values;
+
/** {brief} */
-class SR_API {classname} : public EnumValue
+class SR_API {classname} : public EnumValue<{classname}, enum {enumname}>
{{
public:
- static const {classname} *get(int id);
"""
# Template for beginning of private members.
header_private_template = """
-private:
- static const std::map _values;
- {classname}(enum {enumname} id, const char name[]);
-"""
-
-# Template for class method definitions.
-code_template = """
-{classname}::{classname}(enum {enumname} id, const char name[]) :
- EnumValue(id, name)
-{{
-}}
-
-const {classname} *{classname}::get(int id)
-{{
- if (_values.find(static_cast<{enumname}>(id)) == _values.end())
- throw Error(SR_ERR_ARG);
-
- return {classname}::_values.at(static_cast<{enumname}>(id));
-}}
+protected:
+ {classname}(enum {enumname} id, const char name[]) : EnumValue(id, name) {{}}
"""
def get_text(node):
@@ -115,54 +107,71 @@ for enum, (classname, classbrief) in classes.items():
briefs = [get_text(m.find('briefdescription')) for m in members]
# Begin class and public declarations
- print >> header, header_public_template.format(
- brief=classbrief, classname=classname, enumname=enum_name)
+ print(header_public_template.format(
+ brief=classbrief, classname=classname, enumname=enum_name), file=header)
# Declare public pointers for each enum value
for trimmed_name, brief in zip(trimmed_names, briefs):
if brief:
- print >> header, '\t/** %s */' % brief
- print >> header, '\tstatic const %s * const %s;' % (
- classname, trimmed_name)
+ print('\t/** %s */' % brief, file=header)
+ print('\tstatic const %s * const %s;' % (
+ classname, trimmed_name), file=header)
# Declare additional methods if present
filename = os.path.join(dirname, "%s_methods.hpp" % classname)
if os.path.exists(filename):
- print >> header, str.join('', open(filename).readlines())
+ print(str.join('', open(filename).readlines()), file=header)
# Begin private declarations
- print >> header, header_private_template.format(
- classname=classname, enumname=enum_name)
+ print(header_private_template.format(
+ classname=classname, enumname=enum_name), file=header)
# Declare private constants for each enum value
for trimmed_name in trimmed_names:
- print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name)
+ print('\tstatic const %s _%s;' % (classname, trimmed_name), file=header)
# End class declaration
- print >> header, '};'
-
- # Begin class code
- print >> code, code_template.format(
- classname=classname, enumname=enum_name)
+ print('};', file=header)
# Define private constants for each enum value
for name, trimmed_name in zip(member_names, trimmed_names):
- print >> code, 'const %s %s::_%s = %s(%s, "%s");' % (
- classname, classname, trimmed_name, classname, name, trimmed_name)
+ print('const %s %s::_%s = %s(%s, "%s");' % (
+ classname, classname, trimmed_name, classname, name, trimmed_name),
+ file=code)
# Define public pointers for each enum value
for trimmed_name in trimmed_names:
- print >> code, 'const %s * const %s::%s = &%s::_%s;' % (
- classname, classname, trimmed_name, classname, trimmed_name)
+ print('const %s * const %s::%s = &%s::_%s;' % (
+ classname, classname, trimmed_name, classname, trimmed_name),
+ file=code)
# Define map of enum values to constants
- print >> code, 'const std::map %s::_values = {' % (
- enum_name, classname, classname)
+ print('template<> const SR_API std::map EnumValue<%s, enum %s>::_values = {' % (
+ enum_name, classname, classname, enum_name), file=code)
for name, trimmed_name in zip(member_names, trimmed_names):
- print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name)
- print >> code, '};'
+ print('\t{%s, %s::%s},' % (name, classname, trimmed_name), file=code)
+ print('};', file=code)
# Define additional methods if present
filename = os.path.join(dirname, "%s_methods.cpp" % classname)
if os.path.exists(filename):
- print >> code, str.join('', open(filename).readlines())
+ print(str.join('', open(filename).readlines()), file=code)
+
+ # Map EnumValue::id() and EnumValue::name() as SWIG attributes.
+ print('%%attribute(sigrok::%s, int, id, id);' % classname, file=swig)
+ print('%%attributestring(sigrok::%s, std::string, name, name);' % classname,
+ file=swig)
+
+ # Instantiate EnumValue template for SWIG
+ print('%%template(EnumValue%s) sigrok::EnumValue;' % (
+ classname, classname, enum_name), file=swig)
+
+ # Apply any language-specific extras.
+ print('%%enumextras(%s);' % classname, file=swig)
+
+ # Declare additional attributes if present
+ filename = os.path.join(dirname, "%s_methods.i" % classname)
+ if os.path.exists(filename):
+ print(str.join('', open(filename).readlines()), file=swig)
+
+print("}", file=header)