From 09bfb21a44035c9e2e423f2b4868c9342bb259ae Mon Sep 17 00:00:00 2001 From: Kohei Otsuka <13173186+rjhcnf@users.noreply.github.com> Date: Wed, 14 Oct 2020 19:50:26 +0200 Subject: [PATCH] add namespace feature (depends on ros/genmsg#76). --- scripts/msg.h.template | 23 ++++++++++++++++------- src/gencpp/__init__.py | 15 ++++++++++----- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/scripts/msg.h.template b/scripts/msg.h.template index e35a71d..db0a46d 100644 --- a/scripts/msg.h.template +++ b/scripts/msg.h.template @@ -21,7 +21,7 @@ import genmsg.msgs import gencpp import os -cpp_namespace = '::%s::'%(spec.package) # TODO handle nested namespace +cpp_namespace = '::%s::'%(spec.name_space) # TODO handle nested namespace cpp_class = '%s_'%spec.short_name cpp_full_name = '%s%s'%(cpp_namespace,cpp_class) cpp_full_name_with_alloc = '%s'%(cpp_full_name) @@ -67,7 +67,7 @@ for field in spec.parsed_fields(): #include <@(spec.package)/plugin/@(spec.short_name).h> @[end if]@ -namespace @(spec.package) +namespace @(spec.name_space) { template struct @(spec.short_name)_ @@ -89,7 +89,7 @@ struct @(spec.short_name)_ @[if alloc_name != '' and not [f for f in spec.parsed_fields() if f.is_array and f.array_len is not None and f.base_type == 'string']]@ (void)_alloc; @[end if]@ - @('\n '.join(gencpp.generate_fixed_length_assigns(spec, alloc_name != '', '%s::'%(spec.package))))@ + @('\n '.join(gencpp.generate_fixed_length_assigns(spec, alloc_name != '', '%s::'%(spec.package), msg_context)))@ } @[end for] @[if has_plugin]@ @@ -97,7 +97,13 @@ struct @(spec.short_name)_ @[end if]@ @[for field in spec.parsed_fields()] - @{cpp_type = gencpp.msg_type_to_cpp(field.type)}@ +@{ +name_space = '' +if msg_context.is_registered(field.base_type): + name_space = msg_context.get_registered(field.base_type).name_space + +cpp_type = gencpp.msg_type_to_cpp(field.type, name_space) +}@ typedef @(cpp_type) _@(field.name)_type; _@(field.name)_type @(field.name); @[end for] @@ -137,7 +143,7 @@ for constant in spec.constants: @# Non-integer constants @[for constant in constants_non_integer]@ - static const @(gencpp.msg_type_to_cpp(constant.type)) @(constant.name); + static const @(gencpp.msg_type_to_cpp(constant.type, '')) @(constant.name); @[end for]@ @# Shared pointer typedefs @@ -162,7 +168,7 @@ typedef boost::shared_ptr< @(cpp_namespace+spec.short_name) const> @(spec.short_ // constants requiring out of line definition @[for c in spec.constants] @[if c.type not in ['byte', 'int8', 'int16', 'int32', 'int64', 'char', 'uint8', 'uint16', 'uint32', 'uint64']] - template const @(gencpp.msg_type_to_cpp(c.type)) + template const @(gencpp.msg_type_to_cpp(c.type, '')) @(spec.short_name)_::@(c.name) = @[if c.type == 'string'] "@(gencpp.escape_string(c.val))" @@ -327,7 +333,10 @@ struct Printer< @(cpp_full_name_with_alloc) > @# todo, change this stuff below into proper EmPy syntax @{ for field in spec.parsed_fields(): - cpp_type = gencpp.msg_type_to_cpp(field.base_type) + name_space = '' + if msg_context.is_registered(field.base_type): + name_space = msg_context.get_registered(field.base_type).name_space + cpp_type = gencpp.msg_type_to_cpp(field.base_type, name_space) if (field.is_array): print(' s << indent << "%s[]" << std::endl;'%(field.name)) print(' for (size_t i = 0; i < v.%s.size(); ++i)'%(field.name)) diff --git a/src/gencpp/__init__.py b/src/gencpp/__init__.py index 2a98bc6..ccac8db 100644 --- a/src/gencpp/__init__.py +++ b/src/gencpp/__init__.py @@ -58,7 +58,7 @@ # used -def msg_type_to_cpp(type_): +def msg_type_to_cpp(type_, name_space_): """ Convert a message type into the C++ declaration for that type. @@ -82,7 +82,7 @@ def msg_type_to_cpp(type_): else: pkg = base_type.split('/')[0] msg = base_type.split('/')[1] - cpp_type = ' ::%s::%s_ ' % (pkg, msg) + cpp_type = ' ::%s::%s_ ' % (name_space_, msg) if (is_array): if (array_len is None): @@ -116,7 +116,7 @@ def escape_message_definition(definition): # used2 -def cpp_message_declarations(name_prefix, msg): +def cpp_message_declarations(name_prefix, msg, name_space): """ Return the different possible C++ declarations for a message given the message itself. @@ -132,6 +132,8 @@ def cpp_message_declarations(name_prefix, msg): cpp_name = ' ::%s%s' % (name_prefix, msg) if (pkg): cpp_name = ' ::%s::%s' % (pkg, basetype) + if (name_space) != '': + cpp_name = ' ::%s::%s' % (name_space, basetype) return ('%s_' % (cpp_name), '%s_ ' % (cpp_name), '%s' % (cpp_name)) @@ -215,7 +217,7 @@ def escape_string(str_): # used -def generate_fixed_length_assigns(spec, container_gets_allocator, cpp_name_prefix): +def generate_fixed_length_assigns(spec, container_gets_allocator, cpp_name_prefix, msg_context): """ Initialize any fixed-length arrays. @@ -241,7 +243,10 @@ def generate_fixed_length_assigns(spec, container_gets_allocator, cpp_name_prefi string_cpp = msg_type_to_cpp('string') yield ' %s.assign(%s(_alloc));\n' % (field.name, string_cpp) else: - (cpp_msg_unqualified, cpp_msg_with_alloc, _) = cpp_message_declarations(cpp_name_prefix, field.base_type) + name_space = '' + if msg_context.is_registered(field.base_type): + name_space = msg_context.get_registered(field.base_type).name_space + (cpp_msg_unqualified, cpp_msg_with_alloc, _) = cpp_message_declarations(cpp_name_prefix, field.base_type, name_space) yield ' %s.assign(%s(_alloc));\n' % (field.name, cpp_msg_with_alloc) elif (len(val) > 0): yield ' %s.assign(%s);\n' % (field.name, val)