39 import msg_gen
as genmsg_cpp
47 import roslib.packages
48 import roslib.gentools
49 from rospkg
import RosPack
52 from cStringIO
import StringIO
54 from io
import StringIO
58 Writes the beginning of the header file: a comment saying it's auto-generated and the include guards 60 @param s: The stream to write to 63 @type spec: roslib.srvs.SrvSpec 64 @param file: The file this service is being generated for 67 s.write(
"/* Auto-generated by genmsg_cpp for file %s */\n"%(file))
68 s.write(
'#ifndef %s_SERVICE_%s_H\n'%(spec.package.upper(), spec.short_name.upper()))
69 s.write(
'#define %s_SERVICE_%s_H\n'%(spec.package.upper(), spec.short_name.upper()))
73 Writes the end of the header file: the ending of the include guards 75 @param s: The stream to write to 78 @type spec: roslib.srvs.SrvSpec 80 s.write(
'#endif // %s_SERVICE_%s_H\n'%(spec.package.upper(), spec.short_name.upper()))
84 Writes the includes that all service need 86 @param s: The stream to write to 89 s.write(
'#include "ros/service_traits.h"\n\n')
93 Writes a class trait for traits which have static value() members that return const char* 95 e.g. write_trait_char_class(s, "MD5Sum", "std_srvs::Empty", "hello") yields: 97 struct MD5Sum<std_srvs::Empty> 99 static const char* value() { return "hello"; } 100 static const char* value(const std_srvs::Empty&) { return value(); } 103 @param s: The stream to write to 105 @param class_name: The name of the trait class 106 @type class_name: str 107 @param cpp_msg: The C++ message declaration, e.g. "std_srvs::Empty" 109 @param value: The string value to return from the value() function 112 s.write(
'template<>\nstruct %s<%s> {\n'%(class_name, cpp_msg))
113 s.write(
' static const char* value() \n {\n return "%s";\n }\n\n'%(value))
114 s.write(
' static const char* value(const %s&) { return value(); } \n'%(cpp_msg))
119 Write all the service traits for a message 121 @param s: The stream to write to 123 @param spec: The service spec 124 @type spec: roslib.srvs.SrvSpec 125 @param cpp_name_prefix: The C++ prefix to prepend when referencing the service, e.g. "std_srvs::" 126 @type cpp_name_prefix: str 128 gendeps_dict = roslib.gentools.get_dependencies(spec, spec.package, rospack=rospack)
129 md5sum = roslib.gentools.compute_md5(gendeps_dict, rospack=rospack)
131 s.write(
'namespace ros\n{\n')
132 s.write(
'namespace service_traits\n{\n')
134 request_with_allocator =
'%s%s_<ContainerAllocator> '%(cpp_name_prefix, spec.request.short_name)
135 response_with_allocator =
'%s%s_<ContainerAllocator> '%(cpp_name_prefix, spec.response.short_name)
139 genmsg_cpp.write_trait_char_class(s,
'MD5Sum', request_with_allocator, md5sum)
140 genmsg_cpp.write_trait_char_class(s,
'DataType', request_with_allocator, spec.full_name)
141 genmsg_cpp.write_trait_char_class(s,
'MD5Sum', response_with_allocator, md5sum)
142 genmsg_cpp.write_trait_char_class(s,
'DataType', response_with_allocator, spec.full_name)
143 s.write(
'} // namespace service_traits\n')
144 s.write(
'} // namespace ros\n\n')
150 @param srv_path: the path to the .srv file 153 (package_dir, package) = roslib.packages.get_dir_pkg(srv_path)
154 (_, spec) = roslib.srvs.load_from_file(srv_path, package)
157 cpp_prefix =
'%s::'%(package)
159 genmsg_cpp.write_generic_includes(s)
161 genmsg_cpp.write_includes(s, spec.request)
163 genmsg_cpp.write_includes(s, spec.response)
166 gendeps_dict = roslib.gentools.get_dependencies(spec, spec.package, rospack=rospack)
167 md5sum = roslib.gentools.compute_md5(gendeps_dict, rospack=rospack)
169 s.write(
'namespace %s\n{\n'%(package))
170 genmsg_cpp.write_struct(s, spec.request, cpp_prefix, {
'ServerMD5Sum': md5sum})
171 genmsg_cpp.write_constant_definitions(s, spec.request)
173 genmsg_cpp.write_struct(s, spec.response, cpp_prefix, {
'ServerMD5Sum': md5sum})
174 genmsg_cpp.write_constant_definitions(s, spec.response)
175 s.write(
'struct %s\n{\n'%(spec.short_name))
177 s.write(
'typedef %s Request;\n'%(spec.request.short_name))
178 s.write(
'typedef %s Response;\n'%(spec.response.short_name))
179 s.write(
'Request request;\n')
180 s.write(
'Response response;\n\n')
181 s.write(
'typedef Request RequestType;\n')
182 s.write(
'typedef Response ResponseType;\n')
183 s.write(
'}; // struct %s\n'%(spec.short_name))
184 s.write(
'} // namespace %s\n\n'%(package))
186 request_cpp_name =
"Request" 187 response_cpp_name =
"Response" 188 genmsg_cpp.write_traits(s, spec.request, cpp_prefix, rospack=rospack)
190 genmsg_cpp.write_traits(s, spec.response, cpp_prefix, rospack=rospack)
191 genmsg_cpp.write_serialization(s, spec.request, cpp_prefix)
193 genmsg_cpp.write_serialization(s, spec.response, cpp_prefix)
199 output_dir =
'%s/srv_gen/cpp/include/%s'%(package_dir, package)
200 if (
not os.path.exists(output_dir)):
204 os.makedirs(output_dir)
208 f = open(
'%s/%s.h'%(output_dir, spec.short_name),
'w')
209 f.write(s.getvalue() +
"\n")
217 if __name__ ==
"__main__":
218 roslib.msgs.set_verbose(
False)
def generate_services(argv)
def write_traits(s, spec, cpp_name_prefix, rospack=None)
def write_trait_char_class(s, class_name, cpp_msg, value)
def write_generic_includes(s)
def write_begin(s, spec, file)