]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/enums.py
C++ bindings: Reimplement enums.py using doxygen XML output instead of gccxml.
[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
3532ed01 20from xml.etree import ElementTree
c23c8659
ML
21from collections import OrderedDict
22import sys, os, re
23
3532ed01 24index_file = sys.argv[1]
6884b52b 25
c23c8659
ML
26# Get directory this script is in.
27dirname = os.path.dirname(os.path.realpath(__file__))
28
c23c8659
ML
29mapping = dict([
30 ('sr_loglevel', 'LogLevel'),
31 ('sr_packettype', 'PacketType'),
32 ('sr_mq', 'Quantity'),
33 ('sr_unit', 'Unit'),
34 ('sr_mqflag', 'QuantityFlag'),
35 ('sr_configkey', 'ConfigKey'),
36 ('sr_datatype', 'DataType'),
37 ('sr_channeltype', 'ChannelType'),
38 ('sr_trigger_matches', 'TriggerMatchType')])
39
3532ed01 40index = ElementTree.parse(index_file)
c23c8659
ML
41
42# Build mapping between class names and enumerations.
3532ed01
ML
43
44classes = OrderedDict()
45
46for compound in index.findall('compound'):
47 if compound.attrib['kind'] != 'file':
48 continue
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':
55 continue
56 for member in section.findall('memberdef'):
57 if member.attrib["kind"] != 'enum':
58 continue
59 name = member.find('name').text
60 if name in mapping:
61 classes[member] = mapping[name]
c23c8659
ML
62
63header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w')
64code = open(os.path.join(dirname, 'enums.cpp'), 'w')
65
66for file in (header, code):
67 print >> file, "/* Generated file - edit enums.py instead! */"
68
69# Template for beginning of class declaration and public members.
70header_public_template = """
71class SR_API {classname} : public EnumValue<enum {enumname}>
72{{
73public:
74 static const {classname} *get(int id);
75"""
76
77# Template for beginning of private members.
78header_private_template = """
79private:
80 static const std::map<enum {enumname}, const {classname} *> values;
81 {classname}(enum {enumname} id, const char name[]);
82"""
83
84# Template for class method definitions.
85code_template = """
86{classname}::{classname}(enum {enumname} id, const char name[]) :
87 EnumValue<enum {enumname}>(id, name)
88{{
89}}
90
91const {classname} *{classname}::get(int id)
92{{
93 return {classname}::values.at(static_cast<{enumname}>(id));
94}}
95"""
96
3532ed01
ML
97for enum, classname in classes.items():
98
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]
c23c8659
ML
102
103 # Begin class and public declarations
104 print >> header, header_public_template.format(
3532ed01 105 classname=classname, enumname=enum_name)
c23c8659
ML
106
107 # Declare public pointers for each enum value
3532ed01
ML
108 for trimmed_name in trimmed_names:
109 print >> header, '\tstatic const %s * const %s;' % (
110 classname, trimmed_name)
c23c8659
ML
111
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())
116
117 # Begin private declarations
118 print >> header, header_private_template.format(
3532ed01 119 classname=classname, enumname=enum_name)
c23c8659
ML
120
121 # Declare private constants for each enum value
3532ed01 122 for trimmed_name in trimmed_names:
c23c8659
ML
123 print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name)
124
125 # End class declaration
126 print >> header, '};'
127
128 # Begin class code
129 print >> code, code_template.format(
3532ed01 130 classname=classname, enumname=enum_name)
c23c8659
ML
131
132 # Define private constants for each enum value
3532ed01 133 for name, trimmed_name in zip(member_names, trimmed_names):
c23c8659
ML
134 print >> code, 'const %s %s::_%s = %s(%s, "%s");' % (
135 classname, classname, trimmed_name, classname, name, trimmed_name)
136
137 # Define public pointers for each enum value
3532ed01 138 for trimmed_name in trimmed_names:
c23c8659
ML
139 print >> code, 'const %s * const %s::%s = &%s::_%s;' % (
140 classname, classname, trimmed_name, classname, trimmed_name)
141
142 # Define map of enum values to constants
143 print >> code, 'const std::map<enum %s, const %s *> %s::values = {' % (
3532ed01
ML
144 enum_name, classname, classname)
145 for name, trimmed_name in zip(member_names, trimmed_names):
c23c8659
ML
146 print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name)
147 print >> code, '};'
148
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())