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 import logging, warnings
21 from pygccxml import parser, declarations
22 from collections import OrderedDict
25 class crapfilter(logging.Filter):
26 def filter(self, record):
27 if record.msg.find('GCCXML version') > -1:
30 logger = logging.getLogger('pygccxml.cxx_parser').addFilter(crapfilter())
31 warnings.filterwarnings('ignore', message="unable to find out array size from expression")
33 # Get directory this script is in.
34 dirname = os.path.dirname(os.path.realpath(__file__))
36 # Parse GCCXML output to get declaration tree.
37 decls = parser.parse_xml_file(
38 os.path.join(dirname, "libsigrok.xml"), parser.gccxml_configuration_t())
40 # Get global namespace from declaration tree.
41 ns = declarations.get_global_namespace(decls)
44 ('sr_loglevel', 'LogLevel'),
45 ('sr_packettype', 'PacketType'),
46 ('sr_mq', 'Quantity'),
48 ('sr_mqflag', 'QuantityFlag'),
49 ('sr_configkey', 'ConfigKey'),
50 ('sr_datatype', 'DataType'),
51 ('sr_channeltype', 'ChannelType'),
52 ('sr_trigger_matches', 'TriggerMatchType')])
54 classes = OrderedDict()
56 # Build mapping between class names and enumerations.
57 for enum in ns.enumerations():
58 if enum.name in mapping:
59 classname = mapping[enum.name]
60 classes[classname] = enum
62 header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w')
63 code = open(os.path.join(dirname, 'enums.cpp'), 'w')
65 for file in (header, code):
66 print >> file, "/* Generated file - edit enums.py instead! */"
68 # Template for beginning of class declaration and public members.
69 header_public_template = """
70 class SR_API {classname} : public EnumValue<enum {enumname}>
73 static const {classname} *get(int id);
76 # Template for beginning of private members.
77 header_private_template = """
79 static const std::map<enum {enumname}, const {classname} *> values;
80 {classname}(enum {enumname} id, const char name[]);
83 # Template for class method definitions.
85 {classname}::{classname}(enum {enumname} id, const char name[]) :
86 EnumValue<enum {enumname}>(id, name)
90 const {classname} *{classname}::get(int id)
92 return {classname}::values.at(static_cast<{enumname}>(id));
96 for classname, enum in classes.items():
98 # Begin class and public declarations
99 print >> header, header_public_template.format(
100 classname=classname, enumname=enum.name)
102 # Declare public pointers for each enum value
103 for name, value in enum.values:
104 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
105 print >> header, '\tstatic const %s * const %s;' % (classname, trimmed_name)
107 # Declare additional methods if present
108 filename = os.path.join(dirname, "%s_methods.hpp" % classname)
109 if os.path.exists(filename):
110 print >> header, str.join('', open(filename).readlines())
112 # Begin private declarations
113 print >> header, header_private_template.format(
114 classname=classname, enumname=enum.name)
116 # Declare private constants for each enum value
117 for name, value in enum.values:
118 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
119 print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name)
121 # End class declaration
122 print >> header, '};'
125 print >> code, code_template.format(
126 classname=classname, enumname=enum.name)
128 # Define private constants for each enum value
129 for name, value in enum.values:
130 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
131 print >> code, 'const %s %s::_%s = %s(%s, "%s");' % (
132 classname, classname, trimmed_name, classname, name, trimmed_name)
134 # Define public pointers for each enum value
135 for name, value in enum.values:
136 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
137 print >> code, 'const %s * const %s::%s = &%s::_%s;' % (
138 classname, classname, trimmed_name, classname, trimmed_name)
140 # Define map of enum values to constants
141 print >> code, 'const std::map<enum %s, const %s *> %s::values = {' % (
142 enum.name, classname, classname)
143 for name, value in enum.values:
144 trimmed_name = re.sub("^SR_[A-Z]+_", "", name)
145 print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name)
148 # Define additional methods if present
149 filename = os.path.join(dirname, "%s_methods.cpp" % classname)
150 if os.path.exists(filename):
151 print >> code, str.join('', open(filename).readlines())