Skip to content

Commit

Permalink
add namespace feature (depends on ros/genmsg#76).
Browse files Browse the repository at this point in the history
  • Loading branch information
kohei306 committed Oct 14, 2020
1 parent e85a185 commit 09bfb21
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
23 changes: 16 additions & 7 deletions scripts/msg.h.template
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContainerAllocator>'%(cpp_full_name)
Expand Down Expand Up @@ -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 <class ContainerAllocator>
struct @(spec.short_name)_
Expand All @@ -89,15 +89,21 @@ 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]@
#endif
@[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]
Expand Down Expand Up @@ -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
Expand All @@ -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<typename ContainerAllocator> const @(gencpp.msg_type_to_cpp(c.type))
template<typename ContainerAllocator> const @(gencpp.msg_type_to_cpp(c.type, ''))
@(spec.short_name)_<ContainerAllocator>::@(c.name) =
@[if c.type == 'string']
"@(gencpp.escape_string(c.val))"
Expand Down Expand Up @@ -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))
Expand Down
15 changes: 10 additions & 5 deletions src/gencpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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_<ContainerAllocator> ' % (pkg, msg)
cpp_type = ' ::%s::%s_<ContainerAllocator> ' % (name_space_, msg)

if (is_array):
if (array_len is None):
Expand Down Expand Up @@ -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.
Expand All @@ -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_<ContainerAllocator> ' % (cpp_name), '%s' % (cpp_name))


Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit 09bfb21

Please sign in to comment.