예제 #1
0
def write_boost_serialization(s, spec, cpp_name_prefix, file):
    """
    Writes the boost::serialize function for a message

    @param s: Stream to write to
    @type s: stream
    @param spec: The message spec
    @type spec: roslib.msgs.MsgSpec
    @param cpp_name_prefix: The C++ prefix to prepend to a message to refer to it (e.g. "std_msgs::")
    @type cpp_name_prefix: str
    """
    (cpp_msg_unqualified, cpp_msg_with_alloc,
     _) = gencpp.cpp_message_declarations(cpp_name_prefix, spec.short_name)

    port_spec_dict = get_port_spec_dict(spec)

    s.write(
        "/* Auto-generated by create_components_source.py for file %s */\n" %
        (file))

    s.write('#include <%s/subsystem_buffers/%s.h>\n' %
            (spec.package, spec.short_name))
    s.write('#include <common_interfaces/message_concate.h>\n')
    s.write('#include <common_interfaces/message_split.h>\n')
    s.write('#include <rtt/Component.hpp>\n\n')

    s.write('namespace %s {\n\n' % (spec.package))
    s.write('typedef MessageConcate<%s_InputPorts > %sConcate;\n' %
            (spec.short_name, spec.short_name))
    s.write('typedef MessageSplit<%s_OutputPorts > %sSplit;\n' %
            (spec.short_name, spec.short_name))
    s.write('} // namespace %s\n\n' % (spec.package))

    s.write('typedef %s::%sConcate %s_%sConcate;\n' %
            (spec.package, spec.short_name, spec.package, spec.short_name))
    s.write('ORO_LIST_COMPONENT_TYPE(%s_%sConcate)\n' %
            (spec.package, spec.short_name))

    s.write('typedef %s::%sSplit %s_%sSplit;\n' %
            (spec.package, spec.short_name, spec.package, spec.short_name))
    s.write('ORO_LIST_COMPONENT_TYPE(%s_%sSplit)\n' %
            (spec.package, spec.short_name))
def write_boost_serialization(s, spec, cpp_name_prefix, file):
    """
    Writes the boost::serialize function for a message

    @param s: Stream to write to
    @type s: stream
    @param spec: The message spec
    @type spec: roslib.msgs.MsgSpec
    @param cpp_name_prefix: The C++ prefix to prepend to a message to refer to it (e.g. "std_msgs::")
    @type cpp_name_prefix: str
    """
    (cpp_msg_unqualified, cpp_msg_with_alloc,
     _) = gencpp.cpp_message_declarations(cpp_name_prefix, spec.short_name)

    s.write("/* Auto-generated by create_boost_header.py for file %s */\n" %
            (file))
    s.write('#ifndef %s_BOOST_SERIALIZATION_%s_H\n' %
            (spec.package.upper(), spec.short_name.upper()))
    s.write('#define %s_BOOST_SERIALIZATION_%s_H\n\n' %
            (spec.package.upper(), spec.short_name.upper()))
    s.write('#include <boost/serialization/serialization.hpp>\n')
    s.write('#include <boost/serialization/nvp.hpp>\n')
    s.write('#include <boost/serialization/vector.hpp>\n')
    s.write('#include <boost/serialization/string.hpp>\n')
    s.write('#include <%s/%s.h>\n' % (spec.package, spec.short_name))
    write_boost_includes(s, spec)
    s.write('namespace boost\n{\n')
    s.write('namespace serialization\n{\n\n')

    s.write('template<class Archive, class ContainerAllocator>\n')

    s.write('void serialize(Archive& a, %s & m, unsigned int)\n{\n' %
            (cpp_msg_with_alloc))

    for field in spec.parsed_fields():
        s.write('    a & make_nvp("%s",m.%s);\n' % (field.name, field.name))
    s.write('}\n\n')

    s.write('} // namespace serialization\n')
    s.write('} // namespace boost\n\n')
    s.write('#endif // %s_BOOST_SERIALIZATION_%s_H\n' %
            (spec.package.upper(), spec.short_name.upper()))
