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