Skip to content

Commit

Permalink
Working on fixing the Type name from constexpr.
Browse files Browse the repository at this point in the history
  • Loading branch information
playmer committed Mar 6, 2017
1 parent 002d0cc commit eb3b188
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 31 deletions.
41 changes: 41 additions & 0 deletions Reflection/ConstexprString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,47 @@ struct ConstexprToken
char mData[tConstSize + 1];
};

struct StringRange
{
constexpr StringRange(const char *aBegin, const char *aEnd)
: mBegin(aBegin),
mEnd(aBegin)
{

}

constexpr StringRange(const char *aBegin)
: mBegin(aBegin),
mEnd(aBegin + StringLength(aBegin))
{

}

bool operator==(const StringRange &aRight) const
{
if (Size() == aRight.Size())
{
for (size_t i = 0; i < Size(); ++i)
{
if (mBegin[i] != aRight.mBegin[i])
{
return false;
}
}
}

return true;
}

constexpr size_t Size() const
{
return mEnd - mBegin;
}

const char *mBegin;
const char *mEnd;
};


constexpr size_t GetLastInstanceOfCharacter(const char *aString, size_t aSize, char aCharacter)
{
Expand Down
46 changes: 43 additions & 3 deletions Reflection/Reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ constexpr auto GetFunctionSignature()
return test;
}

constexpr bool IsWhiteSpace(char aCharacter)
{
if ((9 >= aCharacter) && (aCharacter <= 13) || ' ' == aCharacter)
{
return true;
}

return false;
}

constexpr StringRange GetTypeNameRange(const char *aStart, size_t aTotalSize)
{
// Find first token of the Type name.
auto it = aStart;

while (IsWhiteSpace(*it))
{
++it;
}

constexpr const char *begin = it;
constexpr const char *firstKeywordStart = it;

while((false == IsWhiteSpace(*it)) && (it < (aStart + aTotalSize)))
{
++it;
}

constexpr StringRange firstKeyword{ firstKeywordStart, it };
constexpr StringRange structName{ "struct" };
constexpr StringRange className{ "class" };

if ((firstKeyword == structName) || (firstKeyword == className))
{

}
}

template <typename T>
constexpr auto GetTypeName()
{
Expand All @@ -63,11 +101,13 @@ constexpr auto GetTypeName()
ConstexprToken<typeNameLength> token{ typeName + lastSpace + 2 };
#elif defined(_MSC_VER)
constexpr size_t firstArrow = GetFirstInstanceOfCharacter(typeName, StringLength(typeName), '<');
constexpr size_t endOfKeyword = typeName[firstArrow + 1] == 's' ? 8 : 7;
constexpr size_t typeNameLength = totalLength - firstArrow - endOfKeyword - GetTypeEnd();
constexpr size_t lastArrow = GetLastInstanceOfCharacter(typeName, StringLength(typeName), '>');

constexpr size_t typenameTotalRangeSize = lastArrow - firstArrow;
constexpr auto typenameRange = GetTypeNameRange(typeName, typenameTotalRangeSize);

// TODO: Remove struct and class keywords from templated output.
ConstexprToken<typeNameLength> token{ typeName + firstArrow + endOfKeyword };
ConstexprToken<typenameRange.Size()> token{ typenameRange.mBegin};
#endif

return token;
Expand Down
24 changes: 12 additions & 12 deletions Reflection/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ DefineType(Field)

}

DefineExternalType(void)
DefineExternalType(i8)
DefineExternalType(i16)
DefineExternalType(i32)
DefineExternalType(i64)
DefineExternalType(u8)
DefineExternalType(u16)
DefineExternalType(u32)
DefineExternalType(u64)
DefineExternalType(float)
DefineExternalType(double)
DefineExternalType(std::string)
//DefineExternalType(void)
//DefineExternalType(i8)
//DefineExternalType(i16)
//DefineExternalType(i32)
//DefineExternalType(i64)
//DefineExternalType(u8)
//DefineExternalType(u16)
//DefineExternalType(u32)
//DefineExternalType(u64)
//DefineExternalType(float)
//DefineExternalType(double)
//DefineExternalType(std::string)
76 changes: 61 additions & 15 deletions Reflection/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class Type : public DocumentedObject
using DefaultConstructor = void(*)(void*);
using CopyConstructor = void(*)(void*, void*);
using Destructor = void(*)(void*);

enum class Modifier
{
Normal,
Reference,
Pointer,
Const
};