def write_boost_serialization(s, spec, cpp_name_prefix, file):
    """
    Writes the boost::serialize function for a message

    @param s: Stream to write to
    @type s: stream
    @param spec: The message spec
    @type spec: roslib.msgs.MsgSpec
    @param cpp_name_prefix: The C++ prefix to prepend to a message to refer to it (e.g. "std_msgs::")
    @type cpp_name_prefix: str
    """
    (cpp_msg_unqualified, cpp_msg_with_alloc,
     _) = gencpp.cpp_message_declarations(cpp_name_prefix, spec.short_name)

    #    port_spec_dict = get_port_spec_dict(spec)

    s.write("/* Auto-generated by create_boost_header.py for file %s */\n" %
            (file))
    s.write('#ifndef %s_SUBSYSTEM_BUFFERS_%s_H\n' %
            (spec.package.upper(), spec.short_name.upper()))
    s.write('#define %s_SUBSYSTEM_BUFFERS_%s_H\n\n' %
            (spec.package.upper(), spec.short_name.upper()))
    s.write('#include <rtt/RTT.hpp>\n')
    s.write('#include <common_interfaces/interface_ports_fwd.h>\n')
    s.write('#include <%s/%s.h>\n' % (spec.package, spec.short_name))

    s.write('namespace %s {\n\n' % (spec.package))

    fields_with_validity = []
    validity_fields = []
    # find all validity fields
    for field in spec.parsed_fields():
        for field_valid in spec.parsed_fields():
            if field.name == field_valid.name + "_valid" and field_valid.type == "bool":
                fields_with_validity.append(field.name)
                validity_fields.append(field.name + "_valid")

    #
    # input ports
    #
    s.write(
        'class %s_InputPorts : public interface_ports::InputPortInterface<%s > {\n'
        % (spec.short_name, spec.short_name))

    s.write('public:\n')
    s.write('  typedef %s Container_;\n' % (spec.short_name))

    s.write('\nprivate:\n')

    s.write(
        '  boost::shared_ptr<interface_ports::InputPortInterface<%s > > this_container__;\n'
        % (spec.short_name))

    # generate member list
    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        s.write(
            '  boost::shared_ptr<interface_ports::InputPortInterface<%s > > %s_;\n'
            % (msg_type_to_cpp(field.type), field.name))
#        if field.name in port_spec_dict:
#            s.write('  boost::shared_ptr<interface_ports::InputPortInterface<%s > > %s_;\n'%(msg_type_to_cpp(field.type), field.name))

    s.write('\npublic:\n')

    s.write(
        '  %s_InputPorts(RTT::TaskContext *tc, const std::string& prefix="");\n'
        % (spec.short_name))

    s.write('  bool read(%s& ros);\n' % (spec.short_name))
    s.write('  bool removeUnconnectedPorts();\n')

    s.write('};\n\n')

    #
    # output ports
    #
    s.write(
        'class %s_OutputPorts : public interface_ports::OutputPortInterface<%s > {\n'
        % (spec.short_name, spec.short_name))

    s.write('public:\n')
    s.write('  typedef %s Container_;\n' % (spec.short_name))

    s.write('\nprivate:\n')

    s.write(
        '  boost::shared_ptr<interface_ports::OutputPortInterface<%s > > this_container__;\n'
        % (spec.short_name))

    # generate member list
    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        s.write(
            '  boost::shared_ptr<interface_ports::OutputPortInterface<%s > > %s_;\n'
            % (msg_type_to_cpp(field.type), field.name))


#        if field.name in port_spec_dict:
#            s.write('  boost::shared_ptr<interface_ports::OutputPortInterface<%s > > %s_;\n'%(msg_type_to_cpp(field.type), field.name))

    s.write('\npublic:\n')

    s.write(
        '  %s_OutputPorts(RTT::TaskContext *tc, const std::string& prefix="");\n'
        % (spec.short_name))
    s.write('  bool write(const %s& ros);\n' % (spec.short_name))
    s.write('  bool removeUnconnectedPorts();\n')

    s.write('};\n\n')

    s.write('} // namespace %s\n\n' % (spec.package))

    s.write('#endif // %s_SUBSYSTEM_BUFFERS_%s_H\n' %
            (spec.package.upper(), spec.short_name.upper()))
def write_boost_serialization(s, spec, cpp_name_prefix, file):
    """
    Writes the boost::serialize function for a message

    @param s: Stream to write to
    @type s: stream
    @param spec: The message spec
    @type spec: roslib.msgs.MsgSpec
    @param cpp_name_prefix: The C++ prefix to prepend to a message to refer to it (e.g. "std_msgs::")
    @type cpp_name_prefix: str
    """
    (cpp_msg_unqualified, cpp_msg_with_alloc, _) = gencpp.cpp_message_declarations(cpp_name_prefix, spec.short_name)

