]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/enums.py
Fix typo in the Hameg HMO driver and add some error message for when the float compar...
[libsigrok.git] / bindings / cxx / enums.py
CommitLineData
c23c8659
ML
1##
2## This file is part of the libsigrok project.
3##
4## Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
5##
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.
10##
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.
15##
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/>.
18##
19
6884b52b 20import logging, warnings
c23c8659
ML
21from pygccxml import parser, declarations
22from collections import OrderedDict
23import sys, os, re
24
6884b52b
BV
25class crapfilter(logging.Filter):
26 def filter(self, record):
27 if record.msg.find('GCCXML version') > -1:
28 return 0
29 return 1
30logger = logging.getLogger('pygccxml.cxx_parser').addFilter(crapfilter())
31warnings.filterwarnings('ignore', message="unable to find out array size from expression")
32
c23c8659
ML
33# Get directory this script is in.
34dirname = os.path.dirname(os.path.realpath(__file__))
35
36# Parse GCCXML output to get declaration tree.
37decls = parser.parse_xml_file(
c6036f75 38 os.path.join(dirname, "libsigrok.xml"), parser.gccxml_configuration_t())
c23c8659
ML
39
40# Get global namespace from declaration tree.
41ns = declarations.get_global_namespace(decls)
42
43mapping = dict([
44 ('sr_loglevel', 'LogLevel'),
45 ('sr_packettype', 'PacketType'),
46 ('sr_mq', 'Quantity'),
47 ('sr_unit', 'Unit'),
48 ('sr_mqflag', 'QuantityFlag'),
49 ('sr_configkey', 'ConfigKey'),
50 ('sr_datatype', 'DataType'),
51 ('sr_channeltype', 'ChannelType'),
52 ('sr_trigger_matches', 'TriggerMatchType')])
53
54classes = OrderedDict()
55
56# Build mapping between class names and enumerations.
57for enum in ns.enumerations():
58 if enum.name in mapping:
59 classname = mapping[enum.name]
60 classes[classname] = enum
61
62header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w')
63code = open(os.path.join(dirname, 'enums.cpp'), 'w')
64
65for file in (header, code):
66 print >> file, "/* Generated file - edit enums.py instead! */"
67
68# Template for beginning of class declaration and public members.
69header_public_template = """
70class SR_API {classname} : public EnumValue<enum {enumname}>
71{{
72public:
73 static const {classname} *get(int id);
74"""
75
76# Template for beginning of private members.
77header_private_template = """
78private:
79 static const std::map<enum {enumname}, const {classname} *> values;
80 {classname}(enum {enumname} id, const char name[]);
81"""
82
83# Template for class method definitions.
84code_template = """
85{classname}::{classname}(enum {enumname} id, const char name[]) :
86 EnumValue<enum {enumname}>(id, name)
87{{
88}}
89
90const {classname} *{classname}::get(int id)
91{{
92 return {classname}::values.at(static_cast<{enumname}>(id));
93}}
94"""
95
96for classname, enum in classes.items():
97
98 # Begin class and public declarations
99 print >> header, header_public_template.format(
100 classname=classname, enumname=enum.name)
101
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)
106
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())
111
112 # Begin private declarations
113 print >> header, header_private_template.format(
114 classname=classname, enumname=enum.name)
115
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)
120
121 # End class declaration
122 print >> header, '};'
123
124 # Begin class code
125 print >> code, code_template.format(
126 classname=classname, enumname=enum.name)
127
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)
133
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)
139
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)
146 print >> code, '};'
147
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())