/src/Fast-DDS/include/fastdds/rtps/common/GuidPrefix_t.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2016-2019 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | /** |
16 | | * @file GuidPrefix_t.hpp |
17 | | */ |
18 | | |
19 | | #ifndef FASTDDS_RTPS_COMMON__GUIDPREFIX_T_HPP |
20 | | #define FASTDDS_RTPS_COMMON__GUIDPREFIX_T_HPP |
21 | | |
22 | | #include <fastdds/fastdds_dll.hpp> |
23 | | #include <fastdds/rtps/common/Types.hpp> |
24 | | |
25 | | #include <cstdint> |
26 | | #include <cstring> |
27 | | #include <sstream> |
28 | | #include <iomanip> |
29 | | |
30 | | namespace eprosima { |
31 | | namespace fastdds { |
32 | | namespace rtps { |
33 | | |
34 | | //!@brief Structure GuidPrefix_t, Guid Prefix of GUID_t. |
35 | | //!@ingroup COMMON_MODULE |
36 | | struct FASTDDS_EXPORTED_API GuidPrefix_t |
37 | | { |
38 | | static constexpr unsigned int size = 12; |
39 | | octet value[size]; |
40 | | |
41 | | //!Default constructor. Set the Guid prefix to 0. |
42 | | GuidPrefix_t() |
43 | 134k | { |
44 | 134k | memset(value, 0, size); |
45 | 134k | } |
46 | | |
47 | | /** |
48 | | * Checks whether this guid prefix is from an entity on the same host as another guid prefix. |
49 | | * |
50 | | * @note This method assumes the value of \c other_guid_prefix was originally assigned by Fast-DDS vendor. |
51 | | * |
52 | | * @param other_guid_prefix GuidPrefix_t to compare to. |
53 | | * |
54 | | * @return true when this guid prefix is on the same host, false otherwise. |
55 | | */ |
56 | | bool is_on_same_host_as( |
57 | | const GuidPrefix_t& other_guid_prefix) const; |
58 | | |
59 | | /** |
60 | | * Checks whether this guid prefix is from a (Fast-DDS) entity created on this host (from where this method is called). |
61 | | * |
62 | | * @return true when this guid prefix is from a (Fast-DDS) entity created on this host, false otherwise. |
63 | | */ |
64 | | bool is_from_this_host() const; |
65 | | |
66 | | /** |
67 | | * Checks whether this guid prefix is for an entity on the same host and process as another guid prefix. |
68 | | * |
69 | | * @note This method assumes the value of \c other_guid_prefix was originally assigned by Fast-DDS vendor. |
70 | | * |
71 | | * @param other_guid_prefix GuidPrefix_t to compare to. |
72 | | * |
73 | | * @return true when this guid prefix is on the same host and process, false otherwise. |
74 | | */ |
75 | | bool is_on_same_process_as( |
76 | | const GuidPrefix_t& other_guid_prefix) const; |
77 | | |
78 | | /** |
79 | | * Checks whether this guid prefix is from a (Fast-DDS) entity created on this host and process (from where this method is called). |
80 | | * |
81 | | * @return true when this guid prefix is from a (Fast-DDS) entity created on this host and process, false otherwise. |
82 | | */ |
83 | | bool is_from_this_process() const; |
84 | | |
85 | | static GuidPrefix_t unknown() |
86 | 0 | { |
87 | 0 | return GuidPrefix_t(); |
88 | 0 | } |
89 | | |
90 | | #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
91 | | |
92 | | /** |
93 | | * Guid prefix comparison operator |
94 | | * @param prefix guid prefix to compare |
95 | | * @return True if the guid prefixes are equal |
96 | | */ |
97 | | bool operator ==( |
98 | | const GuidPrefix_t& prefix) const |
99 | 0 | { |
100 | 0 | return (memcmp(value, prefix.value, size) == 0); |
101 | 0 | } |
102 | | |
103 | | /** |
104 | | * Guid prefix comparison operator |
105 | | * @param prefix Second guid prefix to compare |
106 | | * @return True if the guid prefixes are not equal |
107 | | */ |
108 | | bool operator !=( |
109 | | const GuidPrefix_t& prefix) const |
110 | 16.0k | { |
111 | 16.0k | return (memcmp(value, prefix.value, size) != 0); |
112 | 16.0k | } |
113 | | |
114 | | /** |
115 | | * Guid prefix minor operator |
116 | | * @param prefix Second guid prefix to compare |
117 | | * @return True if prefix is higher than this |
118 | | */ |
119 | | bool operator <( |
120 | | const GuidPrefix_t& prefix) const |
121 | 0 | { |
122 | 0 | return std::memcmp(value, prefix.value, size) < 0; |
123 | 0 | } |
124 | | |
125 | | /** |
126 | | * Guid Prefix compare static method. |
127 | | * |
128 | | * @param prefix1 First guid prefix to compare |
129 | | * @param prefix2 Second guid prefix to compare |
130 | | * |
131 | | * @return 0 if \c prefix1 is equal to \c prefix2 . |
132 | | * @return < 0 if \c prefix1 is lower than \c prefix2 . |
133 | | * @return > 0 if \c prefix1 is higher than \c prefix2 . |
134 | | */ |
135 | | static int cmp( |
136 | | const GuidPrefix_t& prefix1, |
137 | | const GuidPrefix_t& prefix2) |
138 | 0 | { |
139 | 0 | return std::memcmp(prefix1.value, prefix2.value, size); |
140 | 0 | } |
141 | | |
142 | | #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
143 | | }; |
144 | | |
145 | | const GuidPrefix_t c_GuidPrefix_Unknown; |
146 | | |
147 | | inline std::ostream& operator <<( |
148 | | std::ostream& output, |
149 | | const GuidPrefix_t& guiP) |
150 | 0 | { |
151 | 0 | std::stringstream ss; |
152 | 0 | ss << std::hex; |
153 | 0 | char old_fill = ss.fill('0'); |
154 | 0 | for (uint8_t i = 0; i < 11; ++i) |
155 | 0 | { |
156 | 0 | ss << std::setw(2) << (int)guiP.value[i] << "."; |
157 | 0 | } |
158 | 0 | ss << std::setw(2) << (int)guiP.value[11]; |
159 | 0 | ss.fill(old_fill); |
160 | 0 | ss << std::dec; |
161 | 0 | return output << ss.str(); |
162 | 0 | } |
163 | | |
164 | | inline std::istream& operator >>( |
165 | | std::istream& input, |
166 | | GuidPrefix_t& guiP) |
167 | 0 | { |
168 | 0 | std::istream::sentry s(input); |
169 | |
|
170 | 0 | if (s) |
171 | 0 | { |
172 | 0 | char point; |
173 | 0 | unsigned short hex; |
174 | 0 | std::ios_base::iostate excp_mask = input.exceptions(); |
175 | |
|
176 | 0 | try |
177 | 0 | { |
178 | 0 | input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit); |
179 | 0 | input >> std::hex >> hex; |
180 | |
|
181 | 0 | if (hex > 255) |
182 | 0 | { |
183 | 0 | input.setstate(std::ios_base::failbit); |
184 | 0 | } |
185 | |
|
186 | 0 | guiP.value[0] = static_cast<octet>(hex); |
187 | |
|
188 | 0 | for (int i = 1; i < 12; ++i) |
189 | 0 | { |
190 | 0 | input >> point >> hex; |
191 | 0 | if ( point != '.' || hex > 255 ) |
192 | 0 | { |
193 | 0 | input.setstate(std::ios_base::failbit); |
194 | 0 | } |
195 | 0 | guiP.value[i] = static_cast<octet>(hex); |
196 | 0 | } |
197 | |
|
198 | 0 | input >> std::dec; |
199 | 0 | } |
200 | 0 | catch (std::ios_base::failure& ) |
201 | 0 | { |
202 | 0 | guiP = GuidPrefix_t::unknown(); |
203 | 0 | } |
204 | |
|
205 | 0 | input.exceptions(excp_mask); |
206 | 0 | } |
207 | |
|
208 | 0 | return input; |
209 | 0 | } |
210 | | |
211 | | } // namespace rtps |
212 | | } // namespace fastdds |
213 | | } // namespace eprosima |
214 | | |
215 | | #endif // FASTDDS_RTPS_COMMON__GUIDPREFIX_T_HPP |