PLCnext API Documentation 25.0.2.69
IdType.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6
7#pragma once
8
10#include <functional>
11#include "Arp/System/Core/Enum.hxx"
12#include <limits>
13#include <vector>
14
15
16namespace Arp { namespace System { namespace Nm { namespace Internal
17{
20template<typename T>
21class IdGenerator;
22} // namespace Internal
23
24
28template<typename T, typename TypeTag>
29class ARP_CXX_SYMBOL_EXPORT IdType
30{
31 static_assert(std::is_unsigned<T>::value, "T must be an unsigned integer type");
32 static_assert(std::is_same<T, bool>::value == 0, "Cannot be used with bool");
33
34public:
35 using type = T;
37
38
40 constexpr IdType() noexcept = default;
41
45 constexpr explicit IdType(T value) noexcept : Value(value)
46 {
47 }
48
49 constexpr IdType(const self_type&) noexcept = default;
50
51 IdType(IdType&& other) noexcept : Value(other.Value)
52 {
53 other.Value = self_type().Value;
54 }
55
56 ~IdType() noexcept = default;
57
58
59 self_type& operator=(const self_type&) noexcept = default;
60
61 self_type& operator=(IdType&& other) noexcept
62 {
63 this->Value = other.Value;
64 other.Value = self_type().Value;
65 return *this;
66 }
67
68
69 constexpr bool operator<(const self_type& rhs) const noexcept
70 {
71 return this->Value < rhs.Value;
72 }
73
74
75 constexpr bool operator==(const self_type& rhs) const noexcept
76 {
77 return this->Value == rhs.Value;
78 }
79
80
81 constexpr bool operator!=(const self_type& rhs) const noexcept
82 {
83 return this->Value != rhs.Value;
84 }
85
86
88 constexpr bool IsValid() const noexcept
89 {
90 return this->Value != self_type().Value;
91 }
92
93
95 constexpr T GetValue() const noexcept
96 {
97 return this->Value;
98 }
99
100
101private:
102 constexpr static const self_type GetIncrementedId(const self_type& id) noexcept
103 {
104 return self_type(static_cast<T>(id.Value + 1));
105 }
106
107
108 constexpr static const self_type CreateInitalId() noexcept
109 {
110 return GetIncrementedId(self_type());
111 }
112
113
114 constexpr const self_type CreateNextIdUnique() const noexcept
115 {
116 return GetIncrementedId(*this);
117 }
118
119
120 constexpr const self_type CreateNextId() const noexcept
121 {
122 return this->IsValid() ? this->CreateNextIdUnique() : self_type();
123 }
124
125
126private:
127 T Value = std::numeric_limits<T>::min();
128
129 friend Internal::IdGenerator<self_type>;
130};
131
132
133template<typename T, typename TypeTag>
134std::ostream& operator<<(std::ostream& os, const Arp::System::Nm::IdType<T, TypeTag>& id)
135{
136 os << id.GetValue();
137 return os;
138}
139
140
143template<typename IdType>
144std::vector<typename IdType::type> ConvertIdTypesToRawIds(const std::vector<IdType>& ids)
145{
146 std::vector<typename IdType::type> result;
147 result.reserve(ids.size());
148 for (const IdType& each : ids)
149 {
150 result.emplace_back(each.GetValue());
151 }
152 return result;
153}
154
155
158template<typename IdType>
159std::vector<IdType> ConvertRawIdsToIdTypes(const std::vector<typename IdType::type>& ids)
160{
161 std::vector<IdType> result;
162 result.reserve(ids.size());
163 for (const typename IdType::type& each : ids)
164 {
165 result.emplace_back(IdType(each));
166 }
167 return result;
168}
169
170
171}}} // end of namespace Arp::System::Nm
172
173
174namespace std
175{
177template<typename T, typename TypeTag>
178struct hash<Arp::System::Nm::IdType<T, TypeTag>>
179{
181 using result_type = std::size_t;
182 result_type operator()(const arguement_type& id) const noexcept
183 {
184 return std::hash<T>()(id.GetValue());
185 }
186};
187} // namespace std
188
189template<typename T, typename TypeTag>
190struct fmt::formatter<Arp::System::Nm::IdType<T, TypeTag>>: public fmt::ostream_formatter {};
Representation of an unique id
Definition: IdType.hpp:30
constexpr T GetValue() const noexcept
Returns the underlying value
Definition: IdType.hpp:95
constexpr bool IsValid() const noexcept
Returns true if this object represents a valid id (!= 0)
Definition: IdType.hpp:88
constexpr IdType() noexcept=default
Creates an invalid id
std::vector< typename IdType::type > ConvertIdTypesToRawIds(const std::vector< IdType > &ids)
Converts a vector of IdType to a vector of the underlying type
Definition: IdType.hpp:144
std::vector< IdType > ConvertRawIdsToIdTypes(const std::vector< typename IdType::type > &ids)
Converts a vector of underlying ids to a vector of IdType
Definition: IdType.hpp:159
Root namespace for the PLCnext API
Namespace of the C++ standard library