template <typename T>
explicit Type(const char *aName, T *aNull);
Expand All @@ -78,7 +86,7 @@ class Type : public DocumentedObject
explicit Type(T *aNull);

template <typename T>
explicit Type(Type *aType, bool aReference, T *aNull);
explicit Type(Type *aType, Modifier aModifier, T *aNull);

Type(Type&) = delete;

Expand Down Expand Up @@ -177,8 +185,9 @@ class Type : public DocumentedObject
CopyConstructor mCopyConstructor;
Destructor mDestructor;

Type *mPointerTo;
Type *mReferenceTo;
Type *mPointerTo;
Type *mConstOf;
Type *mBaseType;
};

Expand All @@ -204,7 +213,7 @@ struct TypeIdentification<T*>
{
static inline Type* TypeId()
{
static Type type{ ::TypeId<T>(), false, static_cast<T*>(nullptr) };
static Type type{ ::TypeId<T>(), Type::Modifier::Pointer, static_cast<T*>(nullptr) };

return &type;
}
Expand All @@ -215,7 +224,19 @@ struct TypeIdentification<T&>
{
static inline Type* TypeId()
{
static Type type{ ::TypeId<T>(), true, static_cast<T*>(nullptr) };
static Type type{ ::TypeId<T>(), Type::Modifier::Reference, static_cast<T*>(nullptr) };

return &type;
}
};


template<typename T>
struct TypeIdentification<const T>
{
static inline Type* TypeId()
{
static Type type{ ::TypeId<T>(), Type::Modifier::Const, static_cast<T*>(nullptr) };

return &type;
}
Expand Down Expand Up @@ -262,7 +283,8 @@ DeclareExternalType(double)
DeclareExternalType(std::string)

// TODO: Probably shouldn't need this. Look into const stuff/why a const i8 doesn't work.
DeclareExternalType(const char)
//DeclareExternalType(char)
//DeclareExternalType(const char)



Expand All @@ -279,7 +301,11 @@ inline Type::Type(const char *aName, T *)
mStoredSize(SizeOf<T>()),
mDefaultConstructor(GenericDefaultConstruct<T>),
mCopyConstructor(GenericCopyConstruct<T>),
mDestructor(GenericDestruct<T>)
mDestructor(GenericDestruct<T>),
mReferenceTo(nullptr),
mPointerTo(nullptr),
mConstOf(nullptr),
mBaseType(nullptr)
{
}

Expand All @@ -292,28 +318,48 @@ inline Type::Type(T *)
mStoredSize(SizeOf<T>()),
mDefaultConstructor(GenericDefaultConstruct<T>),
mCopyConstructor(GenericCopyConstruct<T>),
mDestructor(GenericDestruct<T>)
mDestructor(GenericDestruct<T>),
mReferenceTo(nullptr),
mPointerTo(nullptr),
mConstOf(nullptr),
mBaseType(nullptr)
{
}




template <typename T>
inline Type::Type(Type *aType, bool aReference, T *aNull)
inline Type::Type(Type *aType, Modifier aModifier, T *aNull)
: mName(GetTypeName<T>().data()),
mHash(std::hash<std::string>{}(mName)),
mAllocatedSize(SizeOf<T>()),
mStoredSize(SizeOf<T>()),
mDefaultConstructor(GenericDefaultConstruct<T>),
mCopyConstructor(GenericCopyConstruct<T>),
mDestructor(GenericDestruct<T>)
mDestructor(GenericDestruct<T>),
mReferenceTo(nullptr),
mPointerTo(nullptr),
mConstOf(nullptr),
mBaseType(nullptr)
{
if (aReference)
{
mReferenceTo = aType;
}
else
switch (aModifier)
{
mPointerTo = aType;
case Modifier::Const:
{
mConstOf = aType;
break;
}
case Modifier::Reference:
{
mReferenceTo = aType;
break;
}
case Modifier::Pointer:
{
mPointerTo = aType;
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Reflection/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

using byte = std::uint8_t;

using i8 = std::int8_t;
using i8 = char;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
Expand Down
8 changes: 8 additions & 0 deletions Reflection/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ int main()
Test::Test2::Animal::InitializeType();
Cat::InitializeType();

char test = -2;

auto char_Type = TypeId<char>();
auto u8_Type = TypeId<u8>();
//auto char_Type = TypeId<char>;
auto i8_Type = TypeId<i8>();
auto const_char = TypeId<const char>();


auto animalType = Test::Test2::Animal::GetStaticType();
auto catType = Cat::GetStaticType();
Expand Down

0 comments on commit eb3b188

Please sign in to comment.