Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/ConstexprWork'
Browse files Browse the repository at this point in the history
  • Loading branch information
playmer committed Mar 7, 2017
2 parents 002d0cc + 9d07cc3 commit 94f3033
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 47 deletions.
76 changes: 74 additions & 2 deletions Reflection/ConstexprString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ constexpr size_t GetNumberOfTokens(const char *aString)
template <size_t tConstSize>
struct ConstexprToken
{
constexpr ConstexprToken()
: mData{ '\0' }
{
}

constexpr ConstexprToken(const char *aBegin)
: mData{ '\0' }
{
Expand Down Expand Up @@ -55,11 +60,78 @@ struct ConstexprToken
constexpr const char* Data() const { return mData; };
constexpr const char* data() const { return mData; };

private:
protected:
char mData[tConstSize + 1];
};


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

}

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;
}

return false;
}

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

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



template<size_t tConstSize>
struct ConstexprTokenWriter : public ConstexprToken<tConstSize>
{
constexpr ConstexprTokenWriter()
: mWritingPosition(mData)
{
}


constexpr void Write(StringRange aRange)
{
while (aRange.mBegin < aRange.mEnd)
{
*mWritingPosition++ = *aRange.mBegin++;
}
}


private:
char *mWritingPosition;
};

constexpr size_t GetLastInstanceOfCharacter(const char *aString, size_t aSize, char aCharacter)
{
size_t toReturn = aSize + 1;
Expand Down Expand Up @@ -95,4 +167,4 @@ constexpr size_t GetFirstInstanceOfCharacter(const char *aString, size_t aSize,
}

return toReturn;
}
}
98 changes: 84 additions & 14 deletions Reflection/Reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,100 @@ constexpr auto GetFunctionSignature()
return test;
}

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

return false;
}


constexpr bool IsIdentifier(char aCharacter)
{
if ((('a' <= aCharacter) && (aCharacter <= 'z')) ||
(('A' <= aCharacter) && (aCharacter <= 'Z')) ||
(('0' <= aCharacter) && (aCharacter <= '9')) ||
'_' == aCharacter)
{
return true;
}

return false;
}

constexpr StringRange GetToken(StringRange aRange)
{
auto i = aRange.mBegin;

while (!IsWhiteSpace(*i) && IsIdentifier(*i) && i < aRange.mEnd)
{
++i;
}

// Gotta check if it's actually not an identifier and continue moving.
if (i == aRange.mBegin)
{
while (!IsWhiteSpace(*i) && !IsIdentifier(*i) && i < aRange.mEnd)
{
++i;
}
}

// And finally simply check for whitespace.
if (i == aRange.mBegin)
{
while (IsWhiteSpace(*i) && i < aRange.mEnd)
{
++i;
}
}

aRange.mEnd = i;
return aRange;
}



template <typename T>
constexpr auto GetTypeName()
{
constexpr const char* typeName = CONSTEXPR_FUNCTION_SIGNATURE;

constexpr size_t totalLength = StringLength(typeName);
constexpr const char* functionName = CONSTEXPR_FUNCTION_SIGNATURE;

// TODO: Should also work for GCC.
#if defined(__clang__)
constexpr size_t lastSpace = GetLastInstanceOfCharacter(typeName, StringLength(typeName), '=');
constexpr size_t typeNameLength = totalLength - lastSpace - GetTypeEnd() - 2;
size_t lastSpace = GetLastInstanceOfCharacter(typeName, StringLength(typeName), '=');
size_t typeNameLength = totalLength - lastSpace - GetTypeEnd() - 2;

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 firstArrow = GetFirstInstanceOfCharacter(functionName, StringLength(functionName), '<') + 1;
constexpr size_t lastArrow = GetLastInstanceOfCharacter(functionName, StringLength(functionName), '>');

// TODO: Remove struct and class keywords from templated output.
ConstexprToken<typeNameLength> token{ typeName + firstArrow + endOfKeyword };
#endif
constexpr size_t typenameTotalRangeSize = lastArrow - firstArrow;

ConstexprTokenWriter<typenameTotalRangeSize> finalName;

return token;
StringRange totalType{ functionName + firstArrow, functionName + lastArrow };

while (totalType.mBegin < totalType.mEnd)
{
auto token = GetToken(totalType);

if (token == "struct" || token == "class")
{
++token.mEnd;
}
else
{
finalName.Write(token);
}

totalType.mBegin = token.mEnd;
}
#endif

//ConstexprToken<totalLength> token2{ typeName};
//return token2;
return finalName;
}
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)
77 changes: 59 additions & 18 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<const T*>(nullptr) };

return &type;
}
Expand Down Expand Up @@ -261,10 +282,6 @@ DeclareExternalType(float)
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)



#include "Function.hpp"
#include "Property.hpp"
Expand All @@ -279,7 +296,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 +313,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
Loading

0 comments on commit 94f3033

Please sign in to comment.