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