]>
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 | ||
20 | from pygccxml import parser, declarations | |
21 | from collections import OrderedDict | |
22 | import sys, os, re | |
23 | ||
24 | # Get directory this script is in. | |
25 | dirname = os.path.dirname(os.path.realpath(__file__)) | |
26 | ||
27 | # Parse GCCXML output to get declaration tree. | |
28 | decls = parser.parse_xml_file( | |
29 | os.path.join(dirname, "libsigrok.xml"), parser.config_t()) | |
30 | ||
31 | # Get global namespace from declaration tree. | |
32 | ns = declarations.get_global_namespace(decls) | |
33 | ||
34 | mapping = dict([ | |
35 | ('sr_loglevel', 'LogLevel'), | |
36 | ('sr_packettype', 'PacketType'), | |
37 | ('sr_mq', 'Quantity'), | |
38 | ('sr_unit', 'Unit'), | |
39 | ('sr_mqflag', 'QuantityFlag'), | |
40 | ('sr_configkey', 'ConfigKey'), | |
41 | ('sr_datatype', 'DataType'), | |
42 | ('sr_channeltype', 'ChannelType'), | |
43 | ('sr_trigger_matches', 'TriggerMatchType')]) | |
44 | ||
45 | classes = OrderedDict() | |
46 | ||
47 | # Build mapping between class names and enumerations. | |
48 | for enum in ns.enumerations(): | |
49 | if enum.name in mapping: | |
50 | classname = mapping[enum.name] | |
51 | classes[classname] = enum | |
52 | ||
53 | header = open(os.path.join(dirname, 'include/libsigrok/enums.hpp'), 'w') | |
54 | code = open(os.path.join(dirname, 'enums.cpp'), 'w') | |
55 | ||
56 | for file in (header, code): | |
57 | print >> file, "/* Generated file - edit enums.py instead! */" | |
58 | ||
59 | # Template for beginning of class declaration and public members. | |
60 | header_public_template = """ | |
61 | class SR_API {classname} : public EnumValue<enum {enumname}> | |
62 | {{ | |
63 | public: | |
64 | static const {classname} *get(int id); | |
65 | """ | |
66 | ||
67 | # Template for beginning of private members. | |
68 | header_private_template = """ | |
69 | private: | |
70 | static const std::map<enum {enumname}, const {classname} *> values; | |
71 | {classname}(enum {enumname} id, const char name[]); | |
72 | """ | |
73 | ||
74 | # Template for class method definitions. | |
75 | code_template = """ | |
76 | {classname}::{classname}(enum {enumname} id, const char name[]) : | |
77 | EnumValue<enum {enumname}>(id, name) | |
78 | {{ | |
79 | }} | |
80 | ||
81 | const {classname} *{classname}::get(int id) | |
82 | {{ | |
83 | return {classname}::values.at(static_cast<{enumname}>(id)); | |
84 | }} | |
85 | """ | |
86 | ||
87 | for classname, enum in classes.items(): | |
88 | ||
89 | # Begin class and public declarations | |
90 | print >> header, header_public_template.format( | |
91 | classname=classname, enumname=enum.name) | |
92 | ||
93 | # Declare public pointers for each enum value | |
94 | for name, value in enum.values: | |
95 | trimmed_name = re.sub("^SR_[A-Z]+_", "", name) | |
96 | print >> header, '\tstatic const %s * const %s;' % (classname, trimmed_name) | |
97 | ||
98 | # Declare additional methods if present | |
99 | filename = os.path.join(dirname, "%s_methods.hpp" % classname) | |
100 | if os.path.exists(filename): | |
101 | print >> header, str.join('', open(filename).readlines()) | |
102 | ||
103 | # Begin private declarations | |
104 | print >> header, header_private_template.format( | |
105 | classname=classname, enumname=enum.name) | |
106 | ||
107 | # Declare private constants for each enum value | |
108 | for name, value in enum.values: | |
109 | trimmed_name = re.sub("^SR_[A-Z]+_", "", name) | |
110 | print >> header, '\tstatic const %s _%s;' % (classname, trimmed_name) | |
111 | ||
112 | # End class declaration | |
113 | print >> header, '};' | |
114 | ||
115 | # Begin class code | |
116 | print >> code, code_template.format( | |
117 | classname=classname, enumname=enum.name) | |
118 | ||
119 | # Define private constants for each enum value | |
120 | for name, value in enum.values: | |
121 | trimmed_name = re.sub("^SR_[A-Z]+_", "", name) | |
122 | print >> code, 'const %s %s::_%s = %s(%s, "%s");' % ( | |
123 | classname, classname, trimmed_name, classname, name, trimmed_name) | |
124 | ||
125 | # Define public pointers for each enum value | |
126 | for name, value in enum.values: | |
127 | trimmed_name = re.sub("^SR_[A-Z]+_", "", name) | |
128 | print >> code, 'const %s * const %s::%s = &%s::_%s;' % ( | |
129 | classname, classname, trimmed_name, classname, trimmed_name) | |
130 | ||
131 | # Define map of enum values to constants | |
132 | print >> code, 'const std::map<enum %s, const %s *> %s::values = {' % ( | |
133 | enum.name, classname, classname) | |
134 | for name, value in enum.values: | |
135 | trimmed_name = re.sub("^SR_[A-Z]+_", "", name) | |
136 | print >> code, '\t{%s, %s::%s},' % (name, classname, trimmed_name) | |
137 | print >> code, '};' | |
138 | ||
139 | # Define additional methods if present | |
140 | filename = os.path.join(dirname, "%s_methods.cpp" % classname) | |
141 | if os.path.exists(filename): | |
142 | print >> code, str.join('', open(filename).readlines()) |