]>
Commit | Line | Data |
---|---|---|
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 | ||
d2a929ab | 20 | from __future__ import print_function |
3532ed01 | 21 | from xml.etree import ElementTree |
c23c8659 ML |
22 | from collections import OrderedDict |
23 | import sys, os, re | |
24 | ||
3532ed01 | 25 | index_file = sys.argv[1] |
6884b52b | 26 | |
c23c8659 ML |
27 | # Get directory this script is in. |
28 | dirname = os.path.dirname(os.path.realpath(__file__)) | |
29 | ||
33c84e81 | 30 | outdirname = "bindings/cxx" |
161dc24d UH |
31 | if not os.path.exists(os.path.join(outdirname, 'include/libsigrokcxx')): |
32 | os.makedirs(os.path.join(outdirname, 'include/libsigrokcxx')) | |
33c84e81 | 33 | |
c23c8659 | 34 | mapping = dict([ |
84c87085 ML |
35 | ('sr_loglevel', ('LogLevel', 'Log verbosity level')), |
36 | ('sr_packettype', ('PacketType', 'Type of datafeed packet')), | |
37 | ('sr_mq', ('Quantity', 'Measured quantity')), | |
38 | ('sr_unit', ('Unit', 'Unit of measurement')), | |
39 | ('sr_mqflag', ('QuantityFlag', 'Flag applied to measured quantity')), | |
40 | ('sr_configkey', ('ConfigKey', 'Configuration key')), | |
41 | ('sr_datatype', ('DataType', 'Configuration data type')), | |
42 | ('sr_channeltype', ('ChannelType', 'Channel type')), | |
43 | ('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type'))]) | |
c23c8659 | 44 | |
3532ed01 | 45 | index = ElementTree.parse(index_file) |
c23c8659 ML |
46 | |
47 | # Build mapping between class names and enumerations. | |
3532ed01 ML |
48 | |
49 | classes = OrderedDict() | |
50 | ||
51 | for compound in index.findall('compound'): | |
52 | if compound.attrib['kind'] != 'file': | |
53 | continue | |
54 | filename = os.path.join( | |
55 | os.path.dirname(index_file), | |
56 | '%s.xml' % compound.attrib['refid']) | |
57 | doc = ElementTree.parse(filename) | |
58 | for section in doc.find('compounddef').findall('sectiondef'): | |
59 | if section.attrib["kind"] != 'enum': | |
60 | continue | |
61 | for member in section.findall('memberdef'): | |
62 | if member.attrib["kind"] != 'enum': | |
63 | continue | |
64 | name = member.find('name').text | |
65 | if name in mapping: | |
66 | classes[member] = mapping[name] | |
c23c8659 | 67 | |
161dc24d | 68 | header = open(os.path.join(outdirname, 'include/libsigrokcxx/enums.hpp'), 'w') |
33c84e81 | 69 | code = open(os.path.join(outdirname, 'enums.cpp'), 'w') |
b8721d7c | 70 | swig = open(os.path.join(dirname, '../swig/enums.i'), 'w') |
c23c8659 ML |
71 | |
72 | for file in (header, code): | |
d2a929ab | 73 | print("/* Generated file - edit enums.py instead! */", file=file) |
c23c8659 ML |
74 | |
75 | # Template for beginning of class declaration and public members. | |
76 | header_public_template = """ | |
84c87085 | 77 | /** {brief} */ |
9d229ecb | 78 | class SR_API {classname} : public EnumValue<{classname}, enum {enumname}> |
c23c8659 ML |
79 | {{ |
80 | public: | |
c23c8659 ML |
81 | """ |
82 | ||
83 | # Template for beginning of private members. | |
84 | header_private_template = """ | |
9d229ecb ML |
85 | protected: |
86 | {classname}(enum {enumname} id, const char name[]) : EnumValue(id, name) {{}} | |
c23c8659 ML |
87 | """ |
88 | ||
84c87085 ML |
89 | def get_text(node): |
90 | return str.join('\n\n', | |
91 | [p.text.rstrip() for p in node.findall('para')]) | |
92 | ||
93 | for enum, (classname, classbrief) in classes.items(): | |
3532ed01 ML |
94 | |
95 | enum_name = enum.find('name').text | |
84c87085 ML |
96 | members = enum.findall('enumvalue') |
97 | member_names = [m.find('name').text for m in members] | |
3532ed01 | 98 | trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names] |
84c87085 | 99 | briefs = [get_text(m.find('briefdescription')) for m in members] |
c23c8659 ML |
100 | |
101 | # Begin class and public declarations | |
d2a929ab ML |
102 | print(header_public_template.format( |
103 | brief=classbrief, classname=classname, enumname=enum_name), file=header) | |
c23c8659 ML |
104 | |
105 | # Declare public pointers for each enum value | |
84c87085 ML |
106 | for trimmed_name, brief in zip(trimmed_names, briefs): |
107 | if brief: | |
d2a929ab ML |
108 | print('\t/** %s */' % brief, file=header) |
109 | print('\tstatic const %s * const %s;' % ( | |
110 | classname, trimmed_name), file=header) | |
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): | |
d2a929ab | 115 | print(str.join('', open(filename).readlines()), file=header) |
c23c8659 ML |
116 | |
117 | # Begin private declarations | |
d2a929ab ML |
118 | print(header_private_template.format( |
119 | classname=classname, enumname=enum_name), file=header) | |
c23c8659 ML |
120 | |
121 | # Declare private constants for each enum value | |
3532ed01 | 122 | for trimmed_name in trimmed_names: |
d2a929ab | 123 | print('\tstatic const %s _%s;' % (classname, trimmed_name), file=header) |
c23c8659 ML |
124 | |
125 | # End class declaration | |
d2a929ab | 126 | print('};', file=header) |
c23c8659 | 127 | |
c23c8659 | 128 | # Define private constants for each enum value |
3532ed01 | 129 | for name, trimmed_name in zip(member_names, trimmed_names): |
d2a929ab ML |
130 | print('const %s %s::_%s = %s(%s, "%s");' % ( |
131 | classname, classname, trimmed_name, classname, name, trimmed_name), | |
132 | file=code) | |
c23c8659 ML |
133 | |
134 | # Define public pointers for each enum value | |
3532ed01 | 135 | for trimmed_name in trimmed_names: |
d2a929ab ML |
136 | print('const %s * const %s::%s = &%s::_%s;' % ( |
137 | classname, classname, trimmed_name, classname, trimmed_name), | |
138 | file=code) | |
c23c8659 ML |
139 | |
140 | # Define map of enum values to constants | |
dc7125bb | 141 | print('template<> const SR_API std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % ( |
d2a929ab | 142 | enum_name, classname, classname, enum_name), file=code) |
3532ed01 | 143 | for name, trimmed_name in zip(member_names, trimmed_names): |
d2a929ab ML |
144 | print('\t{%s, %s::%s},' % (name, classname, trimmed_name), file=code) |
145 | print('};', file=code) | |
c23c8659 ML |
146 | |
147 | # Define additional methods if present | |
148 | filename = os.path.join(dirname, "%s_methods.cpp" % classname) | |
149 | if os.path.exists(filename): | |
d2a929ab | 150 | print(str.join('', open(filename).readlines()), file=code) |
fe4096fd | 151 | |
189461b2 | 152 | # Map EnumValue::id() and EnumValue::name() as SWIG attributes. |
d2a929ab ML |
153 | print('%%attribute(sigrok::%s, int, id, id);' % classname, file=swig) |
154 | print('%%attributestring(sigrok::%s, std::string, name, name);' % classname, | |
155 | file=swig) | |
189461b2 ML |
156 | |
157 | # Instantiate EnumValue template for SWIG | |
d2a929ab ML |
158 | print('%%template(EnumValue%s) sigrok::EnumValue<sigrok::%s, enum %s>;' % ( |
159 | classname, classname, enum_name), file=swig) | |
189461b2 | 160 | |
7a36ceac | 161 | # Apply any language-specific extras. |
d2a929ab | 162 | print('%%enumextras(%s);' % classname, file=swig) |
7a36ceac | 163 | |
e480df0c ML |
164 | # Declare additional attributes if present |
165 | filename = os.path.join(dirname, "%s_methods.i" % classname) | |
166 | if os.path.exists(filename): | |
d2a929ab | 167 | print(str.join('', open(filename).readlines()), file=swig) |