Skip to content

Commit 7628738

Browse files
committed
modified thrift yarp_generator to add name and version to yarp data types
1 parent 1e84e33 commit 7628738

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

src/idls/thrift/src/t_yarp_generator.cc

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ class t_yarp_generator : public t_oop_generator
316316
void generate_struct_read_connectionreader(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_);
317317
void generate_struct_write_wirereader(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_);
318318
void generate_struct_write_connectionreader(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_);
319+
void generate_struct_typeconstexpr(t_struct* tstruct, std::ostringstream& f_h_, const std::string& type, const std::string& version);
319320
void generate_struct_tostring(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_);
321+
void generate_struct_gettype(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_);
320322
void generate_struct_unwrapped_helper(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_);
321323
void generate_struct_editor(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_);
322324
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)
18421844

18431845
std::string yarp_type_name{};
18441846
if (annotations.find("yarp.type.name") != annotations.end()) {
1845-
yarp_api_keyword = annotations.at("yarp.type.name");
1847+
yarp_type_name = annotations.at("yarp.type.name");
1848+
}
1849+
else {
1850+
yarp_type_name = "yarp/"+name;
18461851
}
18471852

18481853
std::string yarp_type_version{};
18491854
if (annotations.find("yarp.type.version") != annotations.end()) {
1850-
yarp_api_keyword = annotations.at("yarp.type.version");
1855+
yarp_type_version = annotations.at("yarp.type.version");
1856+
}
1857+
else {
1858+
yarp_type_version = "1.0";
18511859
}
18521860

18531861
// Open header file
@@ -1884,6 +1892,7 @@ void t_yarp_generator::generate_struct(t_struct* tstruct)
18841892

18851893
f_h_ << "#include <yarp/os/Wire.h>\n";
18861894
f_h_ << "#include <yarp/os/idl/WireTypes.h>\n";
1895+
f_h_ << "#include <yarp/os/Type.h>\n";
18871896
if (need_common_) {
18881897
f_h_ << '\n';
18891898
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)
19251934
generate_struct_read_connectionreader(tstruct, f_h_, f_cpp_);
19261935
generate_struct_write_wirereader(tstruct, f_h_, f_cpp_);
19271936
generate_struct_write_connectionreader(tstruct, f_h_, f_cpp_);
1937+
generate_struct_typeconstexpr(tstruct, f_h_, yarp_type_name, yarp_type_version);
19281938
generate_struct_tostring(tstruct, f_h_, f_cpp_);
1939+
generate_struct_gettype(tstruct, f_h_, f_cpp_);
19291940
generate_struct_unwrapped_helper(tstruct, f_h_, f_cpp_);
19301941

19311942
// Add editor class, if not disabled
@@ -2228,6 +2239,48 @@ void t_yarp_generator::generate_struct_tostring(t_struct* tstruct, std::ostrings
22282239
assert(indent_count_cpp() == 0);
22292240
}
22302241