#    port_spec_dict = get_port_spec_dict(spec)

    s.write("/* Auto-generated by create_subsystem_ports_source.py for file %s */\n"%(file))
    s.write('#include <%s/subsystem_buffers/%s.h>\n'%(spec.package,spec.short_name))
    s.write('#include <common_interfaces/interface_ports.h>\n')

    s.write('namespace %s {\n\n'%(spec.package))

    for field in spec.parsed_fields():
        s.write('// name: %s   type: %s\n'%(field.name, msg_type_to_cpp(field.type)))

    fields_with_validity = []
    validity_fields = []
    # find all validity fields
    for field in spec.parsed_fields():
        for field_valid in spec.parsed_fields():
            if field.name + "_valid" == field_valid.name and field_valid.type == "bool":
                fields_with_validity.append(field.name)
                validity_fields.append(field.name + "_valid")

    #
    # input ports
    #
    s.write('%s_InputPorts::%s_InputPorts(RTT::TaskContext *tc, const std::string& prefix) {\n'%(spec.short_name, spec.short_name))

    s.write('  this_container__.reset( new interface_ports::InputPort<%s >(tc, prefix ) );\n'%(spec.short_name))

    # generate member list
    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        if field.is_builtin:
            s.write('  %s_.reset( new interface_ports::InputPort<%s >(tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\") );\n'%(field.name, msg_type_to_cpp(field.type), field.name))
        else:
            s.write('  %s_ = interface_ports::InputPortInterfaceFactory<%s >::Instance()->Create("%s", tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\");\n'%(field.name, msg_type_to_cpp(field.type)[:-1], field.type, field.name))
            s.write('  if (!%s_) {\n'%(field.name))
            s.write('    %s_.reset( new interface_ports::InputPort<%s >(tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\") );\n'%(field.name, msg_type_to_cpp(field.type), field.name))
            s.write('  }\n')
#        if field.name in port_spec_dict:
#            port_spec = port_spec_dict[field.name]
#            if port_spec[0] == 'container':
#                if field.is_builtin:
#                    raise
#                s.write('  %s_ = interface_ports::InputPortInterfaceFactory<%s >::Instance()->Create("%s", tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\");\n'%(field.name, msg_type_to_cpp(field.type)[:-1], field.type, field.name))
#            elif port_spec[0] == 'port':
#                s.write('  %s_.reset( new interface_ports::InputPort<%s >(tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\") );\n'%(field.name, msg_type_to_cpp(field.type), field.name))
    s.write('}\n\n')

    s.write('bool %s_InputPorts::read(%s& ros) {\n'%(spec.short_name, spec.short_name))
    s.write('  if (!(this_container__ && this_container__->read(ros))) {\n')

    s.write('    bool result = true;\n')
    s.write('    //ros = %s();\n'%(spec.short_name))
    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        if field.name in fields_with_validity:
            left_side = 'ros.' + field.name + '_valid = '
        else:
            left_side = 'result &= '
        s.write('    ' + left_side + '%s_ && %s_->read(ros.%s);\n'%(field.name, field.name, field.name))

#        if field.name in port_spec_dict:
#            port_spec = port_spec_dict[field.name]
#            validity_field = port_spec[1]
#            if validity_field:
#                left_side = 'ros.' + validity_field + ' = '
#            else:
#                left_side = 'result &= '
#            s.write('    ' + left_side + '%s_ && %s_->read(ros.%s);\n'%(field.name, field.name, field.name))
    s.write('    if (!result) {\n')
    s.write('      ros = %s();\n'%(spec.short_name))
    s.write('    }\n')

    s.write('    return result;\n')
    s.write('  }\n')
    s.write('  return true;\n')
    s.write('}\n\n')

    s.write('bool %s_InputPorts::removeUnconnectedPorts() {\n'%(spec.short_name))
    s.write('  bool result = true;\n')
    s.write('  if (this_container__->removeUnconnectedPorts()) {\n')
    s.write('    this_container__.reset();\n')
    s.write('  }\n')
    s.write('  else {\n')
    s.write('    result = false;\n')
    s.write('  }\n')
    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        s.write('  if (%s_->removeUnconnectedPorts()) {\n'%(field.name))
        s.write('    %s_.reset();\n'%(field.name))
        s.write('  }\n')
        s.write('  else {\n')
        s.write('    result = false;\n')
        s.write('  }\n')
