-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirror.h
2006 lines (1679 loc) · 50.9 KB
/
mirror.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
//*****************************************************************************
// mirror - lightweight single header C++ reflection library
//*****************************************************************************
//-----------------------------------------------------------------------------
// Licence
//-----------------------------------------------------------------------------
/*
The MIT License (MIT)
Copyright © 2023 Rémi Bismuth
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software is provided “as is”, without warranty of any kind, express or implied,
including but not limited to the warranties of merchantability, fitness for a
particular purpose and noninfringement. In no event shall the authors or copyright
holders be liable for any claim, damages or other liability, whether in an action
of contract, tort or otherwise, arising from, out of or in connection with the
software or the use or other dealings in the Software.
*/
//*****************************************************************************
// Header
//*****************************************************************************
//-----------------------------------------------------------------------------
// Header and includes
//-----------------------------------------------------------------------------
// Dll import/export markup
#ifndef MIRROR_API
#define MIRROR_API
#endif
#include <cstdint>
#include <vector>
#include <set>
#include <unordered_map>
#include <type_traits>
#include <typeinfo>
#include <assert.h>
//-----------------------------------------------------------------------------
// Mirror
//-----------------------------------------------------------------------------
namespace mirror {
//-----------------------------------------------------------------------------
// Forward Declarations & Types
//-----------------------------------------------------------------------------
typedef size_t TypeID;
class TypeSet;
class Type;
class Class;
class Enum;
class Pointer;
class StaticFunction;
class FixedSizeArray;
class ClassMember;
class EnumValue;
class VirtualTypeWrapper;
struct MetaData;
struct MetaDataSet;
typedef void* (*AllocateFunction)(size_t _size, void* _userData);
const TypeID UNDEFINED_TYPEID = 0;
//-----------------------------------------------------------------------------
// Enums
//-----------------------------------------------------------------------------
enum TypeInfo
{
TypeInfo_none = 0,
TypeInfo_void,
TypeInfo_bool,
TypeInfo_char,
TypeInfo_int8,
TypeInfo_int16,
TypeInfo_int32,
TypeInfo_int64,
TypeInfo_uint8,
TypeInfo_uint16,
TypeInfo_uint32,
TypeInfo_uint64,
TypeInfo_float,
TypeInfo_double,
TypeInfo_Enum,
TypeInfo_Class,
TypeInfo_Pointer,
TypeInfo_FixedSizeArray,
TypeInfo_StaticFunction,
TypeInfo_Custom,
TypeInfo_COUNT,
};
//-----------------------------------------------------------------------------
// Helpers & Tools
//-----------------------------------------------------------------------------
#define _MIRROR_CAT(a, b) a##b
#define MIRROR_CAT(a, b) _MIRROR_CAT(a, b)
MIRROR_API uint32_t Hash32(const void* _data, size_t _size);
MIRROR_API uint32_t HashCString(const char* _str);
MIRROR_API const char* TypeInfoToString(TypeInfo _type);
//-----------------------------------------------------------------------------
// Global Functions
//-----------------------------------------------------------------------------
MIRROR_API void InitNewTypes();
template <typename T> constexpr TypeID GetTypeID();
template <typename T> constexpr TypeID GetTypeID(T&);
template <typename T> Type* GetType();
template <typename T> Type* GetType(T&);
MIRROR_API Type* GetType(TypeID _id);
template<typename T> Enum* GetEnum();
template<typename T> Enum* GetEnum(T&);
template <typename T> Class* GetClass();
template <typename T> Class* GetClass(T&);
template <typename DestType, typename SourceType> DestType Cast(SourceType _o);
MIRROR_API Type* FindTypeByName(const char* _name);
MIRROR_API Class* FindClassByName(const char* _name);
MIRROR_API Type* FindTypeByID(TypeID _id);
MIRROR_API Class* AsClass(TypeID _id);
MIRROR_API Enum* AsEnum(TypeID _id);
MIRROR_API Pointer* AsPointer(TypeID _id);
MIRROR_API StaticFunction* AsStaticFunction(TypeID _id);
MIRROR_API FixedSizeArray* AsFixedSizeArray(TypeID _id);
MIRROR_API TypeSet& GetTypeSet();
//-----------------------------------------------------------------------------
// Meta Data
//-----------------------------------------------------------------------------
struct MIRROR_API MetaData
{
const char* getName() const;
bool asBool() const;
int asInt() const;
float asFloat() const;
const char* asString() const;
// internal
MetaData(const char* _name, const char* _data);
MetaData(const MetaData& _other);
~MetaData();
char* m_name;
char* m_data;
};
struct MIRROR_API MetaDataSet
{
const MetaData* findMetaData(const char* _key) const;
// internal
MetaDataSet(const char* _metaDataString);
MetaDataSet(const MetaDataSet& _other);
MetaDataSet& operator=(const MetaDataSet& _other) = default;
std::unordered_map<uint32_t, MetaData> m_metaData;
};
//-----------------------------------------------------------------------------
// TypeSet
//-----------------------------------------------------------------------------
class MIRROR_API TypeSet
{
public:
~TypeSet();
Type* findTypeByID(TypeID _typeID) const;
Type* findTypeByName(const char* _name) const;
void addType(Type* _type);
void addTypeName(Type* _type, const char* _name);
void removeType(Type* _type);
const std::set<Type*>& getTypes() const;
void initNewTypes();
//private:
std::set<Type*> m_types;
std::unordered_map<TypeID, Type*> m_typesByID;
std::unordered_map<TypeID, int> m_typesRegistrationCount;
std::unordered_map<uint32_t, Type*> m_typesByName;
};
extern TypeSet* g_typeSetPtr;
//-----------------------------------------------------------------------------
// Type
//-----------------------------------------------------------------------------
class MIRROR_API Type
{
public:
TypeInfo getTypeInfo() const;
const char* getName() const;
const char* getCustomTypeName() const;
bool isCustomType(const char* _customTypeName) const;
TypeID getTypeID() const;
size_t getSize() const;
const Class* asClass() const;
const Enum* asEnum() const;
const Pointer* asPointer() const;
const StaticFunction* asStaticFunction() const;
const FixedSizeArray* asFixedSizeArray() const;
Class* asClass();
Enum* asEnum();
Pointer* asPointer();
StaticFunction* asStaticFunction();
FixedSizeArray* asFixedSizeArray();
// @TODO(2021/02/15|Remi): Allow the user to choose their allocator
bool hasFactory() const;
void* instantiate(AllocateFunction _allocateFunction = nullptr, void* _userData = nullptr) const;
// internal
void setName(const char* _name);
void setCustomTypeName(const char* _name);
virtual void shutdown();
virtual void init();
Type(TypeInfo _typeInfo);
Type(TypeInfo _typeInfo, const char* _name);
virtual ~Type();
template <typename T> void createVirtualTypeWrapper();
char* m_name = nullptr;
char* m_customTypeName = nullptr;
TypeInfo m_typeInfo = TypeInfo_none;
VirtualTypeWrapper* m_virtualTypeWrapper = nullptr;
bool m_initialized = false;
};
// @NOTE(remi): Need to rewrite this a bit after
class MIRROR_API VirtualTypeWrapper
{
public:
TypeID getTypeID() const { return m_typeID; }
size_t getSize() const { return m_size; }
virtual bool hasFactory() const { return false; }
virtual void* instantiate(AllocateFunction _allocateFunction = nullptr, void* _userData = nullptr) const { return nullptr; }
virtual Class* unsafeVirtualGetClass(void* _object) const { return nullptr; }
virtual ~VirtualTypeWrapper() {}
TypeID m_typeID = UNDEFINED_TYPEID;
size_t m_size = 0;
};
//-----------------------------------------------------------------------------
// Specialized Type Descs
//-----------------------------------------------------------------------------
// --- Class
class MIRROR_API Class : public Type
{
public:
size_t getMembersCount(bool _includeInheritedMembers = true) const;
std::vector<ClassMember*> getMembers(bool _includeInheritedMembers = true) const;
size_t getMembers(ClassMember** _outMemberList, size_t _memberListSize, bool _includeInheritedMembers = true) const;
void getMembers(std::vector<ClassMember*>& _outMemberList, bool _includeInheritedMembers = true) const;
ClassMember* findMemberByName(const char* _name, bool _includeInheritedMembers = true) const;
Class* getParent() const;
TypeID getParentID() const;
const std::set<TypeID>& getParents() const;
const std::set<TypeID>& getChildren() const;
bool isChildOf(const Class* _class, bool _checkSelf = true) const;
const MetaDataSet& getMetaDataSet() const;
Class* unsafeVirtualGetClass(void* _object) const;
// internal
void addMember(ClassMember* _member);
void addParent(TypeID _parent);
Class(const char* _name, const char* _metaDataString);
Class(const char* _name, const MetaDataSet& _metaDataSet);
virtual void shutdown() override;
virtual void init() override;
virtual ~Class();
std::set<TypeID> m_parents;
std::set<TypeID> m_children;
std::vector<ClassMember*> m_members;
std::unordered_map<uint32_t, ClassMember*> m_membersByName;
MetaDataSet m_metaDataSet;
};
class MIRROR_API ClassMember
{
public:
const char* getName() const;
Class* getOwnerClass() const;
size_t getOffset() const;
Type* getType() const;
void* getInstanceMemberPointer(void* _classInstancePointer) const;
const MetaDataSet& getMetaDataSet() const;
// internal
ClassMember(const char* _name, size_t _offset, TypeID _type, const char* _metaDataString);
~ClassMember();
Class* m_ownerClass = nullptr;
char* m_name;
size_t m_offset;
TypeID m_typeInfo = UNDEFINED_TYPEID;
MetaDataSet m_metaDataSet;
};
struct ClassInitializerBase {};
template <typename T> struct ClassInitializer : public ClassInitializerBase {};
// --- Enum
class MIRROR_API Enum : public Type
{
public:
template <typename T> bool getValueFromString(const char* _string, T& _outValue) const;
template <typename T> bool getStringFromValue(T _value, const char*& _outString) const;
const std::vector<EnumValue*>& getValues() const;
Type* getSubType() const;
// internal
void addValue(EnumValue* _value);
Enum(const char* _name, TypeID _subType = UNDEFINED_TYPEID);
std::vector<EnumValue*> m_values;
std::unordered_map<size_t, EnumValue*> m_valuesByNameHash;
TypeID m_subType;
};
class MIRROR_API EnumValue
{
public:
const char* getName() const;
int64_t getValue() const;
// internal
EnumValue(const char* _name, int64_t _value);
~EnumValue();
char* m_name;
int64_t m_value;
};
// --- Pointer
class MIRROR_API Pointer : public Type
{
public:
Type* getSubType() const;
// internal
Pointer(TypeID _subType);
virtual void init() override;
TypeID m_subType = UNDEFINED_TYPEID;
};
// --- Fixed Size Array
class MIRROR_API FixedSizeArray : public Type
{
public:
Type* getSubType() const;
size_t getElementCount() const { return m_elementCount; }
void* getDataAt(void* _basePtr, size_t _index) const;
// internal
FixedSizeArray(TypeID _subType, size_t _elementCount);
virtual void init() override;
TypeID m_subType = UNDEFINED_TYPEID;
size_t m_elementCount;
};
} // namespace mirror
//*****************************************************************************
// REFLECTION MACROS
//*****************************************************************************
// Disable some warnings when expanding macros
#if defined(__clang__)
#define MIRROR_PUSH_DISABLE_WARNINGS \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Winconsistent-missing-override\"")
#define MIRROR_POP_DISABLE_WARNINGS \
_Pragma("clang diagnostic pop")
#elif defined(__GNUC__)
#define MIRROR_PUSH_DISABLE_WARNINGS \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Winconsistent-missing-override\"")
#define MIRROR_POP_DISABLE_WARNINGS \
_Pragma("GCC diagnostic pop")
#else
#define MIRROR_PUSH_DISABLE_WARNINGS
#define MIRROR_POP_DISABLE_WARNINGS
#endif
//-----------------------------------------------------------------------------
// Public Macros
//-----------------------------------------------------------------------------
#define MIRROR_CLASS(_class, ...) \
namespace mirror { \
template <> \
struct ClassInitializer<_class> : public ClassInitializerBase \
{ \
ClassInitializer(); \
~ClassInitializer() \
{ \
GetTypeSet().removeType(clss); \
delete clss; \
clss = nullptr; \
} \
Class* clss = nullptr;\
}; \
ClassInitializer<_class> MIRROR_CAT(initializer_,__LINE__); \
\
ClassInitializer<_class>::ClassInitializer() \
{ \
const char* metaDataString = #__VA_ARGS__""; \
mirror::MetaDataSet metaDataSet(metaDataString); \
clss = new ::mirror::Class(#_class, metaDataSet); \
clss->createVirtualTypeWrapper<_class>(); \
char fakePrototype[sizeof(_class)] = {}; \
_class* prototypePtr = reinterpret_cast<_class*>(fakePrototype); \
(void)prototypePtr; \
{ \
__MIRROR_CLASS_CONTENT
#define __MIRROR_CLASS_CONTENT(...) \
__VA_ARGS__ \
} \
GetTypeSet().addType(clss); \
} \
} \
#define MIRROR_GETCLASS_VIRTUAL() \
MIRROR_PUSH_DISABLE_WARNINGS \
public: \
virtual ::mirror::Class* getClass() const { return ::mirror::GetClass(*this); } \
MIRROR_POP_DISABLE_WARNINGS \
#define MIRROR_GETCLASS() \
public: \
::mirror::Class* getClass() const { return ::mirror::GetClass(*this); } \
#define MIRROR_FRIEND() \
friend struct ::mirror::ClassInitializerBase; \
#define MIRROR_MEMBER(_memberName, ...) \
{ \
::mirror::GetType(prototypePtr->_memberName); \
size_t offset = reinterpret_cast<size_t>(&(prototypePtr->_memberName)) - reinterpret_cast<size_t>(prototypePtr); \
const char* memberName = #_memberName; \
const char* metaDataString = #__VA_ARGS__""; \
::mirror::ClassMember* classMember = new ::mirror::ClassMember(memberName, offset, ::mirror::GetTypeID(prototypePtr->_memberName), metaDataString); \
clss->addMember(classMember); \
} \
#define MIRROR_PARENT(_parentClass) \
{ \
clss->addParent(::mirror::GetTypeID<_parentClass>()); \
} \
#define MIRROR_ENUM(_enumName) \
namespace MIRROR_CAT(__Mirror, _enumName) { \
struct Initializer \
{\
mirror::Enum* enm = nullptr; \
~Initializer() \
{ \
::mirror::GetTypeSet().removeType(enm); \
delete enm; \
} \
Initializer() \
{ \
using enumType = _enumName; \
::mirror::Type* subType; \
switch(sizeof(enumType)) { \
case 1: subType = ::mirror::TypeGetter<int8_t>::Get(); break; \
case 2: subType = ::mirror::TypeGetter<int16_t>::Get(); break; \
case 4: subType = ::mirror::TypeGetter<int32_t>::Get(); break; \
case 8: subType = ::mirror::TypeGetter<int64_t>::Get(); break; \
} \
enm = new ::mirror::Enum(#_enumName, subType->getTypeID()); \
enm->createVirtualTypeWrapper<enumType>(); \
__MIRROR_ENUM_CONTENT
#define __MIRROR_ENUM_CONTENT(...) \
__VA_ARGS__ \
mirror::GetTypeSet().addType(enm); \
}\
} initializer; \
} // namespace __Mirror##_enumName
#define MIRROR_ENUM_VALUE(_enumValue) \
enm->addValue(new ::mirror::EnumValue(#_enumValue, int64_t(_enumValue))); \
//*****************************************************************************
// Inline Implementations
//*****************************************************************************
#include <typeinfo>
namespace mirror {
//-----------------------------------------------------------------------------
// GetTypeID
//-----------------------------------------------------------------------------
template <typename T>
constexpr TypeID GetTypeID()
{
return typeid(T).hash_code();
}
template <typename T>
constexpr TypeID GetTypeID(T&)
{
return GetTypeID<T>();
}
//-----------------------------------------------------------------------------
// Type
//-----------------------------------------------------------------------------
template <typename T>
void Type::createVirtualTypeWrapper()
{
if (m_virtualTypeWrapper == nullptr)
{
m_virtualTypeWrapper = new TVirtualTypeWrapper<typename std::remove_const<T>::type>();
}
}
//-----------------------------------------------------------------------------
// Virtual Type Wrapper
//-----------------------------------------------------------------------------
template <typename T, typename IsShallow = void>
class TVirtualTypeWrapper : public VirtualTypeWrapper
{
public:
TVirtualTypeWrapper()
{
m_typeID = GetTypeID<T>();
m_size = sizeof(T);
}
virtual bool hasFactory() const override { return true; }
virtual void* instantiate(AllocateFunction _allocateFunction = nullptr, void* _userData = nullptr) const override
{
if (_allocateFunction == nullptr)
{
return new T();
}
else
{
T* memory = (T*)_allocateFunction(sizeof(T), _userData);
new (memory) T();
return memory;
}
}
};
template <typename T>
class TVirtualTypeWrapper<T, std::enable_if_t<std::is_function<T>::value || std::is_void<T>::value>> : public VirtualTypeWrapper
{
public:
TVirtualTypeWrapper()
{
m_typeID = GetTypeID<T>();
m_size = 0;
}
virtual bool hasFactory() const override { return false; }
virtual void* instantiate(AllocateFunction _allocateFunction = nullptr, void* _userData = nullptr) const override { return nullptr; }
};
//-----------------------------------------------------------------------------
// Global Functions
//-----------------------------------------------------------------------------
// --- TypeGetter
template <typename T>
struct CustomTypeFactory
{
static Type* Create()
{
return nullptr;
}
};
template <typename T, typename IsArray = void, typename IsPointer = void, typename IsEnum = void, typename IsFunction = void>
struct TypeGetter
{
static Type* Get()
{
TypeID typeID = GetTypeID<T>();
Type* typeDesc = GetTypeSet().findTypeByID(typeID);
if (typeDesc == nullptr)
{
typeDesc = CustomTypeFactory<T>::Create();
if (typeDesc != nullptr)
{
typeDesc->createVirtualTypeWrapper<T>();
GetTypeSet().addType(typeDesc);
}
}
return typeDesc;
}
};
template <typename T>
struct FixedSizeArrayInitializer
{
FixedSizeArrayInitializer()
{
using type = typename std::remove_extent<T>::type;
typeDesc = new FixedSizeArray(GetTypeID<type>(), std::extent<T>::value);
typeDesc->createVirtualTypeWrapper<T>();
GetTypeSet().addType(typeDesc);
}
~FixedSizeArrayInitializer()
{
GetTypeSet().removeType(typeDesc);
delete typeDesc;
}
FixedSizeArray* typeDesc = nullptr;
};
template <typename T>
struct TypeGetter<T, std::enable_if_t<std::is_array<T>::value>>
{
static Type* Get()
{
static FixedSizeArrayInitializer<T> s_FixedSizeArrayInitializer;
return GetTypeSet().findTypeByID(GetTypeID<T>());
}
};
template <typename T>
struct PointerInitializer
{
PointerInitializer()
{
using type = typename std::remove_pointer<T>::type;
typeDesc = new Pointer(TypeGetter<type>::Get()->getTypeID());
typeDesc->createVirtualTypeWrapper<T>();
GetTypeSet().addType(typeDesc);
}
~PointerInitializer()
{
GetTypeSet().removeType(typeDesc);
delete typeDesc;
}
Pointer* typeDesc = nullptr;
};
template <typename T>
struct TypeGetter<T, void, std::enable_if_t<std::is_pointer<T>::value>>
{
static Type* Get()
{
static PointerInitializer<T> s_PointerInitializer;
return GetTypeSet().findTypeByID(GetTypeID<T>());
}
};
template <typename T>
struct TypeGetter<T, void, void, std::enable_if_t<std::is_enum<T>::value>>
{
static Type* Get()
{
TypeID typeID = GetTypeID<T>();
Type* type = GetTypeSet().findTypeByID(typeID);
if (type != nullptr)
return type;
switch (sizeof(T))
{
case 1: return TypeGetter<int8_t>::Get();
case 2: return TypeGetter<int16_t>::Get();
case 4: return TypeGetter<int32_t>::Get();
case 8: return TypeGetter<int64_t>::Get();
}
return nullptr;
}
};
// Type Desc Accesors
template<typename T>
Enum* GetEnum()
{
Type* type = TypeGetter<T>::Get();
if (type->getTypeInfo() != TypeInfo_Enum)
return nullptr;
return static_cast<Enum*>(type);
}
template<typename T>
Enum* GetEnum(T&)
{
return GetEnum<T>();
}
template <typename T>
Type* GetType()
{
return TypeGetter<T>::Get();
}
template <typename T>
Type* GetType(T&)
{
return GetType<T>();
}
template <typename T>
Class* GetClass()
{
Type* typeDesc = TypeGetter<T>::Get();
if (typeDesc != nullptr && typeDesc->getTypeInfo() == TypeInfo_Class)
{
return static_cast<Class*>(typeDesc);
}
return nullptr;
}
template <typename T>
Class* GetClass(T&)
{
return GetClass<T>();
}
// --- Cast
template <typename DestType, typename SourceType, typename IsDestLastPointer = void, typename IsSourceLastPointer = void>
struct CastClassesUnpiler
{
static_assert(std::is_pointer<DestType>::value, "Mismatching pointer count between cast source and cast destination (DestType is not a pointer).");
static_assert(std::is_pointer<SourceType>::value, "Mismatching pointer count between cast source and cast destination (SourceType is not a pointer).");
static bool Unpile(SourceType& _o, Class** _destClass, Class** _sourceClass)
{
using source_t = typename std::remove_pointer<SourceType>::type;
using dest_t = typename std::remove_pointer<DestType>::type;
return CastClassesUnpiler<dest_t, source_t>::Unpile(*_o, _destClass, _sourceClass);
}
};
template <typename DestType, typename SourceType>
struct CastClassesUnpiler<DestType, SourceType, std::enable_if_t<!std::is_pointer<DestType>::value>, std::enable_if_t<!std::is_pointer<SourceType>::value>>
{
static bool Unpile(SourceType& _o, Class** _destClass, Class** _sourceClass)
{
*_destClass = GetClass<DestType>();
*_sourceClass = _o.getClass();
return true;
}
};
template <typename DestType, typename SourceType>
DestType Cast(SourceType _o)
{
Class* destClass = nullptr;
Class* sourceClass = nullptr;
if (CastClassesUnpiler<DestType, SourceType>::Unpile(_o, &destClass, &sourceClass))
{
if (destClass != nullptr && sourceClass != nullptr)
{
if (destClass->isChildOf(sourceClass) // upcast
|| sourceClass->isChildOf(destClass)) // downcast
{
return reinterpret_cast<DestType>(_o);
}
}
}
return nullptr;
}
// --- Enum
template <typename T>
bool Enum::getValueFromString(const char* _string, T& _outValue) const
{
if (_string == nullptr)
return false;
size_t hash = HashCString(_string);
auto it = m_valuesByNameHash.find(hash);
if (it != m_valuesByNameHash.end())
{
_outValue = static_cast<T>(it->second->getValue());
return true;
}
return false;
}
template <typename T>
bool Enum::getStringFromValue(T _value, const char*& _outString) const
{
int64_t value = static_cast<int64_t>(_value);
for (auto it = m_values.begin(); it != m_values.end(); ++it)
{
EnumValue* enumValue = *it;
if (enumValue->getValue() == value)
{
_outString = enumValue->getName();
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Types Initialization
//-----------------------------------------------------------------------------
template <typename T>
struct TypeInitializer
{
TypeInitializer(TypeInfo _typeInfo, const char* _name)
{
typeDesc = new Type(_typeInfo, _name);
typeDesc->createVirtualTypeWrapper<T>();
GetTypeSet().addType(typeDesc);
}
~TypeInitializer()
{
GetTypeSet().removeType(typeDesc);
delete typeDesc;
}
Type* typeDesc = nullptr;
};
#define TYPEDESCINITIALIZER_DECLARE(_type) extern TypeInitializer<_type> g_##_type##TypeInitializer
TYPEDESCINITIALIZER_DECLARE(void);
TYPEDESCINITIALIZER_DECLARE(bool);
TYPEDESCINITIALIZER_DECLARE(char);
TYPEDESCINITIALIZER_DECLARE(int8_t);
TYPEDESCINITIALIZER_DECLARE(int16_t);
TYPEDESCINITIALIZER_DECLARE(int32_t);
TYPEDESCINITIALIZER_DECLARE(int64_t);
TYPEDESCINITIALIZER_DECLARE(uint8_t);
TYPEDESCINITIALIZER_DECLARE(uint16_t);
TYPEDESCINITIALIZER_DECLARE(uint32_t);
TYPEDESCINITIALIZER_DECLARE(uint64_t);
TYPEDESCINITIALIZER_DECLARE(float);
TYPEDESCINITIALIZER_DECLARE(double);
#undef TYPEDESCINITIALIZER_DECLARE
// === Static Function (WIP) ===
// @TODO: refactor this so that we can have a full return type + argument type list at construction time, so that we can generate a unique name
// @TODO: add a special initializer for function type desc so that we can manage its responsibility correctly
class MIRROR_API StaticFunction : public Type
{
public:
StaticFunction()
: Type(TypeInfo_StaticFunction, "StaticFunction")
{
}
template <typename T>
void setReturnType()
{
m_returnType = TypeGetter<T>::Get()->getTypeID();
}
template <typename T>
void addArgument() {
Type* typeDesc = TypeGetter<T>::Get();
m_argumentTypes.push_back(typeDesc->getTypeID());
}
private:
TypeID m_returnType = UNDEFINED_TYPEID;
std::vector<TypeID> m_argumentTypes;
};
/*
* Some templates to do reflection for functions
*/
template<typename NotAFunction>
struct FunctionTraits {}; /// #1
template<typename R, typename... Args>
struct FunctionTraits<R(*)(Args...)> {
using result = R;
using args = std::tuple<Args...>;
};
template<typename F>
using FunctionArguments_T = typename FunctionTraits<F>::args;
template <std::size_t N, typename F>
using FunctionArgument_T = typename std::tuple_element<N, FunctionArguments_T<F>>::type;
template <typename F, typename T, T I, T Top>
struct FunctionArgumentsUnpiler
{
static void Unpile(class StaticFunction* _function)
{
using ArgumentType = FunctionArgument_T<I, F>;
_function->addArgument<ArgumentType>();
FunctionArgumentsUnpiler<F, T, I + 1u, Top>::Unpile(_function);
}
};
template <typename F, typename T, T I>
struct FunctionArgumentsUnpiler<F, T, I, I>
{
static void Unpile(class StaticFunction* _function) {}
};
template<typename F>
FunctionArguments_T<F> MakeFunctionArgumentsTuple(F _function)
{
return FunctionArguments_T<F>{};
}
template<typename Function, typename Tuple, size_t ... I>
auto CallFunction(Function f, Tuple t, std::index_sequence<I ...>)
{
return f(std::get<I>(t) ...);
}
template<typename Function, typename Tuple>
auto CallFunction(Function f, Tuple t)
{
static constexpr auto size = std::tuple_size<Tuple>::value;
return CallFunction(f, t, std::make_index_sequence<size>{});
}
template <size_t I, size_t Top>
struct FunctionArgumentsFiller
{
template <typename C, typename ...ArgumentsTypes>
static void Fill(C* _classInstance, const char** _memberNames, size_t _memberCount, std::tuple<ArgumentsTypes...>& _arguments)
{
using Argument_T = typename std::tuple_element<I, std::tuple<ArgumentsTypes...>>::type;
assert(_classInstance != nullptr);
Class* c = _classInstance->GetClass();
ClassMember* member = c->findMemberByName(_memberNames[I]);
assert(member != nullptr);
assert(member->getType() == TypeGetter<Argument_T>::Get());
std::get<I>(_arguments) = *reinterpret_cast<Argument_T*>(member->getInstanceMemberPointer(_classInstance));
FunctionArgumentsFiller<I + 1u, Top>::Fill(_classInstance, _memberNames, _memberCount, _arguments);
}
};
template <size_t I>
struct FunctionArgumentsFiller<I, I>
{
template <typename C, typename ...ArgumentsTypes>
static void Fill(C* _classInstance, const char** _memberNames, size_t _memberCount, std::tuple<ArgumentsTypes...>& _arguments)
{
}
};