Skip to content

Commit

Permalink
Merge pull request #447 from jwillemsen/jwi-usestdstring
Browse files Browse the repository at this point in the history
Use std::string at the RTI Connext DDS side of DDSX11
  • Loading branch information
jwillemsen authored Nov 18, 2024
2 parents 4fe64e3 + bfddd8f commit 4558782
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// --------------------------------------------------------------------

feature(ddsx11, ndds) : ndds_ts_defaults {
ndds_ts_flags += -update typefiles -enableEscapeChar -typeSequenceSuffix RTISeq -language C++98 -namespace -constructor
ndds_ts_flags += -update typefiles -enableEscapeChar -typeSequenceSuffix RTISeq -language C++98 -namespace -useStdString
Define_Custom(TypeSupport) {
}
Define_Custom(DummyTypeSupport) {
Expand Down
90 changes: 51 additions & 39 deletions ddsx11/vendors/ndds/dds/ndds_base_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include "dds/dds_conversion_traits.h"
#include "ace/CDR_Base.h"

/// By default the RTI NDDS::String type manage strings up to 1k
constexpr uint32_t max_string_size () { return 1024; }
/// By default the RTI NDDS::DDS_Char* type for unbounded string members manages strings up to 255 characters
constexpr uint32_t max_member_string_size () { return 255; }

Expand Down Expand Up @@ -123,7 +121,6 @@ namespace DDSX11
public pass_retn<double>
{
};

//@}

/**
Expand Down Expand Up @@ -152,50 +149,34 @@ namespace DDSX11
inline std::string& from_dds<char*, std::string> (std::string& to, char* const & from)
{ if (from) to = from; else to.clear (); return to; }

template <>
inline std::string& to_dds<std::string, std::string> (std::string& to, const std::string& from)
{
to = from;
return to;
}

template <>
inline std::string& from_dds<std::string, std::string> (std::string& to, std::string const & from)
{ to = from; return to; }

template <>
struct traits<std::string>
: public common_traits<std::string, char*>,
public convert_in<std::string, char*>,
public convert_retn<std::string, char const *>
: public common_traits<std::string, std::string>,
public convert_in<std::string, std::string>,
public pass_out_by_ref<std::string>,
public pass_retn<std::string>
{
struct in
{
typedef std::string in_type;
typedef char * dds_in_type;
char dds_value_[max_string_size ()];
typedef std::string dds_in_type;
const std::string& value_;

in (const in_type& v)
{
std::memcpy (this->dds_value_,
v.c_str (),
std::min<std::string::size_type> (v.size ()+1, max_string_size ()));
}
in (const in_type& v) : value_ (v) { }
~in () = default;
operator dds_in_type () { return this->dds_value_; }
};

// special for strings
struct out
{
typedef std::string out_type;
typedef char * dds_out_type;
out_type& value_;
char dds_value_[max_string_size ()];

out (out_type& v) : value_ (v) { }
~out () { this->value_ = this->dds_value_; }
operator dds_out_type () { return this->dds_value_; }
};

struct inout : public out
{
inout (out::out_type& v) : out (v)
{
std::memcpy (this->dds_value_,
this->value_.c_str (),
std::min<std::string::size_type> (this->value_.size ()+1, max_string_size ()));
};
operator out::dds_out_type () { return this->dds_value_; }
operator dds_in_type () { return this->value_; }
operator const char* () { return this->value_.c_str (); }
};
};

Expand Down Expand Up @@ -491,4 +472,35 @@ namespace DDSX11
}
}

namespace DDSX11
{
/// Conversion of DDS::StringSeq to DDS
template<>
inline ::DDS_Native::DDS::StdStringSeq&
to_dds<::DDS_Native::DDS::StdStringSeq, ::DDS::StringSeq> (
::DDS_Native::DDS::StdStringSeq &to, const ::DDS::StringSeq &from)
{
return DDSX11::sequence_to_dds(to, from);
}

/// Conversion of StringSeq from DDS
template<>
inline ::DDS::StringSeq&
from_dds<::DDS_Native::DDS::StdStringSeq, ::DDS::StringSeq> (
::DDS::StringSeq &to, const ::DDS_Native::DDS::StdStringSeq &from)
{
return DDSX11::sequence_from_dds(to, from);
}

template<>
struct traits<::DDS::StringSeq, ::DDS_Native::DDS::StdStringSeq>
: public common_traits<::DDS::StringSeq, ::DDS_Native::DDS::StdStringSeq>
, public convert_in<::DDS::StringSeq, ::DDS_Native::DDS::StdStringSeq>
, public convert_out_by_ref<::DDS::StringSeq, ::DDS_Native::DDS::StdStringSeq>
, public convert_retn<::DDS::StringSeq, ::DDS_Native::DDS::StdStringSeq>
{
};
} // DDSX11


#endif /* DDSX11_IMPL_NDDS_BASE_TRAITS_H_ */
1 change: 1 addition & 0 deletions ddsx11/vendors/ndds/dds/ndds_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace DDS_Native

typedef ::DDS_OctetSeq OctetSeq;
typedef ::DDS_StringSeq StringSeq;
typedef ::DDS_StdStringSeq StdStringSeq;

typedef ::DDS_InstanceHandle_t InstanceHandle_t;
typedef ::DDS_InstanceHandleSeq InstanceHandleSeq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "dds/ndds_typedefs.h"
#include "dds/dds_conversion_traits.h"
#include "dds/ndds_base_traits.h"
#include "idl/ndds_dcps_types_dds_traits.h"
%includes.each do |incfile|
#include "<%= incfile %>"
%end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,31 @@ namespace DDSX11
{
/// Conversion of <%= scoped_cxxname %> to DDS
template<>
inline char*&
to_dds<char*, <%= scoped_cxxtype %>> (
char* &to, const <%= scoped_cxxtype %> &from)
inline std::string&
to_dds<std::string, <%= scoped_cxxtype %>> (
std::string &to, const <%= scoped_cxxtype %> &from)
{
// Always allocate a buffer of max bound
if (to == nullptr) to = DDS_String_alloc (IDL::traits<<%= scoped_cxxtype %>>::bound::value);
// NDDS initialization should have pre-allocated a buffer of the right size
std::strncpy (to, from.c_str (), IDL::traits<<%= scoped_cxxtype %>>::bound::value);
to = from;
return to;
}

/// Conversion of <%= scoped_cxxname %> from DDS
template<>
inline <%= scoped_cxxtype %>&
from_dds<char*, <%= scoped_cxxtype %>> (
<%= scoped_cxxtype %> &to, char* const &from)
from_dds<std::string, <%= scoped_cxxtype %>> (
<%= scoped_cxxtype %> &to, std::string const &from)
{
to = <%= scoped_cxxtype %> (from);
return to;
}

template <>
struct traits<<%= scoped_cxxtype %>>
: public common_traits<<%= scoped_cxxtype %>, char*>
, public convert_in<<%= scoped_cxxtype %>, char*>
, public convert_out_by_ref<<%= scoped_cxxtype %>, char*>
, public convert_retn<<%= scoped_cxxtype %>, char*>
: public common_traits<<%= scoped_cxxtype %>, std::string>
, public convert_in<<%= scoped_cxxtype %>, std::string*>
, public convert_out_by_ref<<%= scoped_cxxtype %>, std::string>
, public convert_retn<<%= scoped_cxxtype %>, std::string>
{
};
} // DDSX11
Expand Down

0 comments on commit 4558782

Please sign in to comment.