__init__.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2011, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import genmsg.msgs
34 
35 try:
36  from cStringIO import StringIO # Python 2.x
37 except ImportError:
38  from io import StringIO # Python 3.x
39 
40 MSG_TYPE_TO_CPP = {
41  'byte': 'int8_t',
42  'char': 'uint8_t',
43  'bool': 'uint8_t',
44  'uint8': 'uint8_t',
45  'int8': 'int8_t',
46  'uint16': 'uint16_t',
47  'int16': 'int16_t',
48  'uint32': 'uint32_t',
49  'int32': 'int32_t',
50  'uint64': 'uint64_t',
51  'int64': 'int64_t',
52  'float32': 'float',
53  'float64': 'double',
54  'string': 'std::basic_string<char, std::char_traits<char>, typename ContainerAllocator::template rebind<char>::other > ',
55  'time': 'ros::Time',
56  'duration': 'ros::Duration',
57 }
58 
59 
60 # used
61 def msg_type_to_cpp(type_):
62  """
63  Convert a message type into the C++ declaration for that type.
64 
65  Example message types: uint32, std_msgs/String.
66  Example C++ types: uint32_t, std_msgs::String_<ContainerAllocator>
67 
68  @param type_: The message type
69  @type type_: str
70  @return: The C++ declaration
71  @rtype: str
72  """
73  (base_type, is_array, array_len) = genmsg.msgs.parse_type(type_)
74  cpp_type = None
75  if (genmsg.msgs.is_builtin(base_type)):
76  cpp_type = MSG_TYPE_TO_CPP[base_type]
77  elif (len(base_type.split('/')) == 1):
78  if (genmsg.msgs.is_header_type(base_type)):
79  cpp_type = ' ::std_msgs::Header_<ContainerAllocator> '
80  else:
81  cpp_type = '%s_<ContainerAllocator> ' % (base_type)
82  else:
83  pkg = base_type.split('/')[0]
84  msg = base_type.split('/')[1]
85  cpp_type = ' ::%s::%s_<ContainerAllocator> ' % (pkg, msg)
86 
87  if (is_array):
88  if (array_len is None):
89  return 'std::vector<%s, typename ContainerAllocator::template rebind<%s>::other > ' % (cpp_type, cpp_type)
90  else:
91  return 'boost::array<%s, %s> ' % (cpp_type, array_len)
92  else:
93  return cpp_type
94 
95 
97  s = s.replace('\\', '\\\\')
98  s = s.replace('"', '\\"')
99  return s
100 
101 
103  lines = definition.splitlines()
104  if not lines:
105  lines.append('')
106  s = StringIO()
107  for line in lines:
108  line = _escape_string(line)
109  # individual string literals cannot be too long; need to utilize string concatenation for long strings
110  # https://docs.microsoft.com/en-us/cpp/c-language/maximum-string-length?view=vs-2017
111  s.write('"%s\\n"\n' % (line))
112 
113  val = s.getvalue()
114  s.close()
115  return val
116 
117 
118 # used2
119 def cpp_message_declarations(name_prefix, msg):
120  """
121  Return the different possible C++ declarations for a message given the message itself.
122 
123  @param name_prefix: The C++ prefix to be prepended to the name, e.g. "std_msgs::"
124  @type name_prefix: str
125  @param msg: The message type
126  @type msg: str
127  @return: A tuple of 3 different names. cpp_message_decelarations("std_msgs::", "String") returns the tuple
128  ("std_msgs::String_", "std_msgs::String_<ContainerAllocator>", "std_msgs::String")
129  @rtype: str
130  """
131  pkg, basetype = genmsg.names.package_resource_name(msg)
132  cpp_name = ' ::%s%s' % (name_prefix, msg)
133  if (pkg):
134  cpp_name = ' ::%s::%s' % (pkg, basetype)
135  return ('%s_' % (cpp_name), '%s_<ContainerAllocator> ' % (cpp_name), '%s' % (cpp_name))
136 
137 
138 # todo
139 def is_fixed_length(spec, msg_context, includepath):
140  """
141  Return whether or not the message is fixed-length.
142 
143  @param spec: The message spec
144  @type spec: genmsg.msgs.MsgSpec
145  @param package: The package of the
146  @type package: str
147  """
148  types = []
149  for field in spec.parsed_fields():
150  if (field.is_array and field.array_len is None):
151  return False
152 
153  if (field.base_type == 'string'):
154  return False
155 
156  if (not field.is_builtin):
157  types.append(field.base_type)
158 
159  types = set(types)
160  for t in types:
161  t = genmsg.msgs.resolve_type(t, spec.package)
162  assert isinstance(includepath, dict)
163  new_spec = genmsg.msg_loader.load_msg_by_type(msg_context, t, includepath)
164  if (not is_fixed_length(new_spec, msg_context, includepath)):
165  return False
166 
167  return True
168 
169 
170 # used2
171 def default_value(type_):
172  """
173  Return the value to initialize a message member with.
174 
175  0 for integer types, 0.0 for floating point, false for bool,
176  empty string for everything else
177 
178  @param type_: The type
179  @type type_: str
180  """
181  if type_ in [
182  'byte', 'int8', 'int16', 'int32', 'int64',
183  'char', 'uint8', 'uint16', 'uint32', 'uint64',
184  ]:
185  return '0'
186  elif type_ in ['float32', 'float64']:
187  return '0.0'
188  elif type_ == 'bool':
189  return 'false'
190 
191  return ''
192 
193 
194 # used2
195 def takes_allocator(type_):
196  """
197  Return whether or not a type can take an allocator in its constructor.
198 
199  False for all builtin types except string.
200  True for all others.
201 
202  @param type_: The type
203  @type: str
204  """
205  return type_ not in [
206  'byte', 'int8', 'int16', 'int32', 'int64',
207  'char', 'uint8', 'uint16', 'uint32', 'uint64',
208  'float32', 'float64', 'bool', 'time', 'duration']
209 
210 
211 def escape_string(str_):
212  str_ = str_.replace('\\', '\\\\')
213  str_ = str_.replace('"', '\\"')
214  return str_
215 
216 
217 # used
218 def generate_fixed_length_assigns(spec, container_gets_allocator, cpp_name_prefix):
219  """
220  Initialize any fixed-length arrays.
221 
222  @param s: The stream to write to
223  @type s: stream
224  @param spec: The message spec
225  @type spec: genmsg.msgs.MsgSpec
226  @param container_gets_allocator: Whether or not a container type (whether it's another message, a vector, array or string)
227  should have the allocator passed to its constructor. Assumes the allocator is named _alloc.
228  @type container_gets_allocator: bool
229  @param cpp_name_prefix: The C++ prefix to use when referring to the message, e.g. "std_msgs::"
230  @type cpp_name_prefix: str
231  """
232  # Assign all fixed-length arrays their default values
233  for field in spec.parsed_fields():
234  if (not field.is_array or field.array_len is None):
235  continue
236 
237  val = default_value(field.base_type)
238  if (container_gets_allocator and takes_allocator(field.base_type)):
239  # String is a special case, as it is the only builtin type that takes an allocator
240  if (field.base_type == 'string'):
241  string_cpp = msg_type_to_cpp('string')
242  yield ' %s.assign(%s(_alloc));\n' % (field.name, string_cpp)
243  else:
244  (cpp_msg_unqualified, cpp_msg_with_alloc, _) = cpp_message_declarations(cpp_name_prefix, field.base_type)
245  yield ' %s.assign(%s(_alloc));\n' % (field.name, cpp_msg_with_alloc)
246  elif (len(val) > 0):
247  yield ' %s.assign(%s);\n' % (field.name, val)
248 
249 
250 # used
251 def generate_initializer_list(spec, container_gets_allocator):
252  """
253  Write the initializer list for a constructor.
254 
255  @param s: The stream to write to
256  @type s: stream
257  @param spec: The message spec
258  @type spec: genmsg.msgs.MsgSpec
259  @param container_gets_allocator: Whether or not a container type (whether it's another message, a vector, array or string)
260  should have the allocator passed to its constructor. Assumes the allocator is named _alloc.
261  @type container_gets_allocator: bool
262  """
263  op = ':'
264  for field in spec.parsed_fields():
265  val = default_value(field.base_type)
266  use_alloc = takes_allocator(field.base_type)
267  if (field.is_array):
268  if (field.array_len is None and container_gets_allocator):
269  yield ' %s %s(_alloc)' % (op, field.name)
270  else:
271  yield ' %s %s()' % (op, field.name)
272  else:
273  if (container_gets_allocator and use_alloc):
274  yield ' %s %s(_alloc)' % (op, field.name)
275  else:
276  yield ' %s %s(%s)' % (op, field.name, val)
277  op = ','
def _escape_string(s)
Definition: __init__.py:96
def generate_fixed_length_assigns(spec, container_gets_allocator, cpp_name_prefix)
Definition: __init__.py:218
def cpp_message_declarations(name_prefix, msg)
Definition: __init__.py:119
def default_value(type_)
Definition: __init__.py:171
def generate_initializer_list(spec, container_gets_allocator)
Definition: __init__.py:251
def escape_string(str_)
Definition: __init__.py:211
def msg_type_to_cpp(type_)
Definition: __init__.py:61
def escape_message_definition(definition)
Definition: __init__.py:102
def is_fixed_length(spec, msg_context, includepath)
Definition: __init__.py:139
def takes_allocator(type_)
Definition: __init__.py:195


gencpp
Author(s): Josh Faust, Troy Straszheim, Morgen Kjaergaard
autogenerated on Tue Aug 4 2020 03:35:57