]> sigrok.org Git - libsigrok.git/blob - bindings/cxx/enums.py
C++ bindings: Reimplement enums.py using doxygen XML output instead of gccxml.
[libsigrok.git] / bindings / cxx / enums.py
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
20 from xml.etree import ElementTree
21 from collections import OrderedDict
22 import sys, os, re
23
24 index_file = sys.argv[1]
25
26 # Get directory this script is in.
27 dirname = os.path.dirname(os.path.realpath(__file__))
28
29 mapping = 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
40 index = ElementTree.parse(index_file)
41
42 # Build mapping between class names and enumerations.
43
44 classes = OrderedDict()
45
46 for 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]
62
63 header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w')
64 code = open(os.path.join(dirname, 'enums.cpp'), 'w')
65
66 for file in (header, code):
67     print >> file, "/* Generated file - edit enums.py instead! */"
68
69 # Template for beginning of class declaration and public members.
70 header_public_template = """
71 class SR_API {classname} : public EnumValue<enum {enumname}>
72 {{
73 public:
74     static const {classname} *get(int id);
75 """
76
77 # Template for beginning of private members.
78 header_private_template = """
79 private:
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.
85 code_template = """
86 {classname}::{classname}(enum {enumname} id, const char name[]) :
87     EnumValue<enum {enumname}>(id, name)
88 {{
89 }}
90
91 const {classname} *{classname}::get(int id)
92 {{
93     return {classname}::values.at(static_cast<{enumname}>(id));
94 }}
95 """
96
97 for 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]
102
103     # Begin class and public declarations
104     print >> header, header_public_template.format(
105         classname=classname, enumname=enum_name)
106
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)
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(
119         classname=classname, enumname=enum_name)
120
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)
124
125     # End class declaration
126     print >> header, '};'
127
128     # Begin class code
129     print >> code, code_template.format(
130         classname=classname, enumname=enum_name)
131
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)
136
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)
141
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)
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())