]>
Commit | Line | Data |
---|---|---|
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 | outdirname = "bindings/cxx" | |
30 | if not os.path.exists(os.path.join(outdirname, 'include/libsigrok')): | |
31 | os.makedirs(os.path.join(outdirname, 'include/libsigrok')) | |
32 | ||
33 | mapping = dict([ | |
34 | ('sr_loglevel', ('LogLevel', 'Log verbosity level')), | |
35 | ('sr_packettype', ('PacketType', 'Type of datafeed packet')), | |
36 | ('sr_mq', ('Quantity', 'Measured quantity')), | |
37 | ('sr_unit', ('Unit', 'Unit of measurement')), | |
38 | ('sr_mqflag', ('QuantityFlag', 'Flag applied to measured quantity')), | |
39 | ('sr_configkey', ('ConfigKey', 'Configuration key')), | |
40 | ('sr_datatype', ('DataType', 'Configuration data type')), | |
41 | ('sr_channeltype', ('ChannelType', 'Channel type')), | |
42 | ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type'))]) | |
43 | ||
44 | index = ElementTree.parse(index_file) | |
45 | ||
46 | # Build mapping between class names and enumerations. | |
47 | ||
48 | classes = OrderedDict() | |
49 | ||
50 | for compound in index.findall('compound'): | |
51 | if compound.attrib['kind'] != 'file': | |
52 | continue | |
53 | filename = os.path.join( | |
54 | os.path.dirname(index_file), | |
55 | '%s.xml' % compound.attrib['refid']) | |
56 | doc = ElementTree.parse(filename) | |
57 | for section in doc.find('compounddef').findall('sectiondef'): | |
58 | if section.attrib["kind"] != 'enum': | |
59 | continue | |
60 | for member in section.findall('memberdef'): | |
61 | if member.attrib["kind"] != 'enum': | |
62 | continue | |
63 | name = member.find('name').text | |
64 | if name in mapping: | |
65 | classes[member] = mapping[name] | |
66 | ||
67 | header = open(os.path.join(outdirname, 'include/libsigrok/enums.hpp'), 'w') | |
68 | code = open(os.path.join(outdirname, 'enums.cpp'), 'w') | |
69 | ||
70 | for file in (header, code): | |
71 | print >> file, "/* Generated file - edit enums.py instead! */" | |
72 | ||
73 | # Template for beginning of class declaration and public members. | |
74 | header_public_template = """ | |
75 | /** {brief} */ | |
76 | class SR_API {classname} : public EnumValue<enum {enumname}> | |
77 | {{ | |
78 | public: | |
79 | static const {classname} *get(int id); | |
80 | """ | |
81 | ||
82 | # Template for beginning of private members. | |
83 | header_private_template = """ | |
84 | private: | |
85 | static const std::map<enum {enumname}, const {classname} *> _values; | |
86 | {classname}(enum {enumname} id, const char name[]); | |
87 | """ | |
88 | ||
89 | # Template for class method definitions. | |
90 | code_template = """ | |
91 | {classname}::{classname}(enum {enumname} id, const char name[]) : | |
92 | EnumValue<enum {enumname}>(id, name) | |
93 | {{ | |
94 | }} | |
95 | ||
96 | const {classname} *{classname}::get(int id) | |
97 | {{ | |
98 | if (_values.find(static_cast<{enumname}>(id)) == _values.end()) | |
99 | throw Error(SR_ERR_ARG); | |
100 | ||
101 | return {classname}::_values.at(static_cast<{enumname}>(id)); | |
102 | }} | |
103 | """ | |
104 | ||
105 | def get_text(node): | |
106 | return str.join('\n\n', | |
107 | [p.text.rstrip() for p in node.findall('para')]) | |
108 | ||
109 | for enum, (classname, classbrief) in classes.items(): | |
110 | ||
111 | enum_name = enum.find('name').text | |
112 | members = enum.findall('enumvalue') | |
113 | member_names = [m.find('name').text for m in members] | |
114 | trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names] | |
115 | briefs = [get_text(m.find('briefdescription')) for m in members] | |
116 | ||
117 | # Begin class and public declarations | |
118 | print >> header, header_public_template.format( | |
119 | brief=classbrief, classname=classname, enumname=enum_name) | |
120 | ||
121 | # Declare public pointers for each enum value | |
122 | for trimmed_name, brief in zip(trimmed_names, briefs): | |
123 | if brief: | |
124 | print >> header, '\t/** %s */' % brief | |
125 | print >> header, '\tstatic const %s * const %s;' % ( | |
126 | classname, trimmed_name) | |
127 | ||
128 | # Declare additional methods if present | |
129 | filename = os.path.join(dirname, "%s_methods.hpp" % classname) | |
130 | if os.path.exists(filename): | |
131 | print >> header, str.join('', open(filename).readlines()) | |
132 | ||
133 | # Begin private declarations | |
134 | print >> header, header_private_template.format( | |
135 | classname=classname, enumname=enum_name) | |
136 | ||
137 | # Declare private constants for each enum value | |
138 | for trimmed_name in trimmed_names: | |
139 | print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name) | |
140 | ||
141 | # End class declaration | |
142 | print >> header, '};' | |
143 | ||
144 | # Begin class code | |
145 | print >> code, code_template.format( | |
146 | classname=classname, enumname=enum_name) | |
147 | ||
148 | # Define private constants for each enum value | |
149 | for name, trimmed_name in zip(member_names, trimmed_names): | |
150 | print >> code, 'const %s %s::_%s = %s(%s, "%s");' % ( | |
151 | classname, classname, trimmed_name, classname, name, trimmed_name) | |
152 | ||
153 | # Define public pointers for each enum value | |
154 | for trimmed_name in trimmed_names: | |
155 | print >> code, 'const %s * const %s::%s = &%s::_%s;' % ( | |
156 | classname, classname, trimmed_name, classname, trimmed_name) | |
157 | ||
158 | # Define map of enum values to constants | |
159 | print >> code, 'const std::map<enum %s, const %s *> %s::_values = {' % ( | |
160 | enum_name, classname, classname) | |
161 | for name, trimmed_name in zip(member_names, trimmed_names): | |
162 | print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name) | |
163 | print >> code, '};' | |
164 | ||
165 | # Define additional methods if present | |
166 | filename = os.path.join(dirname, "%s_methods.cpp" % classname) | |
167 | if os.path.exists(filename): | |
168 | print >> code, str.join('', open(filename).readlines()) |