2014-07-17 17:39:49 +00:00
|
|
|
##
|
|
|
|
## This file is part of the libsigrok project.
|
|
|
|
##
|
|
|
|
## Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
|
|
|
|
##
|
|
|
|
## This program is free software: you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published by
|
|
|
|
## the Free Software Foundation, either version 3 of the License, or
|
|
|
|
## (at your option) any later version.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
2014-11-13 20:38:56 +00:00
|
|
|
from __future__ import print_function
|
2014-08-01 16:18:09 +00:00
|
|
|
from xml.etree import ElementTree
|
2014-07-17 17:39:49 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
import sys, os, re
|
|
|
|
|
2014-08-01 16:18:09 +00:00
|
|
|
index_file = sys.argv[1]
|
2014-07-19 17:48:01 +00:00
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
# Get directory this script is in.
|
|
|
|
dirname = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
2014-08-14 21:07:45 +00:00
|
|
|
outdirname = "bindings/cxx"
|
2015-01-17 19:11:53 +00:00
|
|
|
if not os.path.exists(os.path.join(outdirname, 'include/libsigrokcxx')):
|
|
|
|
os.makedirs(os.path.join(outdirname, 'include/libsigrokcxx'))
|
2014-08-14 21:07:45 +00:00
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
mapping = dict([
|
2014-08-01 16:40:56 +00:00
|
|
|
('sr_loglevel', ('LogLevel', 'Log verbosity level')),
|
|
|
|
('sr_packettype', ('PacketType', 'Type of datafeed packet')),
|
|
|
|
('sr_mq', ('Quantity', 'Measured quantity')),
|
|
|
|
('sr_unit', ('Unit', 'Unit of measurement')),
|
|
|
|
('sr_mqflag', ('QuantityFlag', 'Flag applied to measured quantity')),
|
|
|
|
('sr_configkey', ('ConfigKey', 'Configuration key')),
|
|
|
|
('sr_datatype', ('DataType', 'Configuration data type')),
|
|
|
|
('sr_channeltype', ('ChannelType', 'Channel type')),
|
|
|
|
('sr_trigger_matches', ('TriggerMatchType', 'Trigger match type'))])
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2014-08-01 16:18:09 +00:00
|
|
|
index = ElementTree.parse(index_file)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Build mapping between class names and enumerations.
|
2014-08-01 16:18:09 +00:00
|
|
|
|
|
|
|
classes = OrderedDict()
|
|
|
|
|
|
|
|
for compound in index.findall('compound'):
|
|
|
|
if compound.attrib['kind'] != 'file':
|
|
|
|
continue
|
|
|
|
filename = os.path.join(
|
|
|
|
os.path.dirname(index_file),
|
|
|
|
'%s.xml' % compound.attrib['refid'])
|
|
|
|
doc = ElementTree.parse(filename)
|
|
|
|
for section in doc.find('compounddef').findall('sectiondef'):
|
|
|
|
if section.attrib["kind"] != 'enum':
|
|
|
|
continue
|
|
|
|
for member in section.findall('memberdef'):
|
|
|
|
if member.attrib["kind"] != 'enum':
|
|
|
|
continue
|
|
|
|
name = member.find('name').text
|
|
|
|
if name in mapping:
|
|
|
|
classes[member] = mapping[name]
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2015-01-17 19:11:53 +00:00
|
|
|
header = open(os.path.join(outdirname, 'include/libsigrokcxx/enums.hpp'), 'w')
|
2014-08-14 21:07:45 +00:00
|
|
|
code = open(os.path.join(outdirname, 'enums.cpp'), 'w')
|
2014-11-14 19:01:12 +00:00
|
|
|
swig = open(os.path.join(dirname, '../swig/enums.i'), 'w')
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
for file in (header, code):
|
2014-11-13 20:38:56 +00:00
|
|
|
print("/* Generated file - edit enums.py instead! */", file=file)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Template for beginning of class declaration and public members.
|
|
|
|
header_public_template = """
|
2014-08-01 16:40:56 +00:00
|
|
|
/** {brief} */
|
2014-10-29 14:31:31 +00:00
|
|
|
class SR_API {classname} : public EnumValue<{classname}, enum {enumname}>
|
2014-07-17 17:39:49 +00:00
|
|
|
{{
|
|
|
|
public:
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Template for beginning of private members.
|
|
|
|
header_private_template = """
|
2014-10-29 14:31:31 +00:00
|
|
|
protected:
|
|
|
|
{classname}(enum {enumname} id, const char name[]) : EnumValue(id, name) {{}}
|
2014-07-17 17:39:49 +00:00
|
|
|
"""
|
|
|
|
|
2014-08-01 16:40:56 +00:00
|
|
|
def get_text(node):
|
|
|
|
return str.join('\n\n',
|
|
|
|
[p.text.rstrip() for p in node.findall('para')])
|
|
|
|
|
|
|
|
for enum, (classname, classbrief) in classes.items():
|
2014-08-01 16:18:09 +00:00
|
|
|
|
|
|
|
enum_name = enum.find('name').text
|
2014-08-01 16:40:56 +00:00
|
|
|
members = enum.findall('enumvalue')
|
|
|
|
member_names = [m.find('name').text for m in members]
|
2014-08-01 16:18:09 +00:00
|
|
|
trimmed_names = [re.sub("^SR_[A-Z]+_", "", n) for n in member_names]
|
2014-08-01 16:40:56 +00:00
|
|
|
briefs = [get_text(m.find('briefdescription')) for m in members]
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Begin class and public declarations
|
2014-11-13 20:38:56 +00:00
|
|
|
print(header_public_template.format(
|
|
|
|
brief=classbrief, classname=classname, enumname=enum_name), file=header)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Declare public pointers for each enum value
|
2014-08-01 16:40:56 +00:00
|
|
|
for trimmed_name, brief in zip(trimmed_names, briefs):
|
|
|
|
if brief:
|
2014-11-13 20:38:56 +00:00
|
|
|
print('\t/** %s */' % brief, file=header)
|
|
|
|
print('\tstatic const %s * const %s;' % (
|
|
|
|
classname, trimmed_name), file=header)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Declare additional methods if present
|
|
|
|
filename = os.path.join(dirname, "%s_methods.hpp" % classname)
|
|
|
|
if os.path.exists(filename):
|
2014-11-13 20:38:56 +00:00
|
|
|
print(str.join('', open(filename).readlines()), file=header)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Begin private declarations
|
2014-11-13 20:38:56 +00:00
|
|
|
print(header_private_template.format(
|
|
|
|
classname=classname, enumname=enum_name), file=header)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Declare private constants for each enum value
|
2014-08-01 16:18:09 +00:00
|
|
|
for trimmed_name in trimmed_names:
|
2014-11-13 20:38:56 +00:00
|
|
|
print('\tstatic const %s _%s;' % (classname, trimmed_name), file=header)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# End class declaration
|
2014-11-13 20:38:56 +00:00
|
|
|
print('};', file=header)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Define private constants for each enum value
|
2014-08-01 16:18:09 +00:00
|
|
|
for name, trimmed_name in zip(member_names, trimmed_names):
|
2014-11-13 20:38:56 +00:00
|
|
|
print('const %s %s::_%s = %s(%s, "%s");' % (
|
|
|
|
classname, classname, trimmed_name, classname, name, trimmed_name),
|
|
|
|
file=code)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Define public pointers for each enum value
|
2014-08-01 16:18:09 +00:00
|
|
|
for trimmed_name in trimmed_names:
|
2014-11-13 20:38:56 +00:00
|
|
|
print('const %s * const %s::%s = &%s::_%s;' % (
|
|
|
|
classname, classname, trimmed_name, classname, trimmed_name),
|
|
|
|
file=code)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Define map of enum values to constants
|
2015-01-26 14:26:15 +00:00
|
|
|
print('template<> const SR_API std::map<const enum %s, const %s * const> EnumValue<%s, enum %s>::_values = {' % (
|
2014-11-13 20:38:56 +00:00
|
|
|
enum_name, classname, classname, enum_name), file=code)
|
2014-08-01 16:18:09 +00:00
|
|
|
for name, trimmed_name in zip(member_names, trimmed_names):
|
2014-11-13 20:38:56 +00:00
|
|
|
print('\t{%s, %s::%s},' % (name, classname, trimmed_name), file=code)
|
|
|
|
print('};', file=code)
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
# Define additional methods if present
|
|
|
|
filename = os.path.join(dirname, "%s_methods.cpp" % classname)
|
|
|
|
if os.path.exists(filename):
|
2014-11-13 20:38:56 +00:00
|
|
|
print(str.join('', open(filename).readlines()), file=code)
|
2014-10-29 14:36:45 +00:00
|
|
|
|
2014-11-11 01:18:58 +00:00
|
|
|
# Map EnumValue::id() and EnumValue::name() as SWIG attributes.
|
2014-11-13 20:38:56 +00:00
|
|
|
print('%%attribute(sigrok::%s, int, id, id);' % classname, file=swig)
|
|
|
|
print('%%attributestring(sigrok::%s, std::string, name, name);' % classname,
|
|
|
|
file=swig)
|
2014-11-11 01:18:58 +00:00
|
|
|
|
|
|
|
# Instantiate EnumValue template for SWIG
|
2014-11-13 20:38:56 +00:00
|
|
|
print('%%template(EnumValue%s) sigrok::EnumValue<sigrok::%s, enum %s>;' % (
|
|
|
|
classname, classname, enum_name), file=swig)
|
2014-11-11 01:18:58 +00:00
|
|
|
|
2014-11-11 18:26:50 +00:00
|
|
|
# Apply any language-specific extras.
|
2014-11-13 20:38:56 +00:00
|
|
|
print('%%enumextras(%s);' % classname, file=swig)
|
2014-11-11 18:26:50 +00:00
|
|
|
|
2014-11-11 01:24:19 +00:00
|
|
|
# Declare additional attributes if present
|
|
|
|
filename = os.path.join(dirname, "%s_methods.i" % classname)
|
|
|
|
if os.path.exists(filename):
|
2014-11-13 20:38:56 +00:00
|
|
|
print(str.join('', open(filename).readlines()), file=swig)
|