123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- // Copyright 2016 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 Guid.h
- */
- #ifndef _FASTDDS_RTPS_RTPS_GUID_H_
- #define _FASTDDS_RTPS_RTPS_GUID_H_
- #include <fastrtps/fastrtps_dll.h>
- #include <fastdds/rtps/common/Types.h>
- #include <fastdds/rtps/common/GuidPrefix_t.hpp>
- #include <fastdds/rtps/common/EntityId_t.hpp>
- #include <cstdint>
- #include <cstring>
- #include <sstream>
- namespace eprosima {
- namespace fastrtps {
- namespace rtps {
- struct InstanceHandle_t;
- //!@brief Structure GUID_t, entity identifier, unique in DDS-RTPS Domain.
- //!@ingroup COMMON_MODULE
- struct RTPS_DllAPI GUID_t
- {
- //!Guid prefix
- GuidPrefix_t guidPrefix;
- //!Entity id
- EntityId_t entityId;
- /*!
- * Default constructor. Contructs an unknown GUID.
- */
- GUID_t() noexcept
- {
- }
- /**
- * Construct
- * @param guid_prefix Guid prefix
- * @param id Entity id
- */
- GUID_t(
- const GuidPrefix_t& guid_prefix,
- uint32_t id) noexcept
- : guidPrefix(guid_prefix)
- , entityId(id)
- {
- }
- /**
- * @param guid_prefix Guid prefix
- * @param entity_id Entity id
- */
- GUID_t(
- const GuidPrefix_t& guid_prefix,
- const EntityId_t& entity_id) noexcept
- : guidPrefix(guid_prefix)
- , entityId(entity_id)
- {
- }
- /**
- * Checks whether this guid is for an entity on the same host as another guid.
- *
- * @param other_guid GUID_t to compare to.
- *
- * @return true when this guid is on the same host, false otherwise.
- */
- bool is_on_same_host_as(
- const GUID_t& other_guid) const
- {
- return memcmp(guidPrefix.value, other_guid.guidPrefix.value, 4) == 0;
- }
- /**
- * Checks whether this guid is for an entity on the same host and process as another guid.
- *
- * @param other_guid GUID_t to compare to.
- *
- * @return true when this guid is on the same host and process, false otherwise.
- */
- bool is_on_same_process_as(
- const GUID_t& other_guid) const
- {
- return memcmp(guidPrefix.value, other_guid.guidPrefix.value, 8) == 0;
- }
- /**
- * Checks whether this guid corresponds to a builtin entity.
- *
- * @return true when this guid corresponds to a builtin entity, false otherwise.
- */
- bool is_builtin() const
- {
- return entityId.value[3] >= 0xC0;
- }
- static GUID_t unknown() noexcept
- {
- return GUID_t();
- };
- // TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
- explicit operator const InstanceHandle_t&() const
- {
- return *reinterpret_cast<const InstanceHandle_t*>(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
- {
- return false;
- }
- }
- inline bool operator <(
- const GUID_t& g1,
- const GUID_t& g2)
- {
- for (uint8_t i = 0; i < 12; ++i)
- {
- if (g1.guidPrefix.value[i] < g2.guidPrefix.value[i])
- {
- return true;
- }
- else if (g1.guidPrefix.value[i] > g2.guidPrefix.value[i])
- {
- 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;
- }
- #endif
- const GUID_t c_Guid_Unknown;
- #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
- /**
- * Stream operator, prints a GUID.
- * @param output Output stream.
- * @param guid GUID_t to print.
- * @return Stream operator.
- */
- inline std::ostream& operator <<(
- std::ostream& output,
- const GUID_t& guid)
- {
- if (guid !=c_Guid_Unknown)
- {
- output << guid.guidPrefix << "|" << guid.entityId;
- }
- else
- {
- output << "|GUID UNKNOWN|";
- }
- return output;
- }
- /**
- * Stream operator, retrieves a GUID.
- * @param input Input stream.
- * @param guid GUID_t to print.
- * @return Stream operator.
- */
- inline std::istream& operator >>(
- std::istream& input,
- GUID_t& guid)
- {
- std::istream::sentry s(input);
- if (s)
- {
- std::ios_base::iostate excp_mask = input.exceptions();
- try
- {
- input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
- input >> guid.guidPrefix;
- input >> guid.entityId;
- }
- catch (std::ios_base::failure&)
- {
- // maybe is unknown or just invalid
- guid = c_Guid_Unknown;
- }
- input.exceptions(excp_mask);
- }
- return input;
- }
- #endif
- } // namespace rtps
- } // namespace fastrtps
- } // namespace eprosima
- #endif /* _FASTDDS_RTPS_RTPS_GUID_H_ */
|