Coverage Report

Created: 2022-08-24 06:19

/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 <fastrtps/fastrtps_dll.h>
23
#include <fastdds/rtps/common/Types.h>
24
25
#include <cstdint>
26
#include <cstring>
27
#include <sstream>
28
#include <iomanip>
29
30
namespace eprosima {
31
namespace fastrtps {
32
namespace rtps {
33
34
//!@brief Structure GuidPrefix_t, Guid Prefix of GUID_t.
35
//!@ingroup COMMON_MODULE
36
struct RTPS_DllAPI 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
77.2k
    {
44
77.2k
        memset(value, 0, size);
45
77.2k
    }
46
47
    static GuidPrefix_t unknown()
48
0
    {
49
0
        return GuidPrefix_t();
50
0
    }
51
52
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
53
54
    /**
55
     * Guid prefix comparison operator
56
     * @param prefix guid prefix to compare
57
     * @return True if the guid prefixes are equal
58
     */
59
    bool operator ==(
60
            const GuidPrefix_t& prefix) const
61
0
    {
62
0
        return (memcmp(value, prefix.value, size) == 0);
63
0
    }
64
65
    /**
66
     * Guid prefix comparison operator
67
     * @param prefix Second guid prefix to compare
68
     * @return True if the guid prefixes are not equal
69
     */
70
    bool operator !=(
71
            const GuidPrefix_t& prefix) const
72
29.6k
    {
73
29.6k
        return (memcmp(value, prefix.value, size) != 0);
74
29.6k
    }
75
76
    /**
77
     * Guid prefix minor operator
78
     * @param prefix Second guid prefix to compare
79
     * @return True if prefix is higher
80
     */
81
    bool operator <(
82
            const GuidPrefix_t& prefix) const
83
0
    {
84
0
        return std::memcmp(value, prefix.value, size) < 0;
85
0
    }
86
87
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
88
};
89
90
const GuidPrefix_t c_GuidPrefix_Unknown;
91
92
inline std::ostream& operator <<(
93
        std::ostream& output,
94
        const GuidPrefix_t& guiP)
95
0
{
96
0
    output << std::hex;
97
0
    char old_fill = output.fill('0');
98
0
    for (uint8_t i = 0; i < 11; ++i)
99
0
    {
100
0
        output << std::setw(2) << (int)guiP.value[i] << ".";
101
0
    }
102
0
    output << std::setw(2) << (int)guiP.value[11];
103
0
    output.fill(old_fill);
104
0
    return output << std::dec;
105
0
}
106
107
inline std::istream& operator >>(
108
        std::istream& input,
109
        GuidPrefix_t& guiP)
110
0
{
111
0
    std::istream::sentry s(input);
112
113
0
    if (s)
114
0
    {
115
0
        char point;
116
0
        unsigned short hex;
117
0
        std::ios_base::iostate excp_mask = input.exceptions();
118
119
0
        try
120
0
        {
121
0
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
122
0
            input >> std::hex >> hex;
123
124
0
            if (hex > 255)
125
0
            {
126
0
                input.setstate(std::ios_base::failbit);
127
0
            }
128
129
0
            guiP.value[0] = static_cast<octet>(hex);
130
131
0
            for (int i = 1; i < 12; ++i)
132
0
            {
133
0
                input >> point >> hex;
134
0
                if ( point != '.' || hex > 255 )
135
0
                {
136
0
                    input.setstate(std::ios_base::failbit);
137
0
                }
138
0
                guiP.value[i] = static_cast<octet>(hex);
139
0
            }
140
141
0
            input >> std::dec;
142
0
        }
143
0
        catch (std::ios_base::failure& )
144
0
        {
145
0
            guiP = GuidPrefix_t::unknown();
146
0
        }
147
148
0
        input.exceptions(excp_mask);
149
0
    }
150
151
0
    return input;
152
0
}
153
154
} // namespace rtps
155
} // namespace fastrtps
156
} // namespace eprosima
157
158
#endif /* _FASTDDS_RTPS_COMMON_GUIDPREFIX_T_HPP_ */