LCOV - code coverage report
Current view: top level - DCPS - GuidUtils.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 55 54.5 %
Date: 2023-04-30 01:32:43 Functions: 12 21 57.1 %

          Line data    Source code
       1             : /*
       2             :  * Distributed under the OpenDDS License.
       3             :  * See: http://www.opendds.org/license.html
       4             :  */
       5             : 
       6             : #ifndef OPENDDS_DCPS_GUIDUTILS_H
       7             : #define OPENDDS_DCPS_GUIDUTILS_H
       8             : 
       9             : #include "dcps_export.h"
      10             : #include "PoolAllocator.h"
      11             : #include "Serializer.h"
      12             : #include "Hash.h"
      13             : #include "Util.h"
      14             : 
      15             : #include <dds/DdsDcpsGuidC.h>
      16             : #include <dds/DdsDcpsInfoUtilsC.h>
      17             : 
      18             : #ifndef OPENDDS_SAFETY_PROFILE
      19             : #  include <iosfwd>
      20             : #endif
      21             : #include <cstring>
      22             : 
      23             : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
      24             : 
      25             : namespace OpenDDS {
      26             : namespace DCPS {
      27             : 
      28             : /// Vendor Id value specified for OCI is used for OpenDDS.
      29             : const GuidVendorId_t VENDORID_OCI = {0x01, 0x03};
      30             : 
      31             : /// Nil value for the GUID prefix (participant identifier).
      32             : const GuidPrefix_t GUIDPREFIX_UNKNOWN = {0};
      33             : 
      34             : ///@{
      35             : /// Entity Id values specified in Version 2.1 of RTPS specification.
      36             : const EntityId_t ENTITYID_UNKNOWN = {{0x00, 0x00, 0x00}, 0x00};
      37             : const EntityId_t ENTITYID_PARTICIPANT = {{0x00, 0x00, 0x01}, 0xc1};
      38             : const EntityId_t ENTITYID_SEDP_BUILTIN_TOPIC_WRITER = {{0x00, 0x00, 0x02}, 0xc2};
      39             : const EntityId_t ENTITYID_SEDP_BUILTIN_TOPIC_READER = {{0x00, 0x00, 0x02}, 0xc7};
      40             : const EntityId_t ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER = {{0x00, 0x00, 0x03}, 0xc2};
      41             : const EntityId_t ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER = {{0x00, 0x00, 0x03}, 0xc7};
      42             : const EntityId_t ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER = {{0x00, 0x00, 0x04}, 0xc2};
      43             : const EntityId_t ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER = {{0x00, 0x00, 0x04}, 0xc7};
      44             : const EntityId_t ENTITYID_SPDP_BUILTIN_PARTICIPANT_WRITER = {{0x00, 0x01, 0x00}, 0xc2};
      45             : const EntityId_t ENTITYID_SPDP_BUILTIN_PARTICIPANT_READER = {{0x00, 0x01, 0x00}, 0xc7};
      46             : const EntityId_t ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER = {{0x00, 0x02, 0x00}, 0xc2};
      47             : const EntityId_t ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_READER = {{0x00, 0x02, 0x00}, 0xc7};
      48             : ///@}
      49             : 
      50             : ///@{
      51             : /// XTypes Type Lookup Service
      52             : const EntityId_t ENTITYID_TL_SVC_REQ_WRITER = {{0x00, 0x03, 0x00}, 0xc3};
      53             : const EntityId_t ENTITYID_TL_SVC_REQ_READER = {{0x00, 0x03, 0x00}, 0xc4};
      54             : const EntityId_t ENTITYID_TL_SVC_REPLY_WRITER = {{0x00, 0x03, 0x01}, 0xc3};
      55             : const EntityId_t ENTITYID_TL_SVC_REPLY_READER = {{0x00, 0x03, 0x01}, 0xc4};
      56             : ///@}
      57             : 
      58             : /// Nil value for GUID.
      59             : const GUID_t GUID_UNKNOWN = { { 0 }, { { 0 }, 0 } };
      60             : 
      61             : /**
      62             :  * Identifies the kinds of entities used in a GUID.
      63             :  *
      64             :  * See dds/DdsDcpsGuid.idl for the values these map to.
      65             :  */
      66             : enum EntityKind {
      67             :   KIND_UNKNOWN, ///< ENTITYKIND_USER_UNKNOWN and ENTITYKIND_BUILTIN_UNKNOWN
      68             :   KIND_PARTICIPANT, ///< ENTITYKIND_BUILTIN_PARTICIPANT
      69             :   KIND_USER_WRITER, ///< ENTITYKIND_USER_WRITER_WITH_KEY and ENTITYKIND_USER_WRITER_NO_KEY
      70             :   KIND_USER_READER, ///< ENTITYKIND_USER_READER_WITH_KEY and ENTITYKIND_USER_READER_NO_KEY
      71             :   KIND_USER_TOPIC, ///< ENTITYKIND_OPENDDS_TOPIC
      72             :   KIND_BUILTIN_WRITER, ///< ENTITYKIND_BUILTIN_WRITER_WITH_KEY and ENTITYKIND_USER_WRITER_NO_KEY
      73             :   KIND_BUILTIN_READER, ///< ENTITYKIND_BUILTIN_READER_WITH_KEY and ENTITYKIND_USER_READER_NO_KEY
      74             :   KIND_BUILTIN_TOPIC, ///< ENTITYKIND_BUILTIN_TOPIC
      75             :   KIND_PUBLISHER, ///< Publisher (OpenDDS-specific)
      76             :   KIND_SUBSCRIBER, ///< Subscriber (OpenDDS-specific)
      77             :   KIND_USER ///< For creating custom GUIDs for things that are not DDS entities (OpenDDS-specific)
      78             : };
      79             : 
      80        4135 : inline bool operator<(const GUID_t& lhs, const GUID_t& rhs)
      81             : {
      82        4135 :   return mem_cmp(lhs, rhs) < 0;
      83             : }
      84             : 
      85             : struct OpenDDS_Dcps_Export GUID_tKeyLessThan {
      86             :   static bool entity_less(const EntityId_t& v1, const EntityId_t& v2)
      87             :   {
      88             :     return mem_cmp(v1, v2) < 0;
      89             :   }
      90             : 
      91          23 :   bool operator()(const GUID_t& v1, const GUID_t& v2) const
      92             :   {
      93          23 :     return v1 < v2;
      94             :   }
      95             : };
      96             : 
      97             : struct OpenDDS_Dcps_Export EntityId_tKeyLessThan {
      98             :   bool operator()(const EntityId_t& v1, const EntityId_t& v2) const
      99             :   {
     100             :     return mem_cmp(v1, v2) < 0;
     101             :   }
     102             : };
     103             : 
     104             : struct OpenDDS_Dcps_Export BuiltinTopicKey_tKeyLessThan {
     105           0 :   bool operator()(const DDS::BuiltinTopicKey_t& v1,
     106             :                   const DDS::BuiltinTopicKey_t& v2) const
     107             :   {
     108           0 :     return mem_cmp(v1, v2) < 0;
     109             :   }
     110             : };
     111             : 
     112             : typedef OPENDDS_SET_CMP(GUID_t, GUID_tKeyLessThan) GuidSet;
     113             : typedef GuidSet RepoIdSet;
     114             : 
     115             : const size_t guid_cdr_size = 16;
     116             : 
     117             : #ifndef OPENDDS_SAFETY_PROFILE
     118             : inline bool
     119         879 : operator==(const GUID_t& lhs, const GUID_t& rhs)
     120             : {
     121         879 :   return mem_cmp(lhs, rhs) == 0;
     122             : }
     123             : 
     124             : inline bool
     125         152 : operator!=(const GUID_t& lhs, const GUID_t& rhs)
     126             : {
     127         152 :   return !(lhs == rhs);
     128             : }
     129             : #endif
     130             : 
     131             : OpenDDS_Dcps_Export inline
     132           0 : bool equal_guid_prefixes(const GuidPrefix_t& lhs, const GuidPrefix_t& rhs)
     133             : {
     134           0 :   return mem_cmp(lhs, rhs) == 0;
     135             : }
     136             : 
     137             : OpenDDS_Dcps_Export inline
     138           0 : bool equal_guid_prefixes(const GUID_t& lhs, const GUID_t& rhs)
     139             : {
     140           0 :   return equal_guid_prefixes(lhs.guidPrefix, rhs.guidPrefix);
     141             : }
     142             : 
     143             : #ifndef OPENDDS_SAFETY_PROFILE
     144             : inline bool
     145           0 : operator==(const EntityId_t& lhs, const EntityId_t& rhs)
     146             : {
     147           0 :   return mem_cmp(lhs, rhs) == 0;
     148             : }
     149             : 
     150             : inline bool
     151           0 : operator!=(const EntityId_t& lhs, const EntityId_t& rhs)
     152             : {
     153           0 :   return !(lhs == rhs);
     154             : }
     155             : #endif
     156             : 
     157             : inline void assign(EntityId_t& dest, const EntityId_t& src)
     158             : {
     159             :   std::memcpy(&dest, &src, sizeof(EntityId_t));
     160             : }
     161             : 
     162           0 : inline void assign(EntityKey_t& dest, unsigned src)
     163             : {
     164           0 :   dest[0] = static_cast<CORBA::Octet>(src);
     165           0 :   dest[1] = static_cast<CORBA::Octet>(src >> 8);
     166           0 :   dest[2] = static_cast<CORBA::Octet>(src >> 16);
     167           0 : }
     168             : 
     169             : OpenDDS_Dcps_Export OPENDDS_STRING
     170             : to_string(const GUID_t& guid);
     171             : 
     172             : OpenDDS_Dcps_Export OPENDDS_STRING
     173             : to_string(const EntityId_t& entityId);
     174             : 
     175             : #ifndef OPENDDS_SAFETY_PROFILE
     176             : // Serialize to ASCII Hex string: "xxxx.xxxx.xxxx.xxxx"
     177             : OpenDDS_Dcps_Export std::ostream&
     178             : operator<<(std::ostream& os, const GUID_t& rhs);
     179             : 
     180             : // Deserialize from ASCII Hex string: "xxxx.xxxx.xxxx.xxxx"
     181             : OpenDDS_Dcps_Export std::istream&
     182             : operator>>(std::istream& is, GUID_t& rhs);
     183             : #endif
     184             : 
     185          58 : inline void assign(GuidPrefix_t& dest, const GuidPrefix_t& src)
     186             : {
     187          58 :   std::memcpy(&dest[0], &src[0], sizeof(GuidPrefix_t));
     188          58 : }
     189             : 
     190             : inline void assign(GUID_t& dest, const GUID_t& src)
     191             : {
     192             :   std::memcpy(&dest, &src, sizeof(GUID_t));
     193             : }
     194             : 
     195           1 : inline void assign(DDS::OctetArray16& dest, const GUID_t& src)
     196             : {
     197           1 :   std::memcpy(&dest[0], &src, sizeof(src));
     198           1 : }
     199             : 
     200          58 : inline GUID_t make_id(const GuidPrefix_t& prefix, const EntityId_t& entity)
     201             : {
     202             :   GUID_t id;
     203          58 :   assign(id.guidPrefix, prefix);
     204          58 :   id.entityId = entity;
     205          58 :   return id;
     206             : }
     207             : 
     208           0 : inline GUID_t make_id(const GUID_t& participant_id, const EntityId_t& entity)
     209             : {
     210           0 :   GUID_t id = participant_id;
     211           0 :   id.entityId = entity;
     212           0 :   return id;
     213             : }
     214             : 
     215             : OpenDDS_Dcps_Export inline
     216          58 : GUID_t make_part_guid(const GuidPrefix_t& prefix)
     217             : {
     218          58 :   return make_id(prefix, ENTITYID_PARTICIPANT);
     219             : }
     220             : 
     221             : OpenDDS_Dcps_Export inline
     222          25 : GUID_t make_part_guid(const GUID_t& guid)
     223             : {
     224          25 :   return make_part_guid(guid.guidPrefix);
     225             : }
     226             : 
     227             : OpenDDS_Dcps_Export inline
     228             : GUID_t make_unknown_guid(const GuidPrefix_t& prefix)
     229             : {
     230             :   return make_id(prefix, ENTITYID_UNKNOWN);
     231             : }
     232             : 
     233             : OpenDDS_Dcps_Export inline
     234             : GUID_t make_unknown_guid(const GUID_t& guid)
     235             : {
     236             :   return make_unknown_guid(guid.guidPrefix);
     237             : }
     238             : 
     239             : OpenDDS_Dcps_Export
     240             : void intersect(const GuidSet& a, const GuidSet& b, GuidSet& result);
     241             : 
     242             : OpenDDS_Dcps_Export inline
     243           0 : DDS::BuiltinTopicKey_t guid_to_bit_key(const GUID_t& guid)
     244             : {
     245             :   DDS::BuiltinTopicKey_t key;
     246           0 :   std::memcpy(key.value, &guid, sizeof(key.value));
     247           0 :   return key;
     248             : }
     249             : 
     250             : OpenDDS_Dcps_Export inline
     251           0 : GUID_t bit_key_to_guid(const DDS::BuiltinTopicKey_t& key)
     252             : {
     253             :   GUID_t id;
     254           0 :   std::memcpy(&id, key.value, sizeof(id));
     255           0 :   return id;
     256             : }
     257             : 
     258             : struct OpenDDS_Dcps_Export GuidPair {
     259             :   GUID_t local;
     260             :   GUID_t remote;
     261             : 
     262           2 :   GuidPair(const GUID_t& local, const GUID_t& remote)
     263           2 :   : local(local)
     264           2 :   , remote(remote)
     265             :   {
     266           2 :   }
     267             : 
     268           7 :   int cmp(const GuidPair& other) const
     269             :   {
     270           7 :     const int local_cmp = mem_cmp(local, other.local);
     271           7 :     return local_cmp == 0 ? mem_cmp(remote, other.remote) : local_cmp;
     272             :   }
     273             : 
     274             :   bool operator<(const GuidPair& other) const
     275             :   {
     276             :     return cmp(other) < 0;
     277             :   }
     278             : };
     279             : 
     280             : } // namespace DCPS
     281             : } // namespace OpenDDS
     282             : 
     283             : OPENDDS_END_VERSIONED_NAMESPACE_DECL
     284             : 
     285             : #if defined ACE_HAS_CPP11
     286           9 : OPENDDS_OOAT_STD_HASH(OpenDDS::DCPS::GUID_t, OpenDDS_Dcps_Export);
     287             : #endif
     288             : 
     289             : #endif /* OPENDDS_DDS_DCPS_GUIDUTILS_H */

Generated by: LCOV version 1.16