]>
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 | ||
3532ed01 | 20 | from xml.etree import ElementTree |
c23c8659 ML |
21 | from collections import OrderedDict |
22 | import sys, os, re | |
23 | ||
3532ed01 | 24 | index_file = sys.argv[1] |
6884b52b | 25 | |
c23c8659 ML |
26 | # Get directory this script is in. |
27 | dirname = os.path.dirname(os.path.realpath(__file__)) | |
28 | ||
33c84e81 MC |
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 | ||
c23c8659 | 33 | mapping = dict([ |
84c87085 ML |
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'))]) | |
c23c8659 | 43 | |
3532ed01 | 44 | index = ElementTree.parse(index_file) |
c23c8659 ML |
45 | |
46 | # Build mapping between class names and enumerations. | |
3532ed01 ML |
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] | |
c23c8659 | 66 | |
33c84e81 MC |
67 | header = open(os.path.join(outdirname, 'include/libsigrok/enums.hpp'), 'w') |
68 | code = open(os.path.join(outdirname, 'enums.cpp'), 'w') | |
fe4096fd | 69 | swig = open(os.path.join(outdirname, '../swig/enums.i'), 'w') |
c23c8659 ML |
70 | |
71 | for file in (header, code): | |
72 | print >> file, "/* Generated file - edit enums.py instead! */" | |
73 | ||
74 | # Template for beginning of class declaration and public members. | |
75 | header_public_template = """ | |
84c87085 | 76 | /** {brief} */ |
9d229ecb | 77 | class SR_API {classname} : public EnumValue<{classname}, enum {enumname}> |
c23c8659 ML |
78 | {{ |
79 | public: | |
c23c8659 ML |
80 | """ |
81 | ||
82 | # Template for beginning of private members. | |
83 | header_private_template = """ | |
9d229ecb ML |
84 | protected: |
85 | {classname}(enum {enumname} id, const char name[]) : EnumValue(id, name) {{}} | |
c23c8659 ML |
86 | """ |
87 | ||
84c87085 ML |
88 | def get_text(node): |
89 | return str.join('\n\n', | |
90 | [p.text.rstrip() for p in node.findall('para')]) | |
91 | ||
92 | for enum, (classname, classbrief) in classes.items(): | |
3532ed01 ML |
93 | |
94 | enum_name = enum.find('name').text | |
84c87085 ML |
95 | members = enum.findall('enumvalue') |
96 | member_names = [m.find('name').text for m in members] | |
3532ed01 | 97 | trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names] |
84c87085 | 98 | briefs = [get_text(m.find('briefdescription')) for m in members] |
c23c8659 ML |
99 | |
100 | # Begin class and public declarations | |
101 | print >> header, header_public_template.format( | |
84c87085 | 102 | brief=classbrief, classname=classname, enumname=enum_name) |
c23c8659 ML |
103 | |
104 | # Declare public pointers for each enum value | |
84c87085 ML |
105 | for trimmed_name, brief in zip(trimmed_names, briefs): |
106 | if brief: | |
107 | print >> header, '\t/** %s */' % brief | |
3532ed01 ML |
108 | print >> header, '\tstatic const %s * const %s;' % ( |
109 | classname, trimmed_name) | |
c23c8659 ML |
110 | |
111 | # Declare additional methods if present | |
112 | filename = os.path.join(dirname, "%s_methods.hpp" % classname) | |
113 | if os.path.exists(filename): | |
114 | print >> header, str.join('', open(filename).readlines()) | |
115 | ||
116 | # Begin private declarations | |
117 | print >> header, header_private_template.format( | |
3532ed01 | 118 | classname=classname, enumname=enum_name) |
c23c8659 ML |
119 | |
120 | # Declare private constants for each enum value | |
3532ed01 | 121 | for trimmed_name in trimmed_names: |
c23c8659 ML |
122 | print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name) |
123 | ||
124 | # End class declaration | |
125 | print >> header, '};' | |
126 | ||
c23c8659 | 127 | # Define private constants for each enum value |
3532ed01 | 128 | for name, trimmed_name in zip(member_names, trimmed_names): |
c23c8659 ML |
129 | print >> code, 'const %s %s::_%s = %s(%s, "%s");' % ( |
130 | classname, classname, trimmed_name, classname, name, trimmed_name) | |
131 | ||
132 | # Define public pointers for each enum value | |
3532ed01 | 133 | for trimmed_name in trimmed_names: |
c23c8659 ML |
134 | print >> code, 'const %s * const %s::%s = &%s::_%s;' % ( |
135 | classname, classname, trimmed_name, classname, trimmed_name) | |
136 | ||
137 | # Define map of enum values to constants | |
9d229ecb ML |
138 | print >> code, 'template<> const std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % ( |
139 | enum_name, classname, classname, enum_name) | |
3532ed01 | 140 | for name, trimmed_name in zip(member_names, trimmed_names): |
c23c8659 ML |
141 | print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name) |
142 | print >> code, '};' | |
143 | ||
144 | # Define additional methods if present | |
145 | filename = os.path.join(dirname, "%s_methods.cpp" % classname) | |
146 | if os.path.exists(filename): | |
147 | print >> code, str.join('', open(filename).readlines()) | |
fe4096fd ML |
148 | |
149 | # Instantiate EnumValue template for SWIG wrappers | |
150 | print >> swig, '%%template(EnumValue%s) EnumValue<%s, enum %s>;' % ( | |
151 | classname, classname, enum_name) |