From 1e84e33cc1be2432f6cfea9a5a13e7cdb1dfbba5 Mon Sep 17 00:00:00 2001 From: "Daniele E. Domenichelli" Date: Thu, 12 Mar 2020 18:19:10 +0100 Subject: [PATCH 1/3] wip-thrift-type --- src/idls/thrift/src/t_yarp_generator.cc | 10 ++++++++++ src/libYARP_dev/src/idl/LaserScan2D.thrift | 2 ++ .../src/idl_generated_code/yarp/dev/LaserScan2D.cpp | 9 +++++++++ .../src/idl_generated_code/yarp/dev/LaserScan2D.h | 7 +++++++ 4 files changed, 28 insertions(+) diff --git a/src/idls/thrift/src/t_yarp_generator.cc b/src/idls/thrift/src/t_yarp_generator.cc index 49c4def5910..2fedc9a84c6 100644 --- a/src/idls/thrift/src/t_yarp_generator.cc +++ b/src/idls/thrift/src/t_yarp_generator.cc @@ -1840,6 +1840,16 @@ void t_yarp_generator::generate_struct(t_struct* tstruct) yarp_api_keyword = annotations.at("yarp.api.keyword"); } + std::string yarp_type_name{}; + if (annotations.find("yarp.type.name") != annotations.end()) { + yarp_api_keyword = annotations.at("yarp.type.name"); + } + + std::string yarp_type_version{}; + if (annotations.find("yarp.type.version") != annotations.end()) { + yarp_api_keyword = annotations.at("yarp.type.version"); + } + // Open header file std::string f_header_name = get_out_dir() + get_include_prefix(program_) + name + ".h"; ofstream_with_content_based_conditional_update f_h_; diff --git a/src/libYARP_dev/src/idl/LaserScan2D.thrift b/src/libYARP_dev/src/idl/LaserScan2D.thrift index 485a4371e7c..a438d61ac31 100644 --- a/src/libYARP_dev/src/idl/LaserScan2D.thrift +++ b/src/libYARP_dev/src/idl/LaserScan2D.thrift @@ -37,4 +37,6 @@ struct LaserScan2D ( yarp.api.include = "yarp/dev/api.h" yarp.api.keyword = "YARP_dev_API" + yarp.type.name = "yarp/LaserScan2D" + yarp.type.version = "1.0" ) diff --git a/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp b/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp index af09769c0bb..94841f89f98 100644 --- a/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp +++ b/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp @@ -82,6 +82,8 @@ bool LaserScan2D::read(yarp::os::ConnectionReader& connection) // Write structure on a Wire bool LaserScan2D::write(const yarp::os::idl::WireWriter& writer) const { + write_type(); + if (!write_angle_min(writer)) { return false; } @@ -121,6 +123,13 @@ std::string LaserScan2D::toString() const return b.toString(); } +yarp::os::Type LaserScan2D::getType() const +{ + yarp::os::Type typ = yarp::os::Type::byNameOnWire(typeName); + type.setVersion(typeVersion); + return typ; +} + // Editor: default constructor LaserScan2D::Editor::Editor() { diff --git a/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.h b/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.h index 6040528dd47..d5f6d6fc865 100644 --- a/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.h +++ b/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.h @@ -79,6 +79,13 @@ class YARP_dev_API LaserScan2D : // If you want to serialize this class without nesting, use this helper typedef yarp::os::idl::Unwrapped unwrapped; + + // The name for this message, ROS will need this + static constexpr const char* typeName = "yarp/LaserScan2D"; + static constexpr const char* typeVersion = "1.0"; + + yarp::os::Type getType() const override; + class Editor : public yarp::os::Wire, public yarp::os::PortWriter From 762873863cc84021539536939b2eed6738f5969b Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Fri, 13 Mar 2020 11:17:08 +0100 Subject: [PATCH 2/3] modified thrift yarp_generator to add name and version to yarp data types --- src/idls/thrift/src/t_yarp_generator.cc | 57 ++++++++++++++++++- .../yarp/dev/LaserScan2D.cpp | 4 +- src/libYARP_os/src/yarp/os/Type.cpp | 28 ++++++++- src/libYARP_os/src/yarp/os/Type.h | 10 ++++ 4 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/idls/thrift/src/t_yarp_generator.cc b/src/idls/thrift/src/t_yarp_generator.cc index 2fedc9a84c6..c987d2eaea7 100644 --- a/src/idls/thrift/src/t_yarp_generator.cc +++ b/src/idls/thrift/src/t_yarp_generator.cc @@ -316,7 +316,9 @@ class t_yarp_generator : public t_oop_generator void generate_struct_read_connectionreader(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); void generate_struct_write_wirereader(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); void generate_struct_write_connectionreader(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); + void generate_struct_typeconstexpr(t_struct* tstruct, std::ostringstream& f_h_, const std::string& type, const std::string& version); void generate_struct_tostring(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); + void generate_struct_gettype(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); void generate_struct_unwrapped_helper(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); void generate_struct_editor(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); void generate_struct_editor_default_constructor(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_); @@ -1842,12 +1844,18 @@ void t_yarp_generator::generate_struct(t_struct* tstruct) std::string yarp_type_name{}; if (annotations.find("yarp.type.name") != annotations.end()) { - yarp_api_keyword = annotations.at("yarp.type.name"); + yarp_type_name = annotations.at("yarp.type.name"); + } + else { + yarp_type_name = "yarp/"+name; } std::string yarp_type_version{}; if (annotations.find("yarp.type.version") != annotations.end()) { - yarp_api_keyword = annotations.at("yarp.type.version"); + yarp_type_version = annotations.at("yarp.type.version"); + } + else { + yarp_type_version = "1.0"; } // Open header file @@ -1884,6 +1892,7 @@ void t_yarp_generator::generate_struct(t_struct* tstruct) f_h_ << "#include \n"; f_h_ << "#include \n"; + f_h_ << "#include \n"; if (need_common_) { f_h_ << '\n'; f_h_ << "#include <" << get_include_prefix(program_) << program_->get_name() << "_common.h>" << '\n'; @@ -1925,7 +1934,9 @@ void t_yarp_generator::generate_struct(t_struct* tstruct) generate_struct_read_connectionreader(tstruct, f_h_, f_cpp_); generate_struct_write_wirereader(tstruct, f_h_, f_cpp_); generate_struct_write_connectionreader(tstruct, f_h_, f_cpp_); + generate_struct_typeconstexpr(tstruct, f_h_, yarp_type_name, yarp_type_version); generate_struct_tostring(tstruct, f_h_, f_cpp_); + generate_struct_gettype(tstruct, f_h_, f_cpp_); generate_struct_unwrapped_helper(tstruct, f_h_, f_cpp_); // Add editor class, if not disabled @@ -2228,6 +2239,48 @@ void t_yarp_generator::generate_struct_tostring(t_struct* tstruct, std::ostrings assert(indent_count_cpp() == 0); } +void t_yarp_generator::generate_struct_typeconstexpr(t_struct* tstruct, std::ostringstream& f_h_, const std::string& yarp_type_name, const std::string& yarp_type_version) +{ + THRIFT_DEBUG_COMMENT(f_h_); + + const auto& name = tstruct->get_name(); + + f_h_ << indent_h() << "//The name and the version for this message\n"; + f_h_ << indent_h() << "static constexpr const char* typeName = \"" << yarp_type_name << "\";\n"; + f_h_ << indent_h() << "static constexpr const char* typeVersion = \"" << "1.0" << "\";\n"; + f_h_ << '\n'; + + assert(indent_count_h() == 1); +} + +void t_yarp_generator::generate_struct_gettype(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_) +{ + THRIFT_DEBUG_COMMENT(f_h_); + THRIFT_DEBUG_COMMENT(f_cpp_); + + const auto& name = tstruct->get_name(); + + f_h_ << indent_h() << "// Get the message type\n"; + f_h_ << indent_h() << "yarp::os::Type getType() const;\n"; + f_h_ << '\n'; + + f_cpp_ << indent_cpp() << "// Convert to a printable string\n"; + f_cpp_ << indent_cpp() << "yarp::os::Type " << name << "::getType() const\n"; + f_cpp_ << indent_cpp() << "{\n"; + indent_up_cpp(); + { + f_cpp_ << indent_cpp() << " yarp::os::Type typ = yarp::os::Type::byNameOnWire(typeName);\n"; + f_cpp_ << indent_cpp() << "typ.setVersion(typeVersion);\n"; + f_cpp_ << indent_cpp() << " return typ;\n"; + } + indent_down_cpp(); + f_cpp_ << indent_cpp() << "}\n"; + f_cpp_ << '\n'; + + assert(indent_count_h() == 1); + assert(indent_count_cpp() == 0); +} + void t_yarp_generator::generate_struct_unwrapped_helper(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_) { THRIFT_DEBUG_COMMENT(f_h_); diff --git a/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp b/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp index 94841f89f98..da68357c3b5 100644 --- a/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp +++ b/src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp @@ -82,7 +82,7 @@ bool LaserScan2D::read(yarp::os::ConnectionReader& connection) // Write structure on a Wire bool LaserScan2D::write(const yarp::os::idl::WireWriter& writer) const { - write_type(); + //write_type(); if (!write_angle_min(writer)) { return false; @@ -126,7 +126,7 @@ std::string LaserScan2D::toString() const yarp::os::Type LaserScan2D::getType() const { yarp::os::Type typ = yarp::os::Type::byNameOnWire(typeName); - type.setVersion(typeVersion); + typ.setVersion(typeVersion); return typ; } diff --git a/src/libYARP_os/src/yarp/os/Type.cpp b/src/libYARP_os/src/yarp/os/Type.cpp index ab9551b67ef..f35fd1902b2 100644 --- a/src/libYARP_os/src/yarp/os/Type.cpp +++ b/src/libYARP_os/src/yarp/os/Type.cpp @@ -84,6 +84,7 @@ class Type::Private Property* prop{nullptr}; std::string name; std::string name_on_wire; + std::string version; }; @@ -108,6 +109,21 @@ Type::~Type() delete mPriv; } +std::string Type::getVersion() const +{ + return mPriv->version; +} + +size_t Type::getMajorVersion() const +{ + return 1; +} + +size_t Type::getMinorVersion() const +{ + return 1; +} + Type& Type::operator=(const Type& rhs) { if (&rhs != this) { @@ -154,9 +170,14 @@ bool Type::hasName() const return !mPriv->name.empty(); } +bool Type::hasVersion() const +{ + return !mPriv->version.empty(); +} + bool Type::isValid() const { - return hasName(); + return hasName() && hasVersion(); } std::string Type::toString() const @@ -170,6 +191,11 @@ std::string Type::toString() const return "null"; } +Type& Type::setVersion(const std::string& version) +{ + mPriv->version = version; + return *this; +} Type Type::byName(const char* name) { diff --git a/src/libYARP_os/src/yarp/os/Type.h b/src/libYARP_os/src/yarp/os/Type.h index 43d4db67b9c..9ef811cd998 100644 --- a/src/libYARP_os/src/yarp/os/Type.h +++ b/src/libYARP_os/src/yarp/os/Type.h @@ -72,8 +72,16 @@ class YARP_os_API Type std::string getNameOnWire() const; + std::string getVersion() const; + + size_t getMajorVersion() const; + + size_t getMinorVersion() const; + bool hasName() const; + bool hasVersion() const; + bool isValid() const; std::string toString() const; @@ -84,6 +92,8 @@ class YARP_os_API Type Type& addProperty(const char* key, const Value& val); + Type& setVersion(const std::string& vesion); + /** @} */ /** @{ */ From 08a3bdb035d282b78e3d5595bd9ae7724e1744b1 Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Fri, 13 Mar 2020 16:30:56 +0100 Subject: [PATCH 3/3] comments addressed --- src/idls/thrift/src/t_yarp_generator.cc | 12 ++++++------ src/libYARP_os/src/yarp/os/Type.cpp | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/idls/thrift/src/t_yarp_generator.cc b/src/idls/thrift/src/t_yarp_generator.cc index c987d2eaea7..e92e101d9cd 100644 --- a/src/idls/thrift/src/t_yarp_generator.cc +++ b/src/idls/thrift/src/t_yarp_generator.cc @@ -1847,7 +1847,7 @@ void t_yarp_generator::generate_struct(t_struct* tstruct) yarp_type_name = annotations.at("yarp.type.name"); } else { - yarp_type_name = "yarp/"+name; + yarp_type_name = ""; } std::string yarp_type_version{}; @@ -1855,7 +1855,7 @@ void t_yarp_generator::generate_struct(t_struct* tstruct) yarp_type_version = annotations.at("yarp.type.version"); } else { - yarp_type_version = "1.0"; + yarp_type_version = ""; } // Open header file @@ -2247,7 +2247,7 @@ void t_yarp_generator::generate_struct_typeconstexpr(t_struct* tstruct, std::ost f_h_ << indent_h() << "//The name and the version for this message\n"; f_h_ << indent_h() << "static constexpr const char* typeName = \"" << yarp_type_name << "\";\n"; - f_h_ << indent_h() << "static constexpr const char* typeVersion = \"" << "1.0" << "\";\n"; + f_h_ << indent_h() << "static constexpr const char* typeVersion = \"" << yarp_type_version << "\";\n"; f_h_ << '\n'; assert(indent_count_h() == 1); @@ -2264,14 +2264,14 @@ void t_yarp_generator::generate_struct_gettype(t_struct* tstruct, std::ostringst f_h_ << indent_h() << "yarp::os::Type getType() const;\n"; f_h_ << '\n'; - f_cpp_ << indent_cpp() << "// Convert to a printable string\n"; + f_cpp_ << indent_cpp() << "// Get the message type\n"; f_cpp_ << indent_cpp() << "yarp::os::Type " << name << "::getType() const\n"; f_cpp_ << indent_cpp() << "{\n"; indent_up_cpp(); { - f_cpp_ << indent_cpp() << " yarp::os::Type typ = yarp::os::Type::byNameOnWire(typeName);\n"; + f_cpp_ << indent_cpp() << "yarp::os::Type typ = yarp::os::Type::byNameOnWire(typeName);\n"; f_cpp_ << indent_cpp() << "typ.setVersion(typeVersion);\n"; - f_cpp_ << indent_cpp() << " return typ;\n"; + f_cpp_ << indent_cpp() << "return typ;\n"; } indent_down_cpp(); f_cpp_ << indent_cpp() << "}\n"; diff --git a/src/libYARP_os/src/yarp/os/Type.cpp b/src/libYARP_os/src/yarp/os/Type.cpp index f35fd1902b2..1e948fa89a1 100644 --- a/src/libYARP_os/src/yarp/os/Type.cpp +++ b/src/libYARP_os/src/yarp/os/Type.cpp @@ -116,12 +116,18 @@ std::string Type::getVersion() const size_t Type::getMajorVersion() const { - return 1; + unsigned int major = 0; + unsigned int minor = 0; + int ret = sscanf(mPriv->version.c_str(),"%u.%u", &major, &minor); + return major; } size_t Type::getMinorVersion() const { - return 1; + unsigned int major = 0; + unsigned int minor = 0; + int ret = sscanf(mPriv->version.c_str(), "%u.%u", &major, &minor); + return minor; } Type& Type::operator=(const Type& rhs) @@ -177,7 +183,11 @@ bool Type::hasVersion() const bool Type::isValid() const { +#if 0 + //this should be tested carefully, it could be a breaking change! return hasName() && hasVersion(); +#endif + return hasName(); } std::string Type::toString() const