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__))
30 ('sr_loglevel', 'LogLevel'),
31 ('sr_packettype', 'PacketType'),
32 ('sr_mq', 'Quantity'),
34 ('sr_mqflag', 'QuantityFlag'),
35 ('sr_configkey', 'ConfigKey'),
36 ('sr_datatype', 'DataType'),
37 ('sr_channeltype', 'ChannelType'),
38 ('sr_trigger_matches', 'TriggerMatchType')])
40 index = ElementTree.parse(index_file)
42 # Build mapping between class names and enumerations.
44 classes = OrderedDict()
46 for compound in index.findall('compound'):
47 if compound.attrib['kind'] != 'file':
49 filename = os.path.join(
50 os.path.dirname(index_file),
51 '%s.xml' % compound.attrib['refid'])
52 doc = ElementTree.parse(filename)
53 for section in doc.find('compounddef').findall('sectiondef'):
54 if section.attrib["kind"] != 'enum':
56 for member in section.findall('memberdef'):
57 if member.attrib["kind"] != 'enum':
59 name = member.find('name').text
61 classes[member] = mapping[name]
63 header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w')
64 code = open(os.path.join(dirname, 'enums.cpp'), 'w')
66 for file in (header, code):
67 print >> file, "/* Generated file - edit enums.py instead! */"
69 # Template for beginning of class declaration and public members.
70 header_public_template = """
71 class SR_API {classname} : public EnumValue<enum {enumname}>
74 static const {classname} *get(int id);
77 # Template for beginning of private members.
78 header_private_template = """
80 static const std::map<enum {enumname}, const {classname} *> values;
81 {classname}(enum {enumname} id, const char name[]);
84 # Template for class method definitions.
86 {classname}::{classname}(enum {enumname} id, const char name[]) :
87 EnumValue<enum {enumname}>(id, name)
91 const {classname} *{classname}::get(int id)
93 return {classname}::values.at(static_cast<{enumname}>(id));
97 for enum, classname in classes.items():
99 enum_name = enum.find('name').text
100 member_names = [m.find('name').text for m in enum.findall('enumvalue')]
101 trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names]
103 # Begin class and public declarations
104 print >> header, header_public_template.format(
105 classname=classname, enumname=enum_name)
107 # Declare public pointers for each enum value
108 for trimmed_name in trimmed_names:
109 print >> header, '\tstatic const %s * const %s;' % (
110 classname, trimmed_name)
112 # Declare additional methods if present
113 filename = os.path.join(dirname, "%s_methods.hpp" % classname)
114 if os.path.exists(filename):
115 print >> header, str.join('', open(filename).readlines())
117 # Begin private declarations
118 print >> header, header_private_template.format(
119 classname=classname, enumname=enum_name)
121 # Declare private constants for each enum value
122 for trimmed_name in trimmed_names:
123 print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name)
125 # End class declaration
126 print >> header, '};'
129 print >> code, code_template.format(
130 classname=classname, enumname=enum_name)
132 # Define private constants for each enum value
133 for name, trimmed_name in zip(member_names, trimmed_names):
134 print >> code, 'const %s %s::_%s = %s(%s, "%s");' % (
135 classname, classname, trimmed_name, classname, name, trimmed_name)
137 # Define public pointers for each enum value
138 for trimmed_name in trimmed_names:
139 print >> code, 'const %s * const %s::%s = &%s::_%s;' % (
140 classname, classname, trimmed_name, classname, trimmed_name)
142 # Define map of enum values to constants
143 print >> code, 'const std::map<enum %s, const %s *> %s::values = {' % (
144 enum_name, classname, classname)
145 for name, trimmed_name in zip(member_names, trimmed_names):
146 print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name)
149 # Define additional methods if present
150 filename = os.path.join(dirname, "%s_methods.cpp" % classname)
151 if os.path.exists(filename):
152 print >> code, str.join('', open(filename).readlines())