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