__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 = {'byte': 'int8_t',
41  'char': 'uint8_t',
42  'bool': 'uint8_t',
43  'uint8': 'uint8_t',
44  'int8': 'int8_t',
45  'uint16': 'uint16_t',
46  'int16': 'int16_t',
47  'uint32': 'uint32_t',
48  'int32': 'int32_t',
49  'uint64': 'uint64_t',
50  'int64': 'int64_t',
51  'float32': 'float',
52  'float64': 'double',
53  'string': 'std::basic_string<char, std::char_traits<char>, typename ContainerAllocator::template rebind<char>::other > ',
54  'time': 'ros::Time',
55  'duration': 'ros::Duration'}
56 
57 #used
58 def msg_type_to_cpp(type):
59  """
60  Converts a message type (e.g. uint32, std_msgs/String, etc.) into the C++ declaration
61  for that type (e.g. uint32_t, std_msgs::String_<ContainerAllocator>)
62 
63  @param type: The message type
64  @type type: str
65  @return: The C++ declaration
66  @rtype: str
67  """
68  (base_type, is_array, array_len) = genmsg.msgs.parse_type(type)
69  cpp_type = None
70  if (genmsg.msgs.is_builtin(base_type)):
71  cpp_type = MSG_TYPE_TO_CPP[base_type]
72  elif (len(base_type.split('/')) == 1):
73  if (genmsg.msgs.is_header_type(base_type)):
74  cpp_type = ' ::std_msgs::Header_<ContainerAllocator> '
75  else:
76  cpp_type = '%s_<ContainerAllocator> '%(base_type)
77  else:
78  pkg = base_type.split('/')[0]
79  msg = base_type.split('/')[1]
80  cpp_type = ' ::%s::%s_<ContainerAllocator> '%(pkg, msg)
81 
82  if (is_array):
83  if (array_len is None):
84  return 'std::vector<%s, typename ContainerAllocator::template rebind<%s>::other > '%(cpp_type, cpp_type)
85  else:
86  return 'boost::array<%s, %s> '%(cpp_type, array_len)
87  else:
88  return cpp_type
89 
91  s = s.replace('\\', '\\\\')
92  s = s.replace('"', '\\"')
93  return s
94 
95 def escape_message_definition(definition):
96  lines = definition.splitlines()
97  if not lines:
98  lines.append('')
99  s = StringIO()
100  for line in lines:
101  line = _escape_string(line)
102  # individual string literals cannot be too long; need to utilize string concatenation for long strings
103  # https://docs.microsoft.com/en-us/cpp/c-language/maximum-string-length?view=vs-2017
104  s.write('"%s\\n"\n'%(line))
105 
106  val = s.getvalue()
107  s.close()
108  return val
109 
110 #used2
111 def cpp_message_declarations(name_prefix, msg):
112  """
113  Returns the different possible C++ declarations for a message given the message itself.
114 
115  @param name_prefix: The C++ prefix to be prepended to the name, e.g. "std_msgs::"
116  @type name_prefix: str
117  @param msg: The message type
118  @type msg: str
119  @return: A tuple of 3 different names. cpp_message_decelarations("std_msgs::", "String") returns the tuple
120  ("std_msgs::String_", "std_msgs::String_<ContainerAllocator>", "std_msgs::String")
121  @rtype: str
122  """
123  pkg, basetype = genmsg.names.package_resource_name(msg)
124  cpp_name = ' ::%s%s'%(name_prefix, msg)
125  if (pkg):
126  cpp_name = ' ::%s::%s'%(pkg, basetype)
127  return ('%s_'%(cpp_name), '%s_<ContainerAllocator> '%(cpp_name), '%s'%(cpp_name))
128 
129 #todo
130 def is_fixed_length(spec, msg_context, includepath):
131  """
132  Returns whether or not the message is fixed-length
133 
134  @param spec: The message spec
135  @type spec: genmsg.msgs.MsgSpec
136  @param package: The package of the
137  @type package: str
138  """
139  types = []
140  for field in spec.parsed_fields():
141  if (field.is_array and field.array_len is None):
142  return False
143 
144  if (field.base_type == 'string'):
145  return False
146 
147  if (not field.is_builtin):
148  types.append(field.base_type)
149 
150  types = set(types)
151  for t in types:
152  t = genmsg.msgs.resolve_type(t, spec.package)
153  assert isinstance(includepath, dict)
154  new_spec = genmsg.msg_loader.load_msg_by_type(msg_context, t, includepath)
155  if (not is_fixed_length(new_spec, msg_context, includepath)):
156  return False
157 
158  return True
159 
160 #used2
161 def default_value(type):
162  """
163  Returns the value to initialize a message member with. 0 for integer types, 0.0 for floating point, false for bool,
164  empty string for everything else
165 
166  @param type: The type
167  @type type: str
168  """
169  if type in ['byte', 'int8', 'int16', 'int32', 'int64',
170  'char', 'uint8', 'uint16', 'uint32', 'uint64']:
171  return '0'
172  elif type in ['float32', 'float64']:
173  return '0.0'
174  elif type == 'bool':
175  return 'false'
176 
177  return ""
178 #used2
179 def takes_allocator(type):
180  """
181  Returns whether or not a type can take an allocator in its constructor. False for all builtin types except string.
182  True for all others.
183 
184  @param type: The type
185  @type: str
186  """
187  return not type in ['byte', 'int8', 'int16', 'int32', 'int64',
188  'char', 'uint8', 'uint16', 'uint32', 'uint64',
189  'float32', 'float64', 'bool', 'time', 'duration']
190 
191 def escape_string(str):
192  str = str.replace('\\', '\\\\')
193  str = str.replace('"', '\\"')
194  return str
195 
196 #used
197 def generate_fixed_length_assigns(spec, container_gets_allocator, cpp_name_prefix):
198  """
199  Initialize any fixed-length arrays
200 
201  @param s: The stream to write to
202  @type s: stream
203  @param spec: The message spec
204  @type spec: genmsg.msgs.MsgSpec
205  @param container_gets_allocator: Whether or not a container type (whether it's another message, a vector, array or string)
206  should have the allocator passed to its constructor. Assumes the allocator is named _alloc.
207  @type container_gets_allocator: bool
208  @param cpp_name_prefix: The C++ prefix to use when referring to the message, e.g. "std_msgs::"
209  @type cpp_name_prefix: str
210  """
211  # Assign all fixed-length arrays their default values
212  for field in spec.parsed_fields():
213  if (not field.is_array or field.array_len is None):
214  continue
215 
216  val = default_value(field.base_type)
217  if (container_gets_allocator and takes_allocator(field.base_type)):
218  # String is a special case, as it is the only builtin type that takes an allocator
219  if (field.base_type == "string"):
220  string_cpp = msg_type_to_cpp("string")
221  yield ' %s.assign(%s(_alloc));\n'%(field.name, string_cpp)
222  else:
223  (cpp_msg_unqualified, cpp_msg_with_alloc, _) = cpp_message_declarations(cpp_name_prefix, field.base_type)
224  yield ' %s.assign(%s(_alloc));\n'%(field.name, cpp_msg_with_alloc)
225  elif (len(val) > 0):
226  yield ' %s.assign(%s);\n'%(field.name, val)
227 
228 #used
229 def generate_initializer_list(spec, container_gets_allocator):
230  """
231  Writes the initializer list for a constructor
232 
233  @param s: The stream to write to
234  @type s: stream
235  @param spec: The message spec
236  @type spec: genmsg.msgs.MsgSpec
237  @param container_gets_allocator: Whether or not a container type (whether it's another message, a vector, array or string)
238  should have the allocator passed to its constructor. Assumes the allocator is named _alloc.
239  @type container_gets_allocator: bool
240  """
241 
242  op = ':'
243  for field in spec.parsed_fields():
244  val = default_value(field.base_type)
245  use_alloc = takes_allocator(field.base_type)
246  if (field.is_array):
247  if (field.array_len is None and container_gets_allocator):
248  yield ' %s %s(_alloc)'%(op, field.name)
249  else:
250  yield ' %s %s()'%(op, field.name)
251  else:
252  if (container_gets_allocator and use_alloc):
253  yield ' %s %s(_alloc)'%(op, field.name)
254  else:
255  yield ' %s %s(%s)'%(op, field.name, val)
256  op = ','
def _escape_string(s)
Definition: __init__.py:90
def generate_fixed_length_assigns(spec, container_gets_allocator, cpp_name_prefix)
Definition: __init__.py:197
def escape_string(str)
Definition: __init__.py:191
def takes_allocator(type)
Definition: __init__.py:179
def cpp_message_declarations(name_prefix, msg)
Definition: __init__.py:111
def generate_initializer_list(spec, container_gets_allocator)
Definition: __init__.py:229
def default_value(type)
Definition: __init__.py:161
def escape_message_definition(definition)
Definition: __init__.py:95
def is_fixed_length(spec, msg_context, includepath)
Definition: __init__.py:130
def msg_type_to_cpp(type)
Definition: __init__.py:58


gencpp
Author(s): Josh Faust, Troy Straszheim, Morgen Kjaergaard
autogenerated on Wed Mar 20 2019 07:50:25