create_boost_header.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 from __future__ import unicode_literals
5 
6 import sys
7 
8 import roslib
9 
10 import gencpp
11 import genmsg
12 
13 from roslib import packages,msgs
14 import os
15 
16 from io import StringIO
17 
18 import argparse
19 
20 NAME='create_boost_header'
21 
22 def write_boost_includes(s, spec):
23  """
24  Writes the message-specific includes
25 
26  @param s: The stream to write to
27  @type s: stream
28  @param spec: The message spec to iterate over
29  @type spec: roslib.msgs.MsgSpec
30  @param serializer: The serializer type for which to include headers
31  @type serializer: str
32  """
33  for field in spec.parsed_fields():
34  if (not field.is_builtin):
35  (pkg, name) = genmsg.names.package_resource_name(field.base_type)
36  pkg = (pkg or spec.package) # convert '' to this package
37  s.write('#include <%s/boost/%s.h>\n'%(pkg, name))
38 
39  s.write('\n')
40 
41 
42 def write_boost_serialization(s, spec, cpp_name_prefix, file):
43  """
44  Writes the boost::serialize function for a message
45 
46  @param s: Stream to write to
47  @type s: stream
48  @param spec: The message spec
49  @type spec: roslib.msgs.MsgSpec
50  @param cpp_name_prefix: The C++ prefix to prepend to a message to refer to it (e.g. "std_msgs::")
51  @type cpp_name_prefix: str
52  """
53  (cpp_msg_unqualified, cpp_msg_with_alloc, _) = gencpp.cpp_message_declarations(cpp_name_prefix, spec.short_name)
54 
55  s.write("/* Auto-generated by create_boost_header.py for file %s */\n"%(file))
56  s.write('#ifndef %s_BOOST_SERIALIZATION_%s_H\n'%(spec.package.upper(), spec.short_name.upper()))
57  s.write('#define %s_BOOST_SERIALIZATION_%s_H\n\n'%(spec.package.upper(), spec.short_name.upper()))
58  s.write('#include <boost/serialization/serialization.hpp>\n')
59  s.write('#include <boost/serialization/nvp.hpp>\n')
60  s.write('#include <boost/serialization/vector.hpp>\n')
61  s.write('#include <boost/serialization/string.hpp>\n')
62  s.write('#include <%s/%s.h>\n'%(spec.package,spec.short_name))
63  write_boost_includes(s, spec)
64  s.write('namespace boost\n{\n')
65  s.write('namespace serialization\n{\n\n')
66 
67  s.write('template<class Archive, class ContainerAllocator>\n')
68 
69  s.write('void serialize(Archive& a, %s & m, unsigned int)\n{\n'%(cpp_msg_with_alloc))
70 
71  for field in spec.parsed_fields():
72  s.write(' a & make_nvp("%s",m.%s);\n'%(field.name,field.name))
73  s.write('}\n\n')
74 
75  s.write('} // namespace serialization\n')
76  s.write('} // namespace boost\n\n')
77  s.write('#endif // %s_BOOST_SERIALIZATION_%s_H\n'%(spec.package.upper(), spec.short_name.upper()))
78 
79 
80 
81 def generate_boost_serialization(package, msg_path, msg_type, boost_header_path):
82  """
83  Generate a boost::serialization header
84 
85  @param msg_path: The path to the .msg file
86  @type msg_path: str
87  """
88  mc = genmsg.msg_loader.MsgContext()
89 
90  spec = genmsg.msg_loader.load_msg_from_file(mc, msg_path, msg_type)
91  cpp_prefix = '%s::'%(package)
92 
93  s = StringIO()
94  write_boost_serialization(s, spec, cpp_prefix, msg_path)
95 
96  (output_dir,filename) = os.path.split(boost_header_path)
97  try:
98  os.makedirs(output_dir)
99  except OSError as e:
100  pass
101 
102  f = open(boost_header_path, 'w')
103  print(s.getvalue(), file=f)
104 
105  s.close()
106 
107 
108 def create_boost_headers(argv, stdout, stderr):
109  parser = argparse.ArgumentParser(description='Generate boost serialization header for ROS message.')
110  parser.add_argument('pkg',metavar='PKG',type=str, nargs=1,help='The package name.')
111  parser.add_argument('msg_type',metavar='MSG_TYPE',type=str, nargs=1,help='The message type.')
112  parser.add_argument('msg_file_path',metavar='MSG_FILE_PATH',type=str, nargs=1,help='The full path to a .msg file.')
113  parser.add_argument('boost_file_path',metavar='BOOST_HEADER_PATH',type=str, nargs=1,help='The full path to the generated boost header file.')
114 
115  args = parser.parse_args()
116 
117  generate_boost_serialization(args.pkg[0], args.msg_file_path[0], args.msg_type[0], args.boost_file_path[0])
118 
119 if __name__ == "__main__":
120  try:
121  create_boost_headers(sys.argv, sys.stdout, sys.stderr)
122  except Exception as e:
123  sys.stderr.write("Failed to generate boost headers: " + str(e))
124  raise
125  #sys.exit(1)
def write_boost_includes(s, spec)
def create_boost_headers(argv, stdout, stderr)
def cpp_message_declarations(name_prefix, msg)
def generate_boost_serialization(package, msg_path, msg_type, boost_header_path)
def write_boost_serialization(s, spec, cpp_name_prefix, file)


rtt_roscomm
Author(s): Ruben Smits, Jonathan Bohren
autogenerated on Mon May 10 2021 02:45:04