PLCnext API Documentation 24.0.0.71
TypeName.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/Exception.hpp"
9#include "Arp/System/Core/RuntimeTypeInfo.hpp"
10#include <typeinfo>
11
12namespace Arp
13{
14
16
17namespace __Internal
18{
19
21struct TypeNameHelper
22{
23 static size_t FindNamespaceEndPosition(const String& typeName, bool isCommon)
24 {
25 char separator = isCommon ? '.' : ':';
26
27 size_t lastPos = typeName.FindFirstOf('<'); // template class?
28 if (lastPos == String::NPos)
29 {
30 return typeName.FindLastOf(separator);
31 }
32 // else
33 return typeName.FindLastOf(separator, lastPos);
34 }
35
36 static String GetNamespace(const String& typeName, bool isCommon)
37 {
38 size_t pos = FindNamespaceEndPosition(typeName, isCommon);
39 if (pos == String::NPos)
40 {
41 return String::Empty;
42 }
43 // else
44 return isCommon ? typeName.Substr(0, pos) : typeName.Substr(0, pos - 1);
45 }
46
47 static String GetClassName(const String& typeName, bool isCommon)
48 {
49 size_t pos = FindNamespaceEndPosition(typeName, isCommon);
50 if (pos == String::NPos)
51 {
52 return typeName; // no namespace present
53 }
54 // else
55 return typeName.Substr(pos + 1);
56 }
57};
58} // end of namespace Internal
65template<class T = void>
67{
68 friend class TypeName<void>;
69
70public: // construction/destruction
72 TypeName(void);
75 TypeName(const TypeName& arg) = default;
78 TypeName(TypeName&& arg)noexcept = default;
82 TypeName& operator=(const TypeName& arg) = default;
84 ~TypeName(void) = default;
85
86public: // operations
89 String GetFullName(void)const;
92 String GetCommonName(void)const;
96 String GetSafeName(void)const;
99 String GetNamespace(void)const;
102 String GetClassName(void)const;
103
104public: // operators
107 operator const String& (void)const;
110 operator const char*(void)const;
114 bool operator==(const TypeName& other);
118 bool operator!=(const TypeName& other);
119
120private: // construction
121 TypeName(const char* typeName, bool isCommon = false);
122
123private: // methods
124 void AssignTypeName(const char* typeName, bool isCommon);
125
126public: // fields
129
130private: // fields
131 bool isCommon = false;
132};
133
135// inline methods of class TypeName<T>
136
137template<class T>
139{
140 this->AssignTypeName(typeid(T).name(), false);
141}
142
143template<class T>
144inline TypeName<T>::TypeName(const char* typeName, bool isCommonArg)
145{
146 this->AssignTypeName(typeName, isCommonArg);
147}
148
149template<class T>
151{
152 return this->Value;
153}
154
155template<class T>
157{
158 String result(this->Value);
159 result.ReplaceAll("::", ".");
160 return result;
161}
162
163template<class T>
165{
166 String result(this->Value);
167
168 if (this->isCommon)
169 {
170 result.ReplaceAll(".", "_");
171 }
172 else
173 {
174 result.ReplaceAll("::", "_");
175 }
176 result.ReplaceAll("<", "$");
177 result.ReplaceAll(">", "$");
178
179 return result;
180}
181
182template<class T>
184{
185 return __Internal::TypeNameHelper::GetNamespace(this->Value, this->isCommon);
186}
187
188template<class T>
190{
191 return __Internal::TypeNameHelper::GetClassName(this->Value, this->isCommon);
192}
193
194template<class T>
195inline void TypeName<T>::AssignTypeName(const char* typeName, bool isCommonArg)
196{
197 this->isCommon = isCommonArg;
198
199 if (this->isCommon)
200 {
201 this->Value = typeName; // name was demangled yet
202 }
203 else
204 {
205 this->Value = RuntimeTypeInfo::DemangleSymbol(typeName);
206 if (this->Value.IsEmpty())
207 {
208 throw Exception("Demangling of type name '{0}' failed.", typeid(T).name());
209 }
210
211
212 // remove any kind of type 'class', 'struct' or 'enum' from begin of string
213 Value.ReplaceAll("class ", String::Empty);
214 Value.ReplaceAll("struct ", String::Empty);
215 Value.ReplaceAll("enum ", String::Empty);
216 }
217}
218
219template<class T>
220inline TypeName<T>::operator const String& ()const
221{
222 return this->Value;
223}
224
225template<class T>
226inline TypeName<T>::operator const char*()const
227{
228 return this->Value.CStr();
229}
230
231template<class T>
232inline bool TypeName<T>::operator==(const TypeName& other)
233{
234 return this->Value == other.Value;
235}
236
237template<class T>
238inline bool TypeName<T>::operator!=(const TypeName& other)
239{
240 return !(*this == other);
241}
242
247template<class T>
248inline std::ostream& operator<<(std::ostream& os, const TypeName<T>& typeName)
249{
250 os << typeName.Value;
251 return os;
252}
253
255// implementation of template specialization TypeName<void>
256
258template<>
259class TypeName<void>
260{
261private: // construction/destruction
262 TypeName(void) = delete;
263 TypeName(const TypeName& arg) = delete;
264 TypeName& operator=(const TypeName& arg) = delete;
265 ~TypeName(void) = delete;
266
267public: // static factory operations
272 template <class T2>
273 static TypeName<T2> GetFrom(T2& instance)
274 {
275 return GetFrom(&instance);
276 }
277
282 template <class T2>
283 static TypeName<T2> GetFrom(T2* pInstance)
284 {
285 const char* pResult = typeid(*pInstance).name();
286 pInstance = nullptr;
287 return TypeName<T2>(pResult);
288 }
289
294 static String GetNamespace(const String& typeName, bool isCommon)
295 {
296 return __Internal::TypeNameHelper::GetNamespace(typeName, isCommon);
297 }
298
303 static String GetClassName(const String& typeName, bool isCommon)
304 {
305 return __Internal::TypeNameHelper::GetClassName(typeName, isCommon);
306 }
307};
308
309} // end of namespace Arp
static String DemangleSymbol(const char *symbolName)
Demangles a symbol
static String GetClassName(const String &typeName, bool isCommon)
Extracts the classname of the given typename.
Definition: TypeName.hxx:303
static TypeName< T2 > GetFrom(T2 *pInstance)
Creates the TypeName from the as argument passed variable pointer.
Definition: TypeName.hxx:283
static String GetNamespace(const String &typeName, bool isCommon)
Extracts the namespace of the given typename.
Definition: TypeName.hxx:294
static TypeName< T2 > GetFrom(T2 &instance)
Creates the TypeName from the as argument passed variable.
Definition: TypeName.hxx:273
This (meta programming) class provides the C++ typename of the as template argument passed type.
Definition: TypeName.hxx:67
String GetNamespace(void) const
Gets the namespace of the as template parameter given type.
Definition: TypeName.hxx:183
TypeName(TypeName &&arg) noexcept=default
The default move constructor.
String Value
The resulting typename.
Definition: TypeName.hxx:128
String GetSafeName(void) const
Gets a safe name of the as template parameter given type.
Definition: TypeName.hxx:164
bool operator!=(const TypeName &other)
Determines if this instance is not equal to other .
Definition: TypeName.hxx:238
String GetCommonName(void) const
Gets the common name of the as template parameter given type according to the CLS.
Definition: TypeName.hxx:156
TypeName & operator=(const TypeName &arg)=default
The default assignment operator.
~TypeName(void)=default
The default destructor..
TypeName(const TypeName &arg)=default
The default copy constructor.
String GetFullName(void) const
Gets the full typename of this instance.
Definition: TypeName.hxx:150
TypeName(void)
Constructs a CommonTypeName instance and determines the typename of the of T.
Definition: TypeName.hxx:138
String GetClassName(void) const
Gets the classname of the as template parameter given type.
Definition: TypeName.hxx:189
bool operator==(const TypeName &other)
Determines if this instance is equal to other .
Definition: TypeName.hxx:232
SelfType Substr(size_type offset=0, size_type count=NPos) const
Gets a substring of this string.
Definition: BasicString.hxx:1550
static const size_type NPos
This position value is returned when find operations do not match, or is used as default value for an...
Definition: BasicString.hxx:221
std::ostream & operator<<(std::ostream &os, const BasicString< CharType, Alloc > &right)
Streams the right string to the outstream os .
Definition: BasicString.hxx:1740
SelfType & ReplaceAll(const SelfType &pattern, const SelfType &replacement)
Replaces a given pattern by a replacement string.
Definition: BasicString.hxx:824
static const SelfType Empty
An emtpy static string instance.
Definition: BasicString.hxx:225
Arp::BasicString< char8 > String
The Arp String class.
Definition: TypeSystem.h:27
Root namespace for the PLCnext API