#        if field.name in port_spec_dict:
#            port_spec = port_spec_dict[field.name]
#            s.write('  if (%s_->removeUnconnectedPorts()) {\n'%(field.name))
#            s.write('    %s_.reset();\n'%(field.name))
#            s.write('  }\n')
#            s.write('  else {\n')
#            s.write('    result = false;\n')
#            s.write('  }\n')
    s.write('  return result;\n')
    s.write('}\n')

    s.write('REGISTER_InputPortInterface( %s_InputPorts, "%s/%s" );\n\n'%(spec.short_name, spec.package, spec.short_name))

    #
    # output ports
    #
    s.write('%s_OutputPorts::%s_OutputPorts(RTT::TaskContext *tc, const std::string& prefix) {\n'%(spec.short_name, spec.short_name))

    s.write('  this_container__.reset( new interface_ports::OutputPort<%s >(tc, prefix ) );\n'%(spec.short_name))
    # generate member list
    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        if field.is_builtin:
            s.write('  %s_.reset( new interface_ports::OutputPort<%s >(tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\") );\n'%(field.name, msg_type_to_cpp(field.type), field.name))
        else:
            s.write('  %s_ = interface_ports::OutputPortInterfaceFactory<%s >::Instance()->Create("%s", tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\");\n'%(field.name, msg_type_to_cpp(field.type)[:-1], field.type, field.name))
            s.write('  if (!%s_) {\n'%(field.name))
            s.write('    %s_.reset( new interface_ports::OutputPort<%s >(tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\") );\n'%(field.name, msg_type_to_cpp(field.type), field.name))
            s.write('  }\n')

#        if field.name in port_spec_dict:
#            port_spec = port_spec_dict[field.name]
#            if port_spec[0] == 'container':
#                if field.is_builtin:
#                    raise Exception('built in type', "field \'" + field.name + "\' is specified as container, but it is built-in type" )
#                s.write('  %s_ = interface_ports::OutputPortInterfaceFactory<%s >::Instance()->Create("%s", tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\");\n'%(field.name, msg_type_to_cpp(field.type)[:-1], field.type, field.name))
#            elif port_spec[0] == 'port':
#                s.write('  %s_.reset( new interface_ports::OutputPort<%s >(tc, prefix + std::string(prefix.empty()?"":"_") + \"%s\") );\n'%(field.name, msg_type_to_cpp(field.type), field.name))
    s.write('}\n\n')

    s.write('bool %s_OutputPorts::write(const %s& ros) {\n'%(spec.short_name, spec.short_name))

    s.write('  this_container__ && this_container__->write(ros);\n')

    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        if field.name in fields_with_validity:
            s.write('  if (ros.%s_valid) {\n  '%(field.name))
        s.write('    %s_ && %s_->write(ros.%s);\n'%(field.name, field.name, field.name))
        if field.name in fields_with_validity:
            s.write('  }\n')

#        if field.name in port_spec_dict:
#            port_spec = port_spec_dict[field.name]
#            validity_field = port_spec[1]
#            if validity_field:
#                s.write('  if (ros.%s) {\n  '%(validity_field))
#            s.write('    %s_ && %s_->write(ros.%s);\n'%(field.name, field.name, field.name))
#            if validity_field:
#                s.write('  }\n')

    s.write('  return true;\n')
    s.write('}\n')

    s.write('bool %s_OutputPorts::removeUnconnectedPorts() {\n'%(spec.short_name))
    s.write('  bool result = true;\n')
    s.write('  if (this_container__->removeUnconnectedPorts()) {\n')
    s.write('    this_container__.reset();\n')
    s.write('  }\n')
    s.write('  else {\n')
    s.write('    result = false;\n')
    s.write('  }\n')
    for field in spec.parsed_fields():
        if field.name in validity_fields:
            continue
        s.write('  if (%s_->removeUnconnectedPorts()) {\n'%(field.name))
        s.write('    %s_.reset();\n'%(field.name))
        s.write('  }\n')
        s.write('  else {\n')
        s.write('    result = false;\n')
        s.write('  }\n')
    s.write('  return result;\n')
    s.write('}\n')

    s.write('REGISTER_OutputPortInterface( %s_OutputPorts, "%s/%s" );\n\n'%(spec.short_name, spec.package, spec.short_name))

    s.write('} // namespace %s\n\n'%(spec.package))