2 ## This file is part of the libsigrok project.
4 ## Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
6 ## This program is free software: you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, either version 3 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from xml.etree import ElementTree
21 from collections import OrderedDict
24 index_file = sys.argv[1]
26 # Get directory this script is in.
27 dirname = os.path.dirname(os.path.realpath(__file__))
29 outdirname = "bindings/cxx"
30 if not os.path.exists(os.path.join(outdirname, 'include/libsigrok')):
31 os.makedirs(os.path.join(outdirname, 'include/libsigrok'))
34 ('sr_loglevel', ('LogLevel', 'Log verbosity level')),
35 ('sr_packettype', ('PacketType', 'Type of datafeed packet')),
36 ('sr_mq', ('Quantity', 'Measured quantity')),
37 ('sr_unit', ('Unit', 'Unit of measurement')),
38 ('sr_mqflag', ('QuantityFlag', 'Flag applied to measured quantity')),
39 ('sr_configkey', ('ConfigKey', 'Configuration key')),
40 ('sr_datatype', ('DataType', 'Configuration data type')),
41 ('sr_channeltype', ('ChannelType', 'Channel type')),
42 ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type'))])
44 index = ElementTree.parse(index_file)
46 # Build mapping between class names and enumerations.
48 classes = OrderedDict()
50 for compound in index.findall('compound'):
51 if compound.attrib['kind'] != 'file':
53 filename = os.path.join(
54 os.path.dirname(index_file),
55 '%s.xml' % compound.attrib['refid'])
56 doc = ElementTree.parse(filename)
57 for section in doc.find('compounddef').findall('sectiondef'):
58 if section.attrib["kind"] != 'enum':
60 for member in section.findall('memberdef'):
61 if member.attrib["kind"] != 'enum':
63 name = member.find('name').text
65 classes[member] = mapping[name]
67 header = open(os.path.join(outdirname, 'include/libsigrok/enums.hpp'), 'w')
68 code = open(os.path.join(outdirname, 'enums.cpp'), 'w')
70 for file in (header, code):
71 print >> file, "/* Generated file - edit enums.py instead! */"
73 # Template for beginning of class declaration and public members.
74 header_public_template = """
76 class SR_API {classname} : public EnumValue<{classname}, enum {enumname}>
81 # Template for beginning of private members.
82 header_private_template = """
84 {classname}(enum {enumname} id, const char name[]) : EnumValue(id, name) {{}}
88 return str.join('\n\n',
89 [p.text.rstrip() for p in node.findall('para')])
91 for enum, (classname, classbrief) in classes.items():
93 enum_name = enum.find('name').text
94 members = enum.findall('enumvalue')
95 member_names = [m.find('name').text for m in members]
96 trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names]
97 briefs = [get_text(m.find('briefdescription')) for m in members]
99 # Begin class and public declarations
100 print >> header, header_public_template.format(
101 brief=classbrief, classname=classname, enumname=enum_name)
103 # Declare public pointers for each enum value
104 for trimmed_name, brief in zip(trimmed_names, briefs):
106 print >> header, '\t/** %s */' % brief
107 print >> header, '\tstatic const %s * const %s;' % (
108 classname, trimmed_name)
110 # Declare additional methods if present
111 filename = os.path.join(dirname, "%s_methods.hpp" % classname)
112 if os.path.exists(filename):
113 print >> header, str.join('', open(filename).readlines())
115 # Begin private declarations
116 print >> header, header_private_template.format(
117 classname=classname, enumname=enum_name)
119 # Declare private constants for each enum value
120 for trimmed_name in trimmed_names:
121 print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name)
123 # End class declaration
124 print >> header, '};'
126 # Define private constants for each enum value
127 for name, trimmed_name in zip(member_names, trimmed_names):
128 print >> code, 'const %s %s::_%s = %s(%s, "%s");' % (
129 classname, classname, trimmed_name, classname, name, trimmed_name)
131 # Define public pointers for each enum value
132 for trimmed_name in trimmed_names:
133 print >> code, 'const %s * const %s::%s = &%s::_%s;' % (
134 classname, classname, trimmed_name, classname, trimmed_name)
136 # Define map of enum values to constants
137 print >> code, 'template<> const std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % (
138 enum_name, classname, classname, enum_name)
139 for name, trimmed_name in zip(member_names, trimmed_names):
140 print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name)
143 # Define additional methods if present
144 filename = os.path.join(dirname, "%s_methods.cpp" % classname)
145 if os.path.exists(filename):
146 print >> code, str.join('', open(filename).readlines())