PLCnext API Documentation  20.6.0.30321
TypeIdentifier.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Rsc/Services/IRscSerializable.hpp"
9 #include "Arp/System/Rsc/Services/RscReader.hpp"
10 #include "Arp/System/Rsc/Services/RscString.hxx"
11 #include "Arp/System/Rsc/Services/RscWriter.hpp"
12 namespace Arp { namespace Plc { namespace Commons { namespace Meta
13 {
14 
15 using namespace Arp::System::Rsc::Services;
16 
17 // forwards
18 class DataTag;
19 
22 {
23  friend class DataTag;
24 
25 public: // typedefs
26 
27 public: // construction/destruction
29  TypeIdentifier(void) = default;
31  TypeIdentifier(const String& typeName, const char* namespace_, const void* typeTag = nullptr);
33  TypeIdentifier(const TypeIdentifier& arg) = default;
35  TypeIdentifier(TypeIdentifier&& arg) = default;
37  TypeIdentifier& operator=(const TypeIdentifier& arg) = default;
39  TypeIdentifier& operator=(TypeIdentifier&& arg) = default;
41  ~TypeIdentifier(void) = default;
42 
43 public: // static properties
44  static const TypeIdentifier Empty;
45 
46 public: // static operations
47  static TypeIdentifier Create(const String& commonTypeName, const char* libraryNamespace, void* typeTag = nullptr);
48 
49 public: // operators
50  bool operator==(const TypeIdentifier& rhs)const;
51  bool operator<(const TypeIdentifier& rhs)const;
52 
53 public: // setter/getter operations
56  bool IsEmpty(void)const;
59  const String& GetName(void)const;
60 
63  const String& GetNamespace(void)const;
64 
67  String GetFullName(void)const;
68 
71  const byte* GetTypeTag(void)const;
72 
75  const byte* GetChangeCounterpartTypeTag(void)const;
76 
79  size_t GetHashValue(void)const;
80 
83  size_t GetCacheSize()const;
84 
85 public: // operations
86 
89  String ToString(void)const;
90 
93  void SwapDomain(void);
94 
97  TypeIdentifier CreateUniqueComponentTypeIdentifier() const;
98 
99 public: // IRscSerializable operations
100  void Serialize(RscWriter& writer)const override;
101  void Deserialize(RscReader& reader)override;
102  static size_t GetFieldCount(void);
103 
104 private: // methods
105  void SetChangeCounterpartTypeTag(const byte* value);
106 
107 private: // fields
108  String name;
109  String namespace_;
110  const byte* typeTag = nullptr;
111  const byte* changeCounterpartTypeTag = nullptr;
112 
113 private: // static fields
114  static const char namespaceSeparator = '/';
115 };
116 
118 // inline methods of class TypeIdentifier
119 inline TypeIdentifier::TypeIdentifier(const String& name, const char* namespace_, const void* typeTag)
120  : name(name)
121  , namespace_(namespace_)
122  , typeTag(reinterpret_cast<const byte*>(typeTag))
123 {
124 }
125 
126 inline bool TypeIdentifier::IsEmpty()const
127 {
128  return this->name.IsEmpty() && this->namespace_.IsEmpty();
129 }
130 inline const String& TypeIdentifier::GetName()const
131 {
132  return this->name;
133 }
134 
136 {
137  return this->namespace_;
138 }
140 {
141  return this->namespace_ + namespaceSeparator + this->name;
142 }
143 inline const byte* TypeIdentifier::GetTypeTag()const
144 {
145  return this->typeTag;
146 }
147 
149 {
150  return this->changeCounterpartTypeTag;
151 }
152 
153 inline size_t TypeIdentifier::GetHashValue()const
154 {
155  // if (this->typeTag != nullptr) return reinterpret_cast<const size_t>(this->typeTag);
156  // else
157  std::hash<String> gethash;
158  return gethash(this->namespace_) ^ gethash(this->name);
159 }
160 
162 {
163  std::swap(this->typeTag, this->changeCounterpartTypeTag);
164 }
165 
166 inline size_t TypeIdentifier::GetFieldCount()
167 {
168  return 4;
169 }
170 
171 inline void TypeIdentifier::SetChangeCounterpartTypeTag(const byte* value)
172 {
173  this->changeCounterpartTypeTag = value;
174 }
175 
176 inline size_t TypeIdentifier::GetCacheSize()const
177 {
178  size_t cacheSize = 0;
179  cacheSize += this->name.Capacity();
180  cacheSize += this->namespace_.Capacity();
181  cacheSize += sizeof(this->typeTag);
182  cacheSize += sizeof(this->changeCounterpartTypeTag);
183  cacheSize += sizeof(this->namespaceSeparator);
184 
185  return cacheSize;
186 }
187 
188 inline bool TypeIdentifier::operator==(const TypeIdentifier& rhs)const
189 {
190  //if (this->typeTag != nullptr)
191  //{
192  // return this->typeTag == rhs.typeTag;
193  //}
194  // else
195  return this->name == rhs.name && this->namespace_ == rhs.namespace_;
196 }
197 
198 inline bool TypeIdentifier::operator<(const TypeIdentifier& rhs)const
199 {
200  // just sort using names w/o typeTag
201  return (this->namespace_ == rhs.namespace_) ? (this->name < rhs.name) : (this->namespace_ < rhs.namespace_);
202 }
203 
206 inline std::ostream& operator<<(std::ostream& os, const TypeIdentifier& typeId)
207 {
208  os << typeId.ToString();
209  return os;
210 }
211 
212 }}}} // end of namespace Arp::Plc::Meta
213 
214 namespace std
215 {
216 
217 // hash functor for TypeIdentifier
218 template<>
219 struct hash<Arp::Plc::Commons::Meta::TypeIdentifier>
220 {
221 private: // usings
223 
224 public:
226  typedef size_t result_type;
227 
228 public:
229  result_type operator()(const argument_type& key) const
230  {
231  return key.GetHashValue();
232  }
233 };
234 }
Reads data from Rsc
Definition: RscReader.hpp:23
Includes kind as well as instance browse name.
Definition: DataTag.hpp:24
Includes name and namespace of type object.
Definition: TypeIdentifier.hpp:21
Writes data to Rsc.
Definition: RscWriter.hpp:32
Namespace for classes and interfaces for the Remote Service Call implementation
Namespace of the C++ standard library
bool IsEmpty(void) const
Checks if this instance is empty.
Definition: TypeIdentifier.hpp:126
size_t GetCacheSize() const
Get the cache size
Definition: TypeIdentifier.hpp:176
String ToString(void) const
Get full qualified type name and namespace.
size_t GetHashValue(void) const
Gets the hash value of this instance.
Definition: TypeIdentifier.hpp:153
void SwapDomain(void)
Swaps the type tag with its counterpart.
Definition: TypeIdentifier.hpp:161
bool operator<(Enum< T > lhs, Enum< T > rhs)
Less operator for class Enum.
Definition: Enum.hxx:228
const String & GetNamespace(void) const
Get type namespace of the corresponding object.
Definition: TypeIdentifier.hpp:135
String GetFullName(void) const
Get fullqualified type name and namespace.
Definition: TypeIdentifier.hpp:139
const String & GetName(void) const
Get type name of the corresponding object.
Definition: TypeIdentifier.hpp:130
Marshalls structure or class data types. Serialize and Deserialize have to marshal fields in the same...
Definition: IRscSerializable.hpp:18
const byte * GetTypeTag(void) const
Get the internal type dataTag of the corresponding object.
Definition: TypeIdentifier.hpp:143
Root namespace for the PLCnext API
std::ostream & operator<<(std::ostream &os, Enum< T > value)
Makes the Enum class loggable and streamable.
Definition: Enum.hxx:208
TypeIdentifier(void)=default
Constructs an TypeId instance.
bool operator==(Enum< T > lhs, Enum< T > rhs)
Equality operator for class Enum.
Definition: Enum.hxx:218
const byte * GetChangeCounterpartTypeTag(void) const
Get the internal type tag of the change counterpart.
Definition: TypeIdentifier.hpp:148
unsigned char byte
The Arp character type.
Definition: PrimitiveTypes.hpp:23