Skip to content

Commit

Permalink
Parent types should be detected
Browse files Browse the repository at this point in the history
  • Loading branch information
playmer committed Mar 20, 2017
1 parent aa5af4b commit 8caa014
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 46 deletions.
6 changes: 3 additions & 3 deletions Reflection/Reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ constexpr bool IsWhiteSpace(char aCharacter)
constexpr bool IsIdentifier(char aCharacter)
{
if ((('a' <= aCharacter) && (aCharacter <= 'z')) ||
(('A' <= aCharacter) && (aCharacter <= 'Z')) ||
(('0' <= aCharacter) && (aCharacter <= '9')) ||
'_' == aCharacter)
(('A' <= aCharacter) && (aCharacter <= 'Z')) ||
(('0' <= aCharacter) && (aCharacter <= '9')) ||
'_' == aCharacter)
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Reflection/Reflection.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Attribute.cpp">
<Filter>Header Files</Filter>
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
115 changes: 77 additions & 38 deletions Reflection/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,37 @@ class Base
};



template <typename T>
typename T::SelfType GetSelfType(typename T::SelfType*) {}

template <typename T>
void GetSelfType(...) {}


template <typename T>
T GetDummy(void (T::*)());



// Used to declare a static type within a class
// Requires DefineType be used at some point in a
// translation unit.
#define DeclareType(Name) \
static Type sType; \
static Type* GetStaticType() { return &sType; }; \
Type* GetType() { return &sType; }; \
#define DeclareType(Name) \
void Dummy() {} \
typedef decltype(GetDummy(&Dummy)) TempSelfType; \
typedef decltype(GetSelfType<TempSelfType>(nullptr)) BaseType; \
typedef TempSelfType SelfType; \
static Type sType; \
static Type* GetStaticType() { return &sType; }; \
Type* GetType() { return &sType; }; \
static void InitializeType();


#define DefineType(Name) \
Type Name::sType{#Name, static_cast<Name*>(nullptr)}; \
#define DefineType(Name) \
Type Name::sType{#Name, \
static_cast<Name*>(nullptr), \
static_cast<Name::BaseType*>(nullptr)}; \
void Name::InitializeType()


Expand Down Expand Up @@ -77,13 +96,16 @@ class Type : public DocumentedObject
Pointer,
Const
};

template <typename T>
explicit Type(const char *aName, T *aNull);

template <typename tType>
explicit Type(const char *aName, tType *aNull);

template <typename tDerived, typename tBase>
explicit Type(const char *aName, tDerived *aDerivedNull, tBase *aBaseNull);

template <typename T>
explicit Type(T *aNull);

template <typename tDerived, typename tBase>
explicit Type(tDerived *aDerivedNull, tBase *aBaseNull);

template <typename T>
explicit Type(Type *aType, Modifier aModifier, T *aNull);
Expand Down Expand Up @@ -258,15 +280,16 @@ inline Type* TypeId()
// return TypeInitialization<T>::InitializeType();
//}

#define DeclareExternalType(Name) \
template<> \
struct TypeIdentification<Name> \
{ \
static inline Type* TypeId() \
{ \
static Type type{ #Name, static_cast<Name*>(nullptr) }; \
return &type; \
} \
#define DeclareExternalType(Name) \
template<> \
struct TypeIdentification<Name> \
{ \
static inline Type* TypeId() \
{ \
static Type type{ #Name, \
static_cast<Name*>(nullptr) }; \
return &type; \
} \
};


Expand All @@ -291,36 +314,52 @@ DeclareExternalType(std::string)
#include "Field.hpp"


template <typename T>
inline Type::Type(const char *aName, T *)
template <typename tDerived, typename tBase>
inline Type::Type(const char *aName, tDerived *, tBase *)
: mName(aName),
mHash(std::hash<std::string>{}(mName)),
mAllocatedSize(SizeOf<T>()),
mStoredSize(SizeOf<T>()),
mDefaultConstructor(GenericDefaultConstruct<T>),
mCopyConstructor(GenericCopyConstruct<T>),
mDestructor(GenericDestruct<T>),
mAllocatedSize(SizeOf<tDerived>()),
mStoredSize(SizeOf<tDerived>()),
mDefaultConstructor(GenericDefaultConstruct<tDerived>),
mCopyConstructor(GenericCopyConstruct<tDerived>),
mDestructor(GenericDestruct<tDerived>),
mReferenceTo(nullptr),
mPointerTo(nullptr),
mConstOf(nullptr),
mBaseType(nullptr)
mBaseType(TypeId<tBase>())
{
}

template <typename tType>
inline Type::Type(const char *aName, tType *)
: mName(aName),
mHash(std::hash<std::string>{}(mName)),
mAllocatedSize(SizeOf<tType>()),
mStoredSize(SizeOf<tType>()),
mDefaultConstructor(GenericDefaultConstruct<tType>),
mCopyConstructor(GenericCopyConstruct<tType>),
mDestructor(GenericDestruct<tType>),
mReferenceTo(nullptr),
mPointerTo(nullptr),
mConstOf(nullptr),
mBaseType(nullptr)
{
}

template <typename T>
inline Type::Type(T *)
: mName(GetTypeName<T>().data()),

template <typename tDerived, typename tBase>
inline Type::Type(tDerived *, tBase *)
: mName(GetTypeName<tDerived>().data()),
mHash(std::hash<std::string>{}(mName)),
mAllocatedSize(SizeOf<T>()),
mStoredSize(SizeOf<T>()),
mDefaultConstructor(GenericDefaultConstruct<T>),
mCopyConstructor(GenericCopyConstruct<T>),
mDestructor(GenericDestruct<T>),
mAllocatedSize(SizeOf<tDerived>()),
mStoredSize(SizeOf<tDerived>()),
mDefaultConstructor(GenericDefaultConstruct<tDerived>),
mCopyConstructor(GenericCopyConstruct<tDerived>),
mDestructor(GenericDestruct<tDerived>),
mReferenceTo(nullptr),
mPointerTo(nullptr),
mConstOf(nullptr),
mBaseType(nullptr)
mConstOf(nullptr),
mBaseType(TypeId<tBase>())
{
}

Expand Down
4 changes: 2 additions & 2 deletions Reflection/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ inline void runtime_assert(bool aValue, const char *aMessage = "")
{
printf("ASSERT: %s\n", aMessage);

// Purposely ruining this programs life.
// Intentionally crashing the program.
int *base = nullptr;
*base = 1;
}
}

// The fact that I actually have to do this makes me sick.
// We want to be able to use the string literals, this is the only way.
using namespace std::string_literals;

template<typename T>
Expand Down
3 changes: 1 addition & 2 deletions Reflection/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace Test
int y = 1;
int z = 2;
int w = 3;

};

DefineType(Animal)
Expand Down Expand Up @@ -61,7 +60,7 @@ namespace Test
}
}

class Cat
class Cat : public Test::Test2::Animal
{
public:
DeclareType(Cat)
Expand Down

0 comments on commit 8caa014

Please sign in to comment.