AMTypeID  201909
String localization library
AMCETypeID.h
Go to the documentation of this file.
1 
8 #ifndef AMCETypeID_H
9 #define AMCETypeID_H
10 
11 #include "amfnv1a/AMCEFNV1a.h"
12 
13 using namespace std;
14 
15 #if defined(__clang__)
16  #define _D_PRETTY_FUNCTION __PRETTY_FUNCTION__
17  #define _D_PRETTY_FUNCTION_PREFIX "static AMCore::AMCETypeID::_T_string AMCore::AMCETypeID<"
18  #define _D_PRETTY_FUNCTION_SEPARATOR1 ">::stringInit() [with T = "
19  #define _D_PRETTY_FUNCTION_PREFIX_I "static int AMCore::AMCETypeID<"
20  #define _D_PRETTY_FUNCTION_SEPARATOR3 ">::typeLen() [T = "
21  #define _D_PRETTY_FUNCTION_SUFFIX "]"
22  #define _D_PRETTY_FUNCTION_LENGTH ((sizeof(_D_PRETTY_FUNCTION) - sizeof(_D_PRETTY_FUNCTION_PREFIX_I) - sizeof(_D_PRETTY_FUNCTION_SUFFIX) - sizeof(_D_PRETTY_FUNCTION_SEPARATOR3) + 3) / 2 + 1)
23  //#error test it!!!
24 #elif defined(__GNUC__) && !defined(__clang__)
25  #define _D_PRETTY_FUNCTION __PRETTY_FUNCTION__
26  #define _D_PRETTY_FUNCTION_PREFIX "static constexpr AMCore::AMCETypeID<T>::_T_string AMCore::AMCETypeID<T>::stringInit() [with T = "
27  #define _D_PRETTY_FUNCTION_PREFIX_I "static constexpr int AMCore::AMCETypeID<T>::typeLen() [with T = "
28  #define _D_PRETTY_FUNCTION_SUFFIX "]"
29  #define _D_PRETTY_FUNCTION_LENGTH sizeof(_D_PRETTY_FUNCTION) - sizeof(_D_PRETTY_FUNCTION_PREFIX_I) - sizeof(_D_PRETTY_FUNCTION_SUFFIX) + 2
30 #elif defined(_MSC_VER)
31  #define _D_PRETTY_FUNCTION __FUNCSIG__
32  #define _D_PRETTY_FUNCTION_PREFIX "struct AMCore::AMCETypeID<T>::_T_string __cdecl AMCore::AMCETypeID<T>::stringInit<"
33  #define _D_PRETTY_FUNCTION_PREFIX_I "struct int __cdecl AMCore::AMCETypeID<T>::typeLen<"
34  #define _D_PRETTY_FUNCTION_SUFFIX ">(void)"
35  #error test it!!!
36 #else
37  #error "Andromeda Library do not support this compiler."
38 #endif
39 
40 #include <iostream>
41 
42 namespace AMCore {
43 
44 
45 #ifdef COLLECTING_DATA
46 
47  template<bool>
48  struct _T_AMTypeId_list_leaf {
49  static _T_AMTypeId_list_leaf* _M_first;
50  _T_AMTypeId_list_leaf* _M_next;
51  const char* _M_name;
52  uint64_t _M_hash;
53  uint64_t _M_id;
54  public:
55  constexpr _T_AMTypeId_list_leaf(const char* name, uint64_t hash):_M_name(name),_M_hash(hash),_M_next(_M_first),_M_id(_M_first ? _M_first->_M_id + 1 : 0) {
56  _M_first = this;
57  }
58  };
59  template<bool b>
60  _T_AMTypeId_list_leaf<b>* _T_AMTypeId_list_leaf<b>::_M_first = nullptr;
61 
62 #endif
63 
70  template<typename T>
71  class AMCETypeID
72  {
73  public:
74  static constexpr int typeLen()
75  {
76  return _D_PRETTY_FUNCTION_LENGTH;
77  }
78  static constexpr int _M_lengthCE = AMCETypeID<T>::typeLen();
79  class _T_string
80  {
81  public:
82  constexpr _T_string(): _M_array() {};
83  char _M_array[AMCETypeID<T>::_M_lengthCE];
84  };
85  static constexpr _T_string cc(_T_string dst, int index,const char* src)
86  {
87  dst._M_array[index] = src[0];
88  if (_M_lengthCE-1 <= index) {
89  return dst;
90  }
91  return cc(dst, index+1, src+1);
92  }
93  static constexpr _T_string stringInit()
94  {
95  _T_string rv;
96  const char* ctype = _D_PRETTY_FUNCTION ;
97  int offset = - 1 + sizeof(_D_PRETTY_FUNCTION_PREFIX);
98  const int length = _M_lengthCE -1;
99  for(int i=0; i < length ; i++,offset++) {
100  rv._M_array[i] = ctype[offset];
101  }
102  rv._M_array[_M_lengthCE-1] = 0;
103  return rv;
104  }
105  static const _T_string _M_name;
106  static constexpr const _T_string _M_nameCE = AMCETypeID<T>::stringInit();
107  static constexpr const uint64_t _M_hashCE = AMCEFNV1aAlgorithm::fnv1a64(_M_nameCE._M_array);
108  static constexpr const uint32_t _M_hashCE32 = AMCEFNV1aAlgorithm::fnv1a32(_M_nameCE._M_array);
109 
110  #ifdef COLLECTING_DATA
111  public:
112  static _T_AMTypeId_list_leaf<true> _leaf;
113  #endif
114  public:
119  static constexpr const char* name()
120  {
121  return _M_name._M_array;
122  #ifdef COLLECTING_DATA
123  const uint64_t static_member_starter = _leaf._M_hash;
124  #endif
125  }
131  static constexpr int nameLength()
132  {
133  return _M_lengthCE;
134  #ifdef COLLECTING_DATA
135  const uint64_t static_member_starter = _leaf._M_hash;
136  #endif
137  }
143  static constexpr uint64_t hash_code()
144  {
145  return _M_hashCE;
146  #ifdef COLLECTING_DATA
147  const uint64_t static_member_starter = _leaf._M_hash;
148  #endif
149  }
155  static constexpr uint32_t hash_code32()
156  {
157  return _M_hashCE32;
158  #ifdef COLLECTING_DATA
159  const uint64_t static_member_starter = _leaf._M_hash;
160  #endif
161  }
175  static constexpr uint64_t id()
176  {
177  #ifdef COLLECTING_DATA
178  return _leaf._M_id;
179  const uint64_t static_member_starter = _leaf._M_hash;
180  #else
181  return _M_hashCE;
182  #endif
183  }
188  static constexpr uint32_t id32()
189  {
190  return (uint32_t)id() & 0xFFFFFFFF;
191  #ifdef COLLECTING_DATA
192  const uint64_t static_member_starter = _leaf._M_hash;
193  #endif
194  }
195  };
196  template<typename T>
197  const typename AMCETypeID<T>::_T_string AMCETypeID<T>::_M_name = AMCETypeID<T>::_M_nameCE;
198  #ifdef COLLECTING_DATA
199  template<typename T>
200  _T_AMTypeId_list_leaf<true> AMCETypeID<T>::_leaf = _T_AMTypeId_list_leaf<true>(AMCETypeID<T>::name(), AMCETypeID<T>::hash_code());
201  #endif
202 
203 
204 }//namespace
205 
206 #endif //AMCETypeID_H
AMCore::AMCETypeID::hash_code
static constexpr uint64_t hash_code()
Hash code 64-bit size.
Definition: AMCETypeID.h:143
AMCore::AMCETypeID
Constant expression id for types Simply, retrieve from given type it's name as a string,...
Definition: AMCETypeID.h:71
AMCore::AMCETypeID::id
static constexpr uint64_t id()
id Practically, id is some index in range in 0 to N. Main purpose is optimize switch statement,...
Definition: AMCETypeID.h:175
AMCore::AMCETypeID::id32
static constexpr uint32_t id32()
lowest 32 bit of id()
Definition: AMCETypeID.h:188
AMCore::AMCETypeID::name
static constexpr const char * name()
Name of class.
Definition: AMCETypeID.h:119
AMCore::AMCETypeID::nameLength
static constexpr int nameLength()
Name length Zero at end is included.
Definition: AMCETypeID.h:131
AMCore::AMCETypeID::hash_code32
static constexpr uint32_t hash_code32()
Hash code 32-bit size.
Definition: AMCETypeID.h:155