OpenDDS  Snapshot(2023/04/28-20:55)
GuidUtils.cpp
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "DCPS/DdsDcps_pch.h" //Only the _pch include should start with DCPS/
9 
10 #include "GuidUtils.h"
11 #include "Util.h"
12 #include "GuidBuilder.h"
13 #include "SafetyProfileStreams.h"
14 #ifndef OPENDDS_SAFETY_PROFILE
16 #endif
17 
18 #include <ace/ACE.h>
19 #include <ace/OS_NS_string.h>
20 
21 #include <cstdlib>
22 #include <cstdio>
23 
24 namespace {
25 
26 #ifndef OPENDDS_SAFETY_PROFILE
27 inline std::ostream&
28 sep(std::ostream& os)
29 {
30  return os << ".";
31 }
32 
33 inline std::ostream&
34 setopts(std::ostream& os)
35 {
36  return os << std::setfill('0') << std::setw(2);
37 }
38 #endif
39 
40 } // namespace
41 
43 
44 namespace OpenDDS { namespace DCPS {
45 
47 to_string(const EntityId_t& entityId)
48 {
49  return to_hex_dds_string(&entityId.entityKey[0], sizeof(EntityKey_t)) +
50  to_dds_string(unsigned(entityId.entityKind), true);
51 }
52 
54 to_string(const GUID_t& guid)
55 {
56  return to_hex_dds_string(&guid.guidPrefix[0], sizeof(GuidPrefix_t), '.', 4) +
57  '.' + to_string(guid.entityId);
58 }
59 
60 void intersect(const GuidSet& a, const GuidSet& b, GuidSet& result)
61 {
62  result.clear();
63  intersect_sorted_ranges(a.begin(), a.end(), b.begin(), b.end(),
64  std::inserter(result, result.end()), GUID_tKeyLessThan());
65 }
66 
67 #ifndef OPENDDS_SAFETY_PROFILE
68 std::ostream&
69 operator<<(std::ostream& os, const GUID_t& rhs)
70 {
71  RestoreOutputStreamState os_state(os);
72 
73  std::size_t len = sizeof(rhs.guidPrefix);
74 
75  os << std::hex;
76 
77  for (std::size_t i = 0; i < len; ++i) {
78  os << setopts << unsigned(rhs.guidPrefix[i]);
79 
80  if ((i + 1) % 4 == 0) os << sep;
81  }
82 
83  len = sizeof(rhs.entityId.entityKey);
84 
85  for (std::size_t i = 0; i < len; ++i) {
86  os << setopts << unsigned(rhs.entityId.entityKey[i]);
87  }
88 
89  os << setopts << unsigned(rhs.entityId.entityKind);
90 
91  // Reset, because hex is "sticky"
92  os << std::dec;
93 
94  return os;
95 }
96 
97 std::istream&
98 operator>>(std::istream& is, GUID_t& rhs)
99 {
100  long word;
101  char discard;
102 
103  GuidBuilder builder(rhs);
104 
105  is >> std::hex >> word;
106  builder.guidPrefix0(word);
107  is >> discard; // sep
108 
109  is >> std::hex >> word;
110  builder.guidPrefix1(word);
111  is >> discard; // sep
112 
113  is >> std::hex >> word;
114  builder.guidPrefix2(word);
115  is >> discard; // sep
116 
117  is >> std::hex >> word;
118  builder.entityId(word);
119 
120  return is;
121 }
122 #endif
123 
124 } }
125 
String to_dds_string(unsigned short to_convert)
key GuidPrefix_t guidPrefix
Definition: DdsDcpsGuid.idl:58
octet EntityKey_t[3]
Definition: DdsDcpsGuid.idl:22
#define OPENDDS_STRING
String to_hex_dds_string(const unsigned char *data, const size_t size, const char delim, const size_t delim_every)
ACE_CDR::Boolean operator<<(Serializer &serializer, CoherentChangeControl &value)
Marshal/Insertion into a buffer.
key EntityKey_t entityKey
Definition: DdsDcpsGuid.idl:27
void entityId(EntityId_t entityId)
Definition: GuidBuilder.cpp:90
octet GuidPrefix_t[12]
Definition: DdsDcpsGuid.idl:19
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
void intersect(const GuidSet &a, const GuidSet &b, GuidSet &result)
Definition: GuidUtils.cpp:60
ACE_CDR::Boolean operator>>(Serializer &serializer, CoherentChangeControl &value)
const char * to_string(MessageId value)
OutputIterator intersect_sorted_ranges(InputIteratorA a, InputIteratorA aEnd, InputIteratorB b, InputIteratorB bEnd, OutputIterator intersect)
Definition: Util.h:166
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
key EntityId_t entityId
Definition: DdsDcpsGuid.idl:59