From 10c3924b1e20e57b25b0c7f216bf497fbe055c8d Mon Sep 17 00:00:00 2001 From: Iker Luengo Date: Wed, 1 Sep 2021 10:23:46 +0200 Subject: [PATCH 1/6] Operators are inherited Although these lines are legal, these 'using' structures get a parsing error in SWIG. And since the operators are already inherited from the base class, they are redundant, and can be removed to simplify our lives in the binding process Signed-off-by: Iker Luengo --- include/fastdds/dds/topic/TypeSupport.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/fastdds/dds/topic/TypeSupport.hpp b/include/fastdds/dds/topic/TypeSupport.hpp index 12bfd6cf593..7dc8fb9ab9c 100644 --- a/include/fastdds/dds/topic/TypeSupport.hpp +++ b/include/fastdds/dds/topic/TypeSupport.hpp @@ -54,9 +54,6 @@ class TypeSupport : public std::shared_ptr using ReturnCode_t = eprosima::fastrtps::types::ReturnCode_t; using Base = std::shared_ptr; - using Base::operator ->; - using Base::operator *; - using Base::operator bool; /** * @brief Constructor From 989b999b753dccd5f2d4a64f3753e8e3f768cf15 Mon Sep 17 00:00:00 2001 From: Iker Luengo Date: Tue, 7 Sep 2021 12:46:25 +0200 Subject: [PATCH 2/6] Define operators inside the classes This facilitates the binding to Python Signed-off-by: Iker Luengo --- include/fastdds/dds/topic/TypeSupport.hpp | 1 + include/fastdds/rtps/common/EntityId_t.hpp | 97 ++- include/fastdds/rtps/common/Guid.h | 101 ++- include/fastdds/rtps/common/InstanceHandle.h | 64 +- include/fastdds/rtps/common/Time_t.h | 670 ++++++++----------- 5 files changed, 390 insertions(+), 543 deletions(-) diff --git a/include/fastdds/dds/topic/TypeSupport.hpp b/include/fastdds/dds/topic/TypeSupport.hpp index 7dc8fb9ab9c..2a7de0b21fe 100644 --- a/include/fastdds/dds/topic/TypeSupport.hpp +++ b/include/fastdds/dds/topic/TypeSupport.hpp @@ -20,6 +20,7 @@ #define _FASTDDS_TYPE_SUPPORT_HPP_ #include +#include #include #include diff --git a/include/fastdds/rtps/common/EntityId_t.hpp b/include/fastdds/rtps/common/EntityId_t.hpp index 6fe9e6bdd70..9a628ddd38c 100644 --- a/include/fastdds/rtps/common/EntityId_t.hpp +++ b/include/fastdds/rtps/common/EntityId_t.hpp @@ -177,72 +177,67 @@ struct RTPS_DllAPI EntityId_t return EntityId_t(); } -}; - #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC -/** - * Guid prefix comparison operator - * @param id1 EntityId to compare - * @param id2 ID prefix to compare - * @return True if equal - */ -inline bool operator ==( - EntityId_t& id1, - const uint32_t id2) -{ -#if !FASTDDS_IS_BIG_ENDIAN_TARGET - id1.reverse(); -#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET - const bool result = 0 == memcmp(id1.value, &id2, sizeof(id2)); -#if !FASTDDS_IS_BIG_ENDIAN_TARGET - id1.reverse(); -#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET - return result; -} + /** + * Guid prefix comparison operator + * @param ID prefix to compare + * @return True if equal + */ + inline bool operator ==( + const uint32_t other) + { + #if !FASTDDS_IS_BIG_ENDIAN_TARGET + reverse(); + #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET + const bool result = 0 == memcmp(value, &other, sizeof(other)); + #if !FASTDDS_IS_BIG_ENDIAN_TARGET + reverse(); + #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET + return result; + } -/** - * Guid prefix comparison operator - * @param id1 First EntityId to compare - * @param id2 Second EntityId to compare - * @return True if equal - */ -inline bool operator ==( - const EntityId_t& id1, - const EntityId_t& id2) -{ - for (uint8_t i = 0; i < 4; ++i) + /** + * Guid prefix comparison operator + * @param other Second EntityId to compare + * @return True if equal + */ + inline bool operator ==( + const EntityId_t& other) const { - if (id1.value[i] != id2.value[i]) + for (uint8_t i = 0; i < 4; ++i) { - return false; + if (value[i] != other.value[i]) + { + return false; + } } + return true; } - return true; -} -/** - * Guid prefix comparison operator - * @param id1 First EntityId to compare - * @param id2 Second EntityId to compare - * @return True if not equal - */ -inline bool operator !=( - const EntityId_t& id1, - const EntityId_t& id2) -{ - for (uint8_t i = 0; i < 4; ++i) + /** + * Guid prefix comparison operator + * @param other Second EntityId to compare + * @return True if not equal + */ + inline bool operator !=( + const EntityId_t& other) const { - if (id1.value[i] != id2.value[i]) + for (uint8_t i = 0; i < 4; ++i) { - return true; + if (value[i] != other.value[i]) + { + return true; + } } + return false; } - return false; -} #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC +}; + + inline std::ostream& operator <<( std::ostream& output, const EntityId_t& enI) diff --git a/include/fastdds/rtps/common/Guid.h b/include/fastdds/rtps/common/Guid.h index 4fdb35a28e1..dac89b2f226 100644 --- a/include/fastdds/rtps/common/Guid.h +++ b/include/fastdds/rtps/common/Guid.h @@ -122,81 +122,76 @@ struct RTPS_DllAPI GUID_t return *reinterpret_cast(this); } -}; - #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC -/** - * GUID comparison operator - * @param g1 First GUID to compare - * @param g2 Second GUID to compare - * @return True if equal - */ -inline bool operator ==( - const GUID_t& g1, - const GUID_t& g2) -{ - if (g1.guidPrefix == g2.guidPrefix && g1.entityId == g2.entityId) - { - return true; - } - else - { - return false; - } -} - -/** - * GUID comparison operator - * @param g1 First GUID to compare - * @param g2 Second GUID to compare - * @return True if not equal - */ -inline bool operator !=( - const GUID_t& g1, - const GUID_t& g2) -{ - if (g1.guidPrefix != g2.guidPrefix || g1.entityId != g2.entityId) - { - return true; - } - else + /** + * GUID comparison operator + * @param other Second GUID to compare + * @return True if equal + */ + inline bool operator ==( + const GUID_t& other) const { - return false; + if (guidPrefix == other.guidPrefix && entityId == other.entityId) + { + return true; + } + else + { + return false; + } } -} -inline bool operator <( - const GUID_t& g1, - const GUID_t& g2) -{ - for (uint8_t i = 0; i < 12; ++i) + /** + * GUID comparison operator + * @param other Second GUID to compare + * @return True if not equal + */ + inline bool operator !=( + const GUID_t& other) const { - if (g1.guidPrefix.value[i] < g2.guidPrefix.value[i]) + if (guidPrefix != other.guidPrefix || entityId != other.entityId) { return true; } - else if (g1.guidPrefix.value[i] > g2.guidPrefix.value[i]) + else { return false; } } - for (uint8_t i = 0; i < 4; ++i) + + inline bool operator <( + const GUID_t& other) const { - if (g1.entityId.value[i] < g2.entityId.value[i]) + for (uint8_t i = 0; i < 12; ++i) { - return true; + if (guidPrefix.value[i] < other.guidPrefix.value[i]) + { + return true; + } + else if (guidPrefix.value[i] > other.guidPrefix.value[i]) + { + return false; + } } - else if (g1.entityId.value[i] > g2.entityId.value[i]) + for (uint8_t i = 0; i < 4; ++i) { - return false; + if (entityId.value[i] < other.entityId.value[i]) + { + return true; + } + else if (entityId.value[i] > other.entityId.value[i]) + { + return false; + } } + return false; } - return false; -} #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC +}; + const GUID_t c_Guid_Unknown; #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC diff --git a/include/fastdds/rtps/common/InstanceHandle.h b/include/fastdds/rtps/common/InstanceHandle.h index 37fa6a34cb4..52e35219756 100644 --- a/include/fastdds/rtps/common/InstanceHandle.h +++ b/include/fastdds/rtps/common/InstanceHandle.h @@ -126,41 +126,50 @@ struct RTPS_DllAPI InstanceHandle_t return *reinterpret_cast(this); } -}; - -const InstanceHandle_t c_InstanceHandle_Unknown; + /** + * Strictly less than operator + * @param other The other InstanceHandle_t to compare + * @return True if the value of this handle lexicographically preceeds that of \p other + */ + inline bool operator <( + const InstanceHandle_t& other) const + { + return memcmp(value, other.value, 16) < 0; + } #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC -/** - * Comparison operator - * @param ihandle1 First InstanceHandle_t to compare - * @param ihandle2 Second InstanceHandle_t to compare - * @return True if equal - */ -inline bool operator ==( - const InstanceHandle_t& ihandle1, - const InstanceHandle_t& ihandle2) -{ - for (uint8_t i = 0; i < 16; ++i) + /** + * Comparison operator + * @param other Second InstanceHandle_t to compare + * @return True if equal + */ + inline bool operator ==( + const InstanceHandle_t& other) const { - if (ihandle1.value[i] != ihandle2.value[i]) + for (uint8_t i = 0; i < 16; ++i) { - return false; + if (value[i] != other.value[i]) + { + return false; + } } + return true; } - return true; -} -inline bool operator !=( - const InstanceHandle_t& ihandle1, - const InstanceHandle_t& ihandle2) -{ - return !(ihandle1 == ihandle2); -} + inline bool operator !=( + const InstanceHandle_t& other) const + { + return !(*this == other); + } #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC +}; + +const InstanceHandle_t c_InstanceHandle_Unknown; + + /** * Convert InstanceHandle_t to GUID * @param guid GUID to store the results @@ -207,13 +216,6 @@ inline GUID_t iHandle2GUID( return guid; } -inline bool operator <( - const InstanceHandle_t& h1, - const InstanceHandle_t& h2) -{ - return memcmp(h1.value, h2.value, 16) < 0; -} - #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC /** diff --git a/include/fastdds/rtps/common/Time_t.h b/include/fastdds/rtps/common/Time_t.h index 6620c7c009b..4c3b20573e6 100644 --- a/include/fastdds/rtps/common/Time_t.h +++ b/include/fastdds/rtps/common/Time_t.h @@ -73,10 +73,159 @@ struct RTPS_DllAPI Time_t */ static void now( Time_t& ret); + + +#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC + + /** + * Comparison assignment + * @param other Second Time_t to compare + * @return True if equal + */ + inline bool operator ==( + const Time_t& other) const + { + if (seconds != other.seconds) + { + return false; + } + if (nanosec != other.nanosec) + { + return false; + } + return true; + } + + /** + * Comparison assignment + * @param other Second Time_t to compare + * @return True if not equal + */ + inline bool operator !=( + const Time_t& other) const + { + return !(*this == other); + } + + /** + * Checks if a Time_t is less than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is less than the second + */ + inline bool operator <( + const Time_t& other) const + { + if (seconds < other.seconds) + { + return true; + } + else if (seconds > other.seconds) + { + return false; + } + else + { + if (nanosec < other.nanosec) + { + return true; + } + else + { + return false; + } + } + } + + /** + * Checks if a Time_t is greater than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is greater than the second + */ + inline bool operator >( + const Time_t& other) const + { + return other < *this; + } + + /** + * Checks if a Time_t is less or equal than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is less or equal than the second + */ + inline bool operator <=( + const Time_t& other) const + { + return !(*this > other); + } + + /** + * Checks if a Time_t is greater or equal than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is greater or equal than the second + */ + inline bool operator >=( + const Time_t& other) const + { + return !(*this < other); + } + + /** + * Adds two Time_t. + * @param other Second Time_t to add + * @return A new Time_t with the result. + */ + inline Time_t operator +( + const Time_t& other) const + { + Time_t result(seconds + other.seconds, nanosec + other.nanosec); + if (result.nanosec < nanosec) // Overflow is detected by any of them + { + ++result.seconds; + } + return result; + } + + /** + * Subtracts two Time_t. + * @param other Second Time_t to subtract + * @return A new Time_t with the result. + */ + inline Time_t operator -( + const Time_t& other) const + { + Time_t result(seconds - other.seconds, nanosec - other.nanosec); + if (result.nanosec > nanosec) // Overflow is detected by ta + { + --result.seconds; + } + return result; + } + +#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC + }; using Duration_t = Time_t; +#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC + +inline std::ostream& operator <<( + std::ostream& output, + const Time_t& t) +{ + long double t_aux = t.seconds + (((long double)t.nanosec) / 1000000000ULL); + return output << t_aux; +} + +#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC + +//! Time_t (Duration_t) representing an infinite time. DONT USE IT IN CONSTRUCTORS +const Time_t c_TimeInfinite{TIME_T_INFINITE_SECONDS, TIME_T_INFINITE_NANOSECONDS}; +//! Time_t (Duration_t) representing a zero time. DONT USE IT IN CONSTRUCTORS +const Time_t c_TimeZero{0, 0}; +//! Time_t (Duration_t) representing an invalid time. DONT USE IT IN CONSTRUCTORS +const Time_t c_TimeInvalid{-1, TIME_T_INFINITE_NANOSECONDS}; + namespace rtps { /** @@ -172,191 +321,153 @@ class RTPS_DllAPI Time_t static void now( Time_t& ret); -private: - - //!Seconds - int32_t seconds_ = 0; - - //!Fraction of second (1 fraction = 1/(2^32) seconds) - uint32_t fraction_ = 0; - - //!Nanoseconds - uint32_t nanosec_ = 0; - - void set_fraction( - uint32_t frac); - - void set_nanosec( - uint32_t nanos); -}; - #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC -/** - * Comparison assignment - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if equal - */ -static inline bool operator ==( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds() != t2.seconds()) - { - return false; - } - if (t1.fraction() != t2.fraction()) - { - return false; - } - return true; -} - -/** - * Comparison assignment - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if not equal - */ -static inline bool operator !=( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds() != t2.seconds()) - { - return true; - } - if (t1.fraction() != t2.fraction()) - { - return true; - } - return false; -} - -/** - * Checks if a Time_t is less than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is less than the second - */ -static inline bool operator <( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds() < t2.seconds()) - { - return true; - } - else if (t1.seconds() > t2.seconds()) - { - return false; - } - else + /** + * Comparison assignment + * @param other Second Time_t to compare + * @return True if equal + */ + inline bool operator ==( + const Time_t& other) const { - if (t1.fraction() < t2.fraction()) + if (seconds() != other.seconds()) { - return true; + return false; } - else + if (fraction() != other.fraction()) { return false; } - } -} - -/** - * Checks if a Time_t is greater than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is greater than the second - */ -static inline bool operator >( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds() > t2.seconds()) - { return true; } - else if (t1.seconds() < t2.seconds()) + + /** + * Comparison assignment + * @param other Second Time_t to compare + * @return True if not equal + */ + inline bool operator !=( + const Time_t& other) const { - return false; + return !(*this == other); } - else + + /** + * Checks if a Time_t is less than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is less than the second + */ + inline bool operator <( + const Time_t& other) const { - if (t1.fraction() > t2.fraction()) + if (seconds() < other.seconds()) { return true; } - else + else if (seconds() > other.seconds()) { return false; } - } -} - -/** - * Checks if a Time_t is less or equal than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is less or equal than the second - */ -static inline bool operator <=( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds() < t2.seconds()) - { - return true; - } - else if (t1.seconds() > t2.seconds()) - { - return false; - } - else - { - if (t1.fraction() <= t2.fraction()) - { - return true; - } else { - return false; + if (fraction() < other.fraction()) + { + return true; + } + else + { + return false; + } } } -} -/** - * Checks if a Time_t is greater or equal than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is greater or equal than the second - */ -static inline bool operator >=( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds() > t2.seconds()) + /** + * Checks if a Time_t is greater than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is greater than the second + */ + inline bool operator >( + const Time_t& other) const { - return true; + return other < *this; + } + + /** + * Checks if a Time_t is less or equal than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is less or equal than the second + */ + inline bool operator <=( + const Time_t& other) const + { + return !(*this > other); } - else if (t1.seconds() < t2.seconds()) + + /** + * Checks if a Time_t is greater or equal than other. + * @param other Second Time_t to compare + * @return True if the first Time_t is greater or equal than the second + */ + inline bool operator >=( + const Time_t& other) const { - return false; + return !(*this < other); } - else + + /** + * Adds two Time_t. + * @param other Second Time_t to add + * @return A new Time_t with the result. + */ + inline Time_t operator +( + const Time_t& other) const { - if (t1.fraction() >= t2.fraction()) + Time_t result(seconds() + other.seconds(), fraction() + other.fraction()); + if (result.fraction() < fraction()) // Overflow is detected by any of them { - return true; + ++result.seconds(); } - else + return result; + } + + /** + * Subtracts two Time_t. + * @param other Second Time_t to subtract + * @return A new Time_t with the result. + */ + inline Time_t operator -( + const Time_t& other) const + { + Time_t result(seconds() - other.seconds(), fraction() - other.fraction()); + if (result.fraction() > fraction()) // Overflow is detected by ta { - return false; + --result.seconds(); } + return result; } -} + +#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC + +private: + + //!Seconds + int32_t seconds_ = 0; + + //!Fraction of second (1 fraction = 1/(2^32) seconds) + uint32_t fraction_ = 0; + + //!Nanoseconds + uint32_t nanosec_ = 0; + + void set_fraction( + uint32_t frac); + + void set_nanosec( + uint32_t nanos); +}; + +#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC inline std::ostream& operator <<( std::ostream& output, @@ -403,42 +514,6 @@ inline std::istream& operator >>( return input; } -/** - * Adds two Time_t. - * @param ta First Time_t to add - * @param tb Second Time_t to add - * @return A new Time_t with the result. - */ -static inline Time_t operator +( - const Time_t& ta, - const Time_t& tb) -{ - Time_t result(ta.seconds() + tb.seconds(), ta.fraction() + tb.fraction()); - if (result.fraction() < ta.fraction()) // Overflow is detected by any of them - { - ++result.seconds(); - } - return result; -} - -/** - * Subtracts two Time_t. - * @param ta First Time_t to subtract - * @param tb Second Time_t to subtract - * @return A new Time_t with the result. - */ -static inline Time_t operator -( - const Time_t& ta, - const Time_t& tb) -{ - Time_t result(ta.seconds() - tb.seconds(), ta.fraction() - tb.fraction()); - if (result.fraction() > ta.fraction()) // Overflow is detected by ta - { - --result.seconds(); - } - return result; -} - #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC const Time_t c_RTPSTimeInfinite{0x7fffffff, 0xffffffff}; @@ -447,227 +522,6 @@ const Time_t c_RTPSTimeInvalid{-1, 0xffffffff}; } // namespace rtps -#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - -/** - * Comparison assignment - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if equal - */ -static inline bool operator ==( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds != t2.seconds) - { - return false; - } - if (t1.nanosec != t2.nanosec) - { - return false; - } - return true; -} - -/** - * Comparison assignment - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if not equal - */ -static inline bool operator !=( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds != t2.seconds) - { - return true; - } - if (t1.nanosec != t2.nanosec) - { - return true; - } - return false; -} - -/** - * Checks if a Time_t is less than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is less than the second - */ -static inline bool operator <( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds < t2.seconds) - { - return true; - } - else if (t1.seconds > t2.seconds) - { - return false; - } - else - { - if (t1.nanosec < t2.nanosec) - { - return true; - } - else - { - return false; - } - } -} - -/** - * Checks if a Time_t is greater than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is greater than the second - */ -static inline bool operator >( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds > t2.seconds) - { - return true; - } - else if (t1.seconds < t2.seconds) - { - return false; - } - else - { - if (t1.nanosec > t2.nanosec) - { - return true; - } - else - { - return false; - } - } -} - -/** - * Checks if a Time_t is less or equal than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is less or equal than the second - */ -static inline bool operator <=( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds < t2.seconds) - { - return true; - } - else if (t1.seconds > t2.seconds) - { - return false; - } - else - { - if (t1.nanosec <= t2.nanosec) - { - return true; - } - else - { - return false; - } - } -} - -/** - * Checks if a Time_t is greater or equal than other. - * @param t1 First Time_t to compare - * @param t2 Second Time_t to compare - * @return True if the first Time_t is greater or equal than the second - */ -static inline bool operator >=( - const Time_t& t1, - const Time_t& t2) -{ - if (t1.seconds > t2.seconds) - { - return true; - } - else if (t1.seconds < t2.seconds) - { - return false; - } - else - { - if (t1.nanosec >= t2.nanosec) - { - return true; - } - else - { - return false; - } - } -} - -inline std::ostream& operator <<( - std::ostream& output, - const Time_t& t) -{ - long double t_aux = t.seconds + (((long double)t.nanosec) / 1000000000ULL); - return output << t_aux; -} - -/** - * Adds two Time_t. - * @param ta First Time_t to add - * @param tb Second Time_t to add - * @return A new Time_t with the result. - */ -static inline Time_t operator +( - const Time_t& ta, - const Time_t& tb) -{ - Time_t result(ta.seconds + tb.seconds, ta.nanosec + tb.nanosec); - if (result.nanosec < ta.nanosec) // Overflow is detected by any of them - { - ++result.seconds; - } - return result; -} - -/** - * Subtracts two Time_t. - * @param ta First Time_t to subtract - * @param tb Second Time_t to subtract - * @return A new Time_t with the result. - */ -static inline Time_t operator -( - const Time_t& ta, - const Time_t& tb) -{ - Time_t result(ta.seconds - tb.seconds, ta.nanosec - tb.nanosec); - if (result.nanosec > ta.nanosec) // Overflow is detected by ta - { - --result.seconds; - } - return result; -} - -#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - -//! Time_t (Duration_t) representing an infinite time. DONT USE IT IN CONSTRUCTORS -const Time_t c_TimeInfinite{TIME_T_INFINITE_SECONDS, TIME_T_INFINITE_NANOSECONDS}; -//! Time_t (Duration_t) representing a zero time. DONT USE IT IN CONSTRUCTORS -const Time_t c_TimeZero{0, 0}; -//! Time_t (Duration_t) representing an invalid time. DONT USE IT IN CONSTRUCTORS -const Time_t c_TimeInvalid{-1, TIME_T_INFINITE_NANOSECONDS}; - } // namespace fastrtps } // namespace eprosima From 62572a3a00baaa99008174024eca07e015b31d39 Mon Sep 17 00:00:00 2001 From: Iker Luengo Date: Tue, 14 Sep 2021 16:47:18 +0200 Subject: [PATCH 3/6] Requested changes Signed-off-by: Iker Luengo --- include/fastdds/dds/topic/InstanceHandle.hpp | 37 ++++++++ include/fastdds/dds/topic/TypeSupport.hpp | 8 +- include/fastdds/rtps/common/EntityId_t.hpp | 98 ++++++++++---------- include/fastdds/rtps/common/Guid.h | 18 +--- include/fastdds/rtps/common/Time_t.h | 2 +- 5 files changed, 89 insertions(+), 74 deletions(-) create mode 100644 include/fastdds/dds/topic/InstanceHandle.hpp diff --git a/include/fastdds/dds/topic/InstanceHandle.hpp b/include/fastdds/dds/topic/InstanceHandle.hpp new file mode 100644 index 00000000000..bd8854c5600 --- /dev/null +++ b/include/fastdds/dds/topic/InstanceHandle.hpp @@ -0,0 +1,37 @@ +// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file InstanceHandle.hpp + * + */ + +#ifndef _FASTDDS_DDS_INSTANCE_HANDLE_HPP_ +#define _FASTDDS_DDS_INSTANCE_HANDLE_HPP_ + +#include + +namespace eprosima { +namespace fastdds { +namespace dds { + +using InstanceHandle_t = eprosima::fastrtps::rtps::InstanceHandle_t; + +extern RTPS_DllAPI const InstanceHandle_t HANDLE_NIL; + +} // namespace dds +} // namespace fastdds +} // namespace eprosima + +#endif // _FASTDDS_DDS_INSTANCE_HANDLE_HPP_ diff --git a/include/fastdds/dds/topic/TypeSupport.hpp b/include/fastdds/dds/topic/TypeSupport.hpp index 2a7de0b21fe..2cf7e7471e8 100644 --- a/include/fastdds/dds/topic/TypeSupport.hpp +++ b/include/fastdds/dds/topic/TypeSupport.hpp @@ -20,7 +20,7 @@ #define _FASTDDS_TYPE_SUPPORT_HPP_ #include -#include +#include #include #include @@ -32,12 +32,6 @@ namespace eprosima { namespace fastdds { namespace dds { -//! Handle to identiy different instances of the same Topic of a certain type. -using InstanceHandle_t = eprosima::fastrtps::rtps::InstanceHandle_t; - -//! The NIL instance handle. -extern RTPS_DllAPI const InstanceHandle_t HANDLE_NIL; - class DomainParticipant; /** diff --git a/include/fastdds/rtps/common/EntityId_t.hpp b/include/fastdds/rtps/common/EntityId_t.hpp index 9a628ddd38c..de5ed5588ca 100644 --- a/include/fastdds/rtps/common/EntityId_t.hpp +++ b/include/fastdds/rtps/common/EntityId_t.hpp @@ -177,67 +177,65 @@ struct RTPS_DllAPI EntityId_t return EntityId_t(); } +}; + #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - /** - * Guid prefix comparison operator - * @param ID prefix to compare - * @return True if equal - */ - inline bool operator ==( - const uint32_t other) - { - #if !FASTDDS_IS_BIG_ENDIAN_TARGET - reverse(); - #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET - const bool result = 0 == memcmp(value, &other, sizeof(other)); - #if !FASTDDS_IS_BIG_ENDIAN_TARGET - reverse(); - #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET - return result; - } +/** + * Guid prefix comparison operator + * @param id1 EntityId to compare + * @param id2 ID prefix to compare + * @return True if equal + */ +inline bool operator ==( + EntityId_t& id1, + const uint32_t id2) +{ +#if !FASTDDS_IS_BIG_ENDIAN_TARGET + id1.reverse(); +#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET + const bool result = 0 == memcmp(id1.value, &id2, sizeof(id2)); +#if !FASTDDS_IS_BIG_ENDIAN_TARGET + id1.reverse(); +#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET + return result; +} - /** - * Guid prefix comparison operator - * @param other Second EntityId to compare - * @return True if equal - */ - inline bool operator ==( - const EntityId_t& other) const +/** + * Guid prefix comparison operator + * @param id1 First EntityId to compare + * @param id2 Second EntityId to compare + * @return True if equal + */ +inline bool operator ==( + const EntityId_t& id1, + const EntityId_t& id2) +{ + for (uint8_t i = 0; i < 4; ++i) { - for (uint8_t i = 0; i < 4; ++i) + if (id1.value[i] != id2.value[i]) { - if (value[i] != other.value[i]) - { - return false; - } + return false; } - return true; } + return true; +} - /** - * Guid prefix comparison operator - * @param other Second EntityId to compare - * @return True if not equal - */ - inline bool operator !=( - const EntityId_t& other) const - { - for (uint8_t i = 0; i < 4; ++i) - { - if (value[i] != other.value[i]) - { - return true; - } - } - return false; - } +/** + * Guid prefix comparison operator + * @param id1 First EntityId to compare + * @param id2 Second EntityId to compare + * @return True if not equal + */ +inline bool operator !=( + const EntityId_t& id1, + const EntityId_t& id2) +{ + return !(id1 == id2); +} #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC -}; - - inline std::ostream& operator <<( std::ostream& output, const EntityId_t& enI) diff --git a/include/fastdds/rtps/common/Guid.h b/include/fastdds/rtps/common/Guid.h index dac89b2f226..93f08227bc0 100644 --- a/include/fastdds/rtps/common/Guid.h +++ b/include/fastdds/rtps/common/Guid.h @@ -132,14 +132,7 @@ struct RTPS_DllAPI GUID_t inline bool operator ==( const GUID_t& other) const { - if (guidPrefix == other.guidPrefix && entityId == other.entityId) - { - return true; - } - else - { - return false; - } + return (guidPrefix == other.guidPrefix && entityId == other.entityId); } /** @@ -150,14 +143,7 @@ struct RTPS_DllAPI GUID_t inline bool operator !=( const GUID_t& other) const { - if (guidPrefix != other.guidPrefix || entityId != other.entityId) - { - return true; - } - else - { - return false; - } + return (guidPrefix != other.guidPrefix || entityId != other.entityId); } inline bool operator <( diff --git a/include/fastdds/rtps/common/Time_t.h b/include/fastdds/rtps/common/Time_t.h index 4c3b20573e6..e0646c60a99 100644 --- a/include/fastdds/rtps/common/Time_t.h +++ b/include/fastdds/rtps/common/Time_t.h @@ -78,7 +78,7 @@ struct RTPS_DllAPI Time_t #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC /** - * Comparison assignment + * Comparison operator * @param other Second Time_t to compare * @return True if equal */ From de0d66f6f22f125827e207b5b5ffc660abb9ffeb Mon Sep 17 00:00:00 2001 From: Iker Luengo Date: Wed, 15 Sep 2021 10:00:09 +0200 Subject: [PATCH 4/6] Requested changes Signed-off-by: Iker Luengo --- include/fastdds/dds/{topic => common}/InstanceHandle.hpp | 4 +++- include/fastdds/dds/topic/TypeSupport.hpp | 2 +- include/fastdds/rtps/common/Time_t.h | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) rename include/fastdds/dds/{topic => common}/InstanceHandle.hpp (91%) diff --git a/include/fastdds/dds/topic/InstanceHandle.hpp b/include/fastdds/dds/common/InstanceHandle.hpp similarity index 91% rename from include/fastdds/dds/topic/InstanceHandle.hpp rename to include/fastdds/dds/common/InstanceHandle.hpp index bd8854c5600..c884204e197 100644 --- a/include/fastdds/dds/topic/InstanceHandle.hpp +++ b/include/fastdds/dds/common/InstanceHandle.hpp @@ -1,4 +1,4 @@ -// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima). // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -21,6 +21,8 @@ #define _FASTDDS_DDS_INSTANCE_HANDLE_HPP_ #include +#include + namespace eprosima { namespace fastdds { diff --git a/include/fastdds/dds/topic/TypeSupport.hpp b/include/fastdds/dds/topic/TypeSupport.hpp index 2cf7e7471e8..42dd3bfa690 100644 --- a/include/fastdds/dds/topic/TypeSupport.hpp +++ b/include/fastdds/dds/topic/TypeSupport.hpp @@ -20,7 +20,7 @@ #define _FASTDDS_TYPE_SUPPORT_HPP_ #include -#include +#include #include #include diff --git a/include/fastdds/rtps/common/Time_t.h b/include/fastdds/rtps/common/Time_t.h index e0646c60a99..0a87c6d5e66 100644 --- a/include/fastdds/rtps/common/Time_t.h +++ b/include/fastdds/rtps/common/Time_t.h @@ -97,7 +97,7 @@ struct RTPS_DllAPI Time_t } /** - * Comparison assignment + * Comparison operator * @param other Second Time_t to compare * @return True if not equal */ @@ -324,7 +324,7 @@ class RTPS_DllAPI Time_t #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC /** - * Comparison assignment + * Comparison operator * @param other Second Time_t to compare * @return True if equal */ @@ -343,7 +343,7 @@ class RTPS_DllAPI Time_t } /** - * Comparison assignment + * Comparison operator * @param other Second Time_t to compare * @return True if not equal */ From 5c19e6c456871fae815bf74d4f1f2705ae27855a Mon Sep 17 00:00:00 2001 From: Iker Luengo Date: Wed, 15 Sep 2021 13:59:41 +0200 Subject: [PATCH 5/6] More requested changes Signed-off-by: Iker Luengo --- include/fastdds/dds/common/InstanceHandle.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/fastdds/dds/common/InstanceHandle.hpp b/include/fastdds/dds/common/InstanceHandle.hpp index c884204e197..144af61e0dd 100644 --- a/include/fastdds/dds/common/InstanceHandle.hpp +++ b/include/fastdds/dds/common/InstanceHandle.hpp @@ -17,8 +17,8 @@ * */ -#ifndef _FASTDDS_DDS_INSTANCE_HANDLE_HPP_ -#define _FASTDDS_DDS_INSTANCE_HANDLE_HPP_ +#ifndef _FASTDDS_DDS_COMMON_INSTANCEHANDLE_HPP_ +#define _FASTDDS_DDS_COMMON_INSTANCEHANDLE_HPP_ #include #include @@ -36,4 +36,4 @@ extern RTPS_DllAPI const InstanceHandle_t HANDLE_NIL; } // namespace fastdds } // namespace eprosima -#endif // _FASTDDS_DDS_INSTANCE_HANDLE_HPP_ +#endif // _FASTDDS_DDS_COMMON_INSTANCEHANDLE_HPP_ From 35e723e022a67050f13080770889cf81d2750196 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Fri, 17 Sep 2021 10:41:57 +0200 Subject: [PATCH 6/6] Reverting changes on rtps/common headers. Signed-off-by: Miguel Company --- include/fastdds/rtps/common/EntityId_t.hpp | 9 +- include/fastdds/rtps/common/Guid.h | 97 +-- include/fastdds/rtps/common/InstanceHandle.h | 64 +- include/fastdds/rtps/common/Time_t.h | 670 +++++++++++-------- 4 files changed, 505 insertions(+), 335 deletions(-) diff --git a/include/fastdds/rtps/common/EntityId_t.hpp b/include/fastdds/rtps/common/EntityId_t.hpp index de5ed5588ca..6fe9e6bdd70 100644 --- a/include/fastdds/rtps/common/EntityId_t.hpp +++ b/include/fastdds/rtps/common/EntityId_t.hpp @@ -231,7 +231,14 @@ inline bool operator !=( const EntityId_t& id1, const EntityId_t& id2) { - return !(id1 == id2); + for (uint8_t i = 0; i < 4; ++i) + { + if (id1.value[i] != id2.value[i]) + { + return true; + } + } + return false; } #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC diff --git a/include/fastdds/rtps/common/Guid.h b/include/fastdds/rtps/common/Guid.h index 93f08227bc0..4fdb35a28e1 100644 --- a/include/fastdds/rtps/common/Guid.h +++ b/include/fastdds/rtps/common/Guid.h @@ -122,62 +122,81 @@ struct RTPS_DllAPI GUID_t return *reinterpret_cast(this); } +}; + #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - /** - * GUID comparison operator - * @param other Second GUID to compare - * @return True if equal - */ - inline bool operator ==( - const GUID_t& other) const +/** + * GUID comparison operator + * @param g1 First GUID to compare + * @param g2 Second GUID to compare + * @return True if equal + */ +inline bool operator ==( + const GUID_t& g1, + const GUID_t& g2) +{ + if (g1.guidPrefix == g2.guidPrefix && g1.entityId == g2.entityId) { - return (guidPrefix == other.guidPrefix && entityId == other.entityId); + return true; + } + else + { + return false; } +} - /** - * GUID comparison operator - * @param other Second GUID to compare - * @return True if not equal - */ - inline bool operator !=( - const GUID_t& other) const +/** + * GUID comparison operator + * @param g1 First GUID to compare + * @param g2 Second GUID to compare + * @return True if not equal + */ +inline bool operator !=( + const GUID_t& g1, + const GUID_t& g2) +{ + if (g1.guidPrefix != g2.guidPrefix || g1.entityId != g2.entityId) { - return (guidPrefix != other.guidPrefix || entityId != other.entityId); + return true; + } + else + { + return false; } +} - inline bool operator <( - const GUID_t& other) const +inline bool operator <( + const GUID_t& g1, + const GUID_t& g2) +{ + for (uint8_t i = 0; i < 12; ++i) { - for (uint8_t i = 0; i < 12; ++i) + if (g1.guidPrefix.value[i] < g2.guidPrefix.value[i]) { - if (guidPrefix.value[i] < other.guidPrefix.value[i]) - { - return true; - } - else if (guidPrefix.value[i] > other.guidPrefix.value[i]) - { - return false; - } + return true; } - for (uint8_t i = 0; i < 4; ++i) + else if (g1.guidPrefix.value[i] > g2.guidPrefix.value[i]) { - if (entityId.value[i] < other.entityId.value[i]) - { - return true; - } - else if (entityId.value[i] > other.entityId.value[i]) - { - return false; - } + return false; + } + } + for (uint8_t i = 0; i < 4; ++i) + { + if (g1.entityId.value[i] < g2.entityId.value[i]) + { + return true; + } + else if (g1.entityId.value[i] > g2.entityId.value[i]) + { + return false; } - return false; } + return false; +} #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC -}; - const GUID_t c_Guid_Unknown; #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC diff --git a/include/fastdds/rtps/common/InstanceHandle.h b/include/fastdds/rtps/common/InstanceHandle.h index 52e35219756..37fa6a34cb4 100644 --- a/include/fastdds/rtps/common/InstanceHandle.h +++ b/include/fastdds/rtps/common/InstanceHandle.h @@ -126,50 +126,41 @@ struct RTPS_DllAPI InstanceHandle_t return *reinterpret_cast(this); } - /** - * Strictly less than operator - * @param other The other InstanceHandle_t to compare - * @return True if the value of this handle lexicographically preceeds that of \p other - */ - inline bool operator <( - const InstanceHandle_t& other) const - { - return memcmp(value, other.value, 16) < 0; - } +}; + +const InstanceHandle_t c_InstanceHandle_Unknown; #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - /** - * Comparison operator - * @param other Second InstanceHandle_t to compare - * @return True if equal - */ - inline bool operator ==( - const InstanceHandle_t& other) const +/** + * Comparison operator + * @param ihandle1 First InstanceHandle_t to compare + * @param ihandle2 Second InstanceHandle_t to compare + * @return True if equal + */ +inline bool operator ==( + const InstanceHandle_t& ihandle1, + const InstanceHandle_t& ihandle2) +{ + for (uint8_t i = 0; i < 16; ++i) { - for (uint8_t i = 0; i < 16; ++i) + if (ihandle1.value[i] != ihandle2.value[i]) { - if (value[i] != other.value[i]) - { - return false; - } + return false; } - return true; } + return true; +} - inline bool operator !=( - const InstanceHandle_t& other) const - { - return !(*this == other); - } +inline bool operator !=( + const InstanceHandle_t& ihandle1, + const InstanceHandle_t& ihandle2) +{ + return !(ihandle1 == ihandle2); +} #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC -}; - -const InstanceHandle_t c_InstanceHandle_Unknown; - - /** * Convert InstanceHandle_t to GUID * @param guid GUID to store the results @@ -216,6 +207,13 @@ inline GUID_t iHandle2GUID( return guid; } +inline bool operator <( + const InstanceHandle_t& h1, + const InstanceHandle_t& h2) +{ + return memcmp(h1.value, h2.value, 16) < 0; +} + #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC /** diff --git a/include/fastdds/rtps/common/Time_t.h b/include/fastdds/rtps/common/Time_t.h index 0a87c6d5e66..6620c7c009b 100644 --- a/include/fastdds/rtps/common/Time_t.h +++ b/include/fastdds/rtps/common/Time_t.h @@ -73,159 +73,10 @@ struct RTPS_DllAPI Time_t */ static void now( Time_t& ret); - - -#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - - /** - * Comparison operator - * @param other Second Time_t to compare - * @return True if equal - */ - inline bool operator ==( - const Time_t& other) const - { - if (seconds != other.seconds) - { - return false; - } - if (nanosec != other.nanosec) - { - return false; - } - return true; - } - - /** - * Comparison operator - * @param other Second Time_t to compare - * @return True if not equal - */ - inline bool operator !=( - const Time_t& other) const - { - return !(*this == other); - } - - /** - * Checks if a Time_t is less than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is less than the second - */ - inline bool operator <( - const Time_t& other) const - { - if (seconds < other.seconds) - { - return true; - } - else if (seconds > other.seconds) - { - return false; - } - else - { - if (nanosec < other.nanosec) - { - return true; - } - else - { - return false; - } - } - } - - /** - * Checks if a Time_t is greater than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is greater than the second - */ - inline bool operator >( - const Time_t& other) const - { - return other < *this; - } - - /** - * Checks if a Time_t is less or equal than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is less or equal than the second - */ - inline bool operator <=( - const Time_t& other) const - { - return !(*this > other); - } - - /** - * Checks if a Time_t is greater or equal than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is greater or equal than the second - */ - inline bool operator >=( - const Time_t& other) const - { - return !(*this < other); - } - - /** - * Adds two Time_t. - * @param other Second Time_t to add - * @return A new Time_t with the result. - */ - inline Time_t operator +( - const Time_t& other) const - { - Time_t result(seconds + other.seconds, nanosec + other.nanosec); - if (result.nanosec < nanosec) // Overflow is detected by any of them - { - ++result.seconds; - } - return result; - } - - /** - * Subtracts two Time_t. - * @param other Second Time_t to subtract - * @return A new Time_t with the result. - */ - inline Time_t operator -( - const Time_t& other) const - { - Time_t result(seconds - other.seconds, nanosec - other.nanosec); - if (result.nanosec > nanosec) // Overflow is detected by ta - { - --result.seconds; - } - return result; - } - -#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - }; using Duration_t = Time_t; -#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - -inline std::ostream& operator <<( - std::ostream& output, - const Time_t& t) -{ - long double t_aux = t.seconds + (((long double)t.nanosec) / 1000000000ULL); - return output << t_aux; -} - -#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - -//! Time_t (Duration_t) representing an infinite time. DONT USE IT IN CONSTRUCTORS -const Time_t c_TimeInfinite{TIME_T_INFINITE_SECONDS, TIME_T_INFINITE_NANOSECONDS}; -//! Time_t (Duration_t) representing a zero time. DONT USE IT IN CONSTRUCTORS -const Time_t c_TimeZero{0, 0}; -//! Time_t (Duration_t) representing an invalid time. DONT USE IT IN CONSTRUCTORS -const Time_t c_TimeInvalid{-1, TIME_T_INFINITE_NANOSECONDS}; - namespace rtps { /** @@ -321,153 +172,191 @@ class RTPS_DllAPI Time_t static void now( Time_t& ret); +private: + + //!Seconds + int32_t seconds_ = 0; + + //!Fraction of second (1 fraction = 1/(2^32) seconds) + uint32_t fraction_ = 0; + + //!Nanoseconds + uint32_t nanosec_ = 0; + + void set_fraction( + uint32_t frac); + + void set_nanosec( + uint32_t nanos); +}; + #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - /** - * Comparison operator - * @param other Second Time_t to compare - * @return True if equal - */ - inline bool operator ==( - const Time_t& other) const +/** + * Comparison assignment + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if equal + */ +static inline bool operator ==( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds() != t2.seconds()) + { + return false; + } + if (t1.fraction() != t2.fraction()) + { + return false; + } + return true; +} + +/** + * Comparison assignment + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if not equal + */ +static inline bool operator !=( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds() != t2.seconds()) { - if (seconds() != other.seconds()) + return true; + } + if (t1.fraction() != t2.fraction()) + { + return true; + } + return false; +} + +/** + * Checks if a Time_t is less than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is less than the second + */ +static inline bool operator <( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds() < t2.seconds()) + { + return true; + } + else if (t1.seconds() > t2.seconds()) + { + return false; + } + else + { + if (t1.fraction() < t2.fraction()) { - return false; + return true; } - if (fraction() != other.fraction()) + else { return false; } - return true; } +} - /** - * Comparison operator - * @param other Second Time_t to compare - * @return True if not equal - */ - inline bool operator !=( - const Time_t& other) const +/** + * Checks if a Time_t is greater than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is greater than the second + */ +static inline bool operator >( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds() > t2.seconds()) { - return !(*this == other); + return true; } - - /** - * Checks if a Time_t is less than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is less than the second - */ - inline bool operator <( - const Time_t& other) const + else if (t1.seconds() < t2.seconds()) + { + return false; + } + else { - if (seconds() < other.seconds()) + if (t1.fraction() > t2.fraction()) { return true; } - else if (seconds() > other.seconds()) - { - return false; - } else { - if (fraction() < other.fraction()) - { - return true; - } - else - { - return false; - } + return false; } } +} - /** - * Checks if a Time_t is greater than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is greater than the second - */ - inline bool operator >( - const Time_t& other) const - { - return other < *this; - } - - /** - * Checks if a Time_t is less or equal than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is less or equal than the second - */ - inline bool operator <=( - const Time_t& other) const +/** + * Checks if a Time_t is less or equal than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is less or equal than the second + */ +static inline bool operator <=( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds() < t2.seconds()) { - return !(*this > other); + return true; } - - /** - * Checks if a Time_t is greater or equal than other. - * @param other Second Time_t to compare - * @return True if the first Time_t is greater or equal than the second - */ - inline bool operator >=( - const Time_t& other) const + else if (t1.seconds() > t2.seconds()) { - return !(*this < other); + return false; } - - /** - * Adds two Time_t. - * @param other Second Time_t to add - * @return A new Time_t with the result. - */ - inline Time_t operator +( - const Time_t& other) const + else { - Time_t result(seconds() + other.seconds(), fraction() + other.fraction()); - if (result.fraction() < fraction()) // Overflow is detected by any of them + if (t1.fraction() <= t2.fraction()) + { + return true; + } + else { - ++result.seconds(); + return false; } - return result; } +} - /** - * Subtracts two Time_t. - * @param other Second Time_t to subtract - * @return A new Time_t with the result. - */ - inline Time_t operator -( - const Time_t& other) const +/** + * Checks if a Time_t is greater or equal than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is greater or equal than the second + */ +static inline bool operator >=( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds() > t2.seconds()) + { + return true; + } + else if (t1.seconds() < t2.seconds()) + { + return false; + } + else { - Time_t result(seconds() - other.seconds(), fraction() - other.fraction()); - if (result.fraction() > fraction()) // Overflow is detected by ta + if (t1.fraction() >= t2.fraction()) { - --result.seconds(); + return true; + } + else + { + return false; } - return result; } - -#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC - -private: - - //!Seconds - int32_t seconds_ = 0; - - //!Fraction of second (1 fraction = 1/(2^32) seconds) - uint32_t fraction_ = 0; - - //!Nanoseconds - uint32_t nanosec_ = 0; - - void set_fraction( - uint32_t frac); - - void set_nanosec( - uint32_t nanos); -}; - -#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC +} inline std::ostream& operator <<( std::ostream& output, @@ -514,6 +403,42 @@ inline std::istream& operator >>( return input; } +/** + * Adds two Time_t. + * @param ta First Time_t to add + * @param tb Second Time_t to add + * @return A new Time_t with the result. + */ +static inline Time_t operator +( + const Time_t& ta, + const Time_t& tb) +{ + Time_t result(ta.seconds() + tb.seconds(), ta.fraction() + tb.fraction()); + if (result.fraction() < ta.fraction()) // Overflow is detected by any of them + { + ++result.seconds(); + } + return result; +} + +/** + * Subtracts two Time_t. + * @param ta First Time_t to subtract + * @param tb Second Time_t to subtract + * @return A new Time_t with the result. + */ +static inline Time_t operator -( + const Time_t& ta, + const Time_t& tb) +{ + Time_t result(ta.seconds() - tb.seconds(), ta.fraction() - tb.fraction()); + if (result.fraction() > ta.fraction()) // Overflow is detected by ta + { + --result.seconds(); + } + return result; +} + #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC const Time_t c_RTPSTimeInfinite{0x7fffffff, 0xffffffff}; @@ -522,6 +447,227 @@ const Time_t c_RTPSTimeInvalid{-1, 0xffffffff}; } // namespace rtps +#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC + +/** + * Comparison assignment + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if equal + */ +static inline bool operator ==( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds != t2.seconds) + { + return false; + } + if (t1.nanosec != t2.nanosec) + { + return false; + } + return true; +} + +/** + * Comparison assignment + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if not equal + */ +static inline bool operator !=( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds != t2.seconds) + { + return true; + } + if (t1.nanosec != t2.nanosec) + { + return true; + } + return false; +} + +/** + * Checks if a Time_t is less than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is less than the second + */ +static inline bool operator <( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds < t2.seconds) + { + return true; + } + else if (t1.seconds > t2.seconds) + { + return false; + } + else + { + if (t1.nanosec < t2.nanosec) + { + return true; + } + else + { + return false; + } + } +} + +/** + * Checks if a Time_t is greater than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is greater than the second + */ +static inline bool operator >( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds > t2.seconds) + { + return true; + } + else if (t1.seconds < t2.seconds) + { + return false; + } + else + { + if (t1.nanosec > t2.nanosec) + { + return true; + } + else + { + return false; + } + } +} + +/** + * Checks if a Time_t is less or equal than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is less or equal than the second + */ +static inline bool operator <=( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds < t2.seconds) + { + return true; + } + else if (t1.seconds > t2.seconds) + { + return false; + } + else + { + if (t1.nanosec <= t2.nanosec) + { + return true; + } + else + { + return false; + } + } +} + +/** + * Checks if a Time_t is greater or equal than other. + * @param t1 First Time_t to compare + * @param t2 Second Time_t to compare + * @return True if the first Time_t is greater or equal than the second + */ +static inline bool operator >=( + const Time_t& t1, + const Time_t& t2) +{ + if (t1.seconds > t2.seconds) + { + return true; + } + else if (t1.seconds < t2.seconds) + { + return false; + } + else + { + if (t1.nanosec >= t2.nanosec) + { + return true; + } + else + { + return false; + } + } +} + +inline std::ostream& operator <<( + std::ostream& output, + const Time_t& t) +{ + long double t_aux = t.seconds + (((long double)t.nanosec) / 1000000000ULL); + return output << t_aux; +} + +/** + * Adds two Time_t. + * @param ta First Time_t to add + * @param tb Second Time_t to add + * @return A new Time_t with the result. + */ +static inline Time_t operator +( + const Time_t& ta, + const Time_t& tb) +{ + Time_t result(ta.seconds + tb.seconds, ta.nanosec + tb.nanosec); + if (result.nanosec < ta.nanosec) // Overflow is detected by any of them + { + ++result.seconds; + } + return result; +} + +/** + * Subtracts two Time_t. + * @param ta First Time_t to subtract + * @param tb Second Time_t to subtract + * @return A new Time_t with the result. + */ +static inline Time_t operator -( + const Time_t& ta, + const Time_t& tb) +{ + Time_t result(ta.seconds - tb.seconds, ta.nanosec - tb.nanosec); + if (result.nanosec > ta.nanosec) // Overflow is detected by ta + { + --result.seconds; + } + return result; +} + +#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC + +//! Time_t (Duration_t) representing an infinite time. DONT USE IT IN CONSTRUCTORS +const Time_t c_TimeInfinite{TIME_T_INFINITE_SECONDS, TIME_T_INFINITE_NANOSECONDS}; +//! Time_t (Duration_t) representing a zero time. DONT USE IT IN CONSTRUCTORS +const Time_t c_TimeZero{0, 0}; +//! Time_t (Duration_t) representing an invalid time. DONT USE IT IN CONSTRUCTORS +const Time_t c_TimeInvalid{-1, TIME_T_INFINITE_NANOSECONDS}; + } // namespace fastrtps } // namespace eprosima