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 pygccxml import parser, declarations
21 from collections import OrderedDict
24 # Get directory this script is in.
25 dirname = os.path.dirname(os.path.realpath(__file__))
27 # Parse GCCXML output to get declaration tree.
28 decls = parser.parse_xml_file(
29 os.path.join(dirname, "libsigrok.xml"), parser.config_t())
31 # Get global namespace from declaration tree.
32 ns = declarations.get_global_namespace(decls)
35 ('sr_loglevel', 'LogLevel'),
36 ('sr_packettype', 'PacketType'),
37 ('sr_mq', 'Quantity'),
39 ('sr_mqflag', 'QuantityFlag'),
40 ('sr_configkey', 'ConfigKey'),
41 ('sr_datatype', 'DataType'),
42 ('sr_channeltype', 'ChannelType'),
43 ('sr_trigger_matches', 'TriggerMatchType')])
45 classes = OrderedDict()
47 # Build mapping between class names and enumerations.
48 for enum in ns.enumerations():
49 if enum.name in mapping:
50 classname = mapping[enum.name]
51 classes[classname] = enum
53 header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w')
54 code = open(os.path.join(dirname, 'enums.cpp'), 'w')
56 for file in (header, code):
57 print >> file, "/* Generated file - edit enums.py instead! */"
59 # Template for beginning of class declaration and public members.
60 header_public_template = """
61 class SR_API {classname} : public EnumValue<enum {enumname}>
64 static const {classname} *get(int id);
67 # Template for beginning of private members.
68 header_private_template = """
70 static const std::map<enum {enumname}, const {classname} *> values;
71 {classname}(enum {enumname} id, const char name[]);
74 # Template for class method definitions.
76 {classname}::{classname}(enum {enumname} id, const char name[]) :
77 EnumValue<enum {enumname}>(id, name)
81 const {classname} *{classname}::get(int id)
83 return {classname}::values.at(static_cast<{enumname}>(id));
87 for classname, enum in classes.items():
89 # Begin class and public declarations
90 print >> header, header_public_template.format(
91 classname=classname, enumname=enum.name)
93 # Declare public pointers for each enum value
94 for name, value in enum.values:
95 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
96 print >> header, '\tstatic const %s * const %s;' % (classname, trimmed_name)
98 # Declare additional methods if present
99 filename = os.path.join(dirname, "%s_methods.hpp" % classname)
100 if os.path.exists(filename):
101 print >> header, str.join('', open(filename).readlines())
103 # Begin private declarations
104 print >> header, header_private_template.format(
105 classname=classname, enumname=enum.name)
107 # Declare private constants for each enum value
108 for name, value in enum.values:
109 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
110 print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name)
112 # End class declaration
113 print >> header, '};'
116 print >> code, code_template.format(
117 classname=classname, enumname=enum.name)
119 # Define private constants for each enum value
120 for name, value in enum.values:
121 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
122 print >> code, 'const %s %s::_%s = %s(%s, "%s");' % (
123 classname, classname, trimmed_name, classname, name, trimmed_name)
125 # Define public pointers for each enum value
126 for name, value in enum.values:
127 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
128 print >> code, 'const %s * const %s::%s = &%s::_%s;' % (
129 classname, classname, trimmed_name, classname, trimmed_name)
131 # Define map of enum values to constants
132 print >> code, 'const std::map<enum %s, const %s *> %s::values = {' % (
133 enum.name, classname, classname)
134 for name, value in enum.values:
135 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
136 print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name)
139 # Define additional methods if present
140 filename = os.path.join(dirname, "%s_methods.cpp" % classname)
141 if os.path.exists(filename):
142 print >> code, str.join('', open(filename).readlines())