2242+
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)
2243+
{
2244+
THRIFT_DEBUG_COMMENT(f_h_);
2245+
2246+
const auto& name = tstruct->get_name();
2247+
2248+
f_h_ << indent_h() << "//The name and the version for this message\n";
2249+
f_h_ << indent_h() << "static constexpr const char* typeName = \"" << yarp_type_name << "\";\n";
2250+
f_h_ << indent_h() << "static constexpr const char* typeVersion = \"" << "1.0" << "\";\n";
2251+
f_h_ << '\n';
2252+
2253+
assert(indent_count_h() == 1);
2254+
}
2255+
2256+
void t_yarp_generator::generate_struct_gettype(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_)
2257+
{
2258+
THRIFT_DEBUG_COMMENT(f_h_);
2259+
THRIFT_DEBUG_COMMENT(f_cpp_);
2260+
2261+
const auto& name = tstruct->get_name();
2262+
2263+
f_h_ << indent_h() << "// Get the message type\n";
2264+
f_h_ << indent_h() << "yarp::os::Type getType() const;\n";
2265+
f_h_ << '\n';
2266+
2267+
f_cpp_ << indent_cpp() << "// Convert to a printable string\n";
2268+
f_cpp_ << indent_cpp() << "yarp::os::Type " << name << "::getType() const\n";
2269+
f_cpp_ << indent_cpp() << "{\n";
2270+
indent_up_cpp();
2271+
{
2272+
f_cpp_ << indent_cpp() << " yarp::os::Type typ = yarp::os::Type::byNameOnWire(typeName);\n";
2273+
f_cpp_ << indent_cpp() << "typ.setVersion(typeVersion);\n";
2274+
f_cpp_ << indent_cpp() << " return typ;\n";
2275+
}
2276+
indent_down_cpp();
2277+
f_cpp_ << indent_cpp() << "}\n";
2278+
f_cpp_ << '\n';
2279+
2280+
assert(indent_count_h() == 1);
2281+
assert(indent_count_cpp() == 0);
2282+
}
2283+
22312284
void t_yarp_generator::generate_struct_unwrapped_helper(t_struct* tstruct, std::ostringstream& f_h_, std::ostringstream& f_cpp_)
22322285
{
22332286
THRIFT_DEBUG_COMMENT(f_h_);

src/libYARP_dev/src/idl_generated_code/yarp/dev/LaserScan2D.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool LaserScan2D::read(yarp::os::ConnectionReader& connection)
8282
// Write structure on a Wire
8383
bool LaserScan2D::write(const yarp::os::idl::WireWriter& writer) const
8484
{
85-
write_type();
85+
//write_type();
8686

8787
if (!write_angle_min(writer)) {
8888
return false;
@@ -126,7 +126,7 @@ std::string LaserScan2D::toString() const
126126
yarp::os::Type LaserScan2D::getType() const
127127
{
128128
yarp::os::Type typ = yarp::os::Type::byNameOnWire(typeName);
129-
type.setVersion(typeVersion);
129+
typ.setVersion(typeVersion);
130130
return typ;
131131
}
132132

src/libYARP_os/src/yarp/os/Type.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class Type::Private
8484
Property* prop{nullptr};
8585
std::string name;
8686
std::string name_on_wire;
87+
std::string version;
8788
};
8889

8990

@@ -108,6 +109,21 @@ Type::~Type()
108109
delete mPriv;
109110
}
110111

112+
std::string Type::getVersion() const
113+
{
114+
return mPriv->version;
115+
}
116+
117+
size_t Type::getMajorVersion() const
118+
{
119+
return 1;
120+
}
121+
122+
size_t Type::getMinorVersion() const
123+
{
124+
return 1;
125+
}
126+
111127
Type& Type::operator=(const Type& rhs)
112128
{
113129
if (&rhs != this) {
@@ -154,9 +170,14 @@ bool Type::hasName() const
154170
return !mPriv->name.empty();
155171
}
156172

173+
bool Type::hasVersion() const
174+
{
175+
return !mPriv->version.empty();
176+
}
177+
157178
bool Type::isValid() const
158179
{
159-
return hasName();
180+
return hasName() && hasVersion();
160181
}
161182

162183
std::string Type::toString() const
@@ -170,6 +191,11 @@ std::string Type::toString() const
170191
return "null";
171192
}
172193

194+
Type& Type::setVersion(const std::string& version)
195+
{
196+
mPriv->version = version;
197+
return *this;
198+
}
173199

174200
Type Type::byName(const char* name)
175201
{

src/libYARP_os/src/yarp/os/Type.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@ class YARP_os_API Type
7272

7373
std::string getNameOnWire() const;
7474

75+
std::string getVersion() const;
76+
77+
size_t getMajorVersion() const;
78+
79+
size_t getMinorVersion() const;
80+
7581
bool hasName() const;
7682

83+
bool hasVersion() const;
84+
7785
bool isValid() const;
7886

7987
std::string toString() const;
@@ -84,6 +92,8 @@ class YARP_os_API Type
8492

8593
Type& addProperty(const char* key, const Value& val);
8694

95+
Type& setVersion(const std::string& vesion);
96+
8797
/** @} */
8898
/** @{ */
8999

0 commit comments

Comments
 (0)