Skip to content

Commit

Permalink
We can get proper types for pointers and references now!
Browse files Browse the repository at this point in the history
  • Loading branch information
playmer committed Mar 3, 2017
1 parent 14f2498 commit 0e61073
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 93 deletions.
70 changes: 38 additions & 32 deletions Reflection/Any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,9 @@ class Any
{
public:


template <typename... Rest> struct ParseArguments;

template <>
struct ParseArguments<>
{
inline static void Parse(std::vector<Any> &aArguments)
{
}
};

template <typename First, typename... Rest>
struct ParseArguments<First, Rest...>
{
inline static void Parse(std::vector<Any> &aArguments, First aFirst, Rest ...aRest)
{
aArguments.emplace_back(aFirst);
ParseArguments<Rest...>::Parse(aArguments, aRest...);
}
};

template <typename ...Arguments>
static std::vector<Any> FromVariadic(Arguments...aArguments)
{
std::vector<Any> arguments;
ParseArguments<Arguments...>::Parse(arguments, aArguments...);

return arguments;
}

static std::vector<Any> FromVariadic(Arguments...aArguments);

Any()
{
mType = nullptr;
Expand All @@ -62,7 +35,7 @@ class Any
template <typename T>
explicit Any(const T& value)
{
mType = TypeId<remove_all_pointers_t<T>>();
mType = TypeId<T>();
byte* data = AllocateData(sizeof(T));
new (data) T(value);
}
Expand Down Expand Up @@ -116,7 +89,7 @@ class Any
template <typename T>
bool IsType()
{
auto type = TypeId<remove_all_pointers_t<T>>();
auto type = TypeId<T>();
auto truth = mType == type;
return truth;
}
Expand All @@ -131,4 +104,37 @@ class Any

Type* mType;
byte mData[16];
};
};





template <typename... Rest> struct ParseAndAddArguments;

template <>
struct ParseAndAddArguments<>
{
inline static void Parse(std::vector<Any> &aArguments)
{
}
};

template <typename First, typename... Rest>
struct ParseAndAddArguments<First, Rest...>
{
inline static void Parse(std::vector<Any> &aArguments, First aFirst, Rest ...aRest)
{
aArguments.emplace_back(aFirst);
ParseAndAddArguments<Rest...>::Parse(aArguments, aRest...);
}
};

template <typename ...Arguments>
std::vector<Any> Any::FromVariadic(Arguments...aArguments)
{
std::vector<Any> arguments;
ParseAndAddArguments<Arguments...>::Parse(arguments, aArguments...);

return arguments;
}
2 changes: 1 addition & 1 deletion Reflection/CacheOrderedSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class CacheOrderedSet
///////////////////////////////////////
// Protected Fields
///////////////////////////////////////
typename ContainerType mData;
ContainerType mData;
};

#endif
7 changes: 3 additions & 4 deletions Reflection/Function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct Binding<Return(*)(Arguments...), typename std::enable_if<std::is_void<Ret
template <FunctionSignature BoundFunc>
static std::unique_ptr<Function> BindFunction(const char *name)
{
auto function = std::make_unique<Function>(name, TypeId<Return>(), TypeId<ObjectType>(), true);
auto function = std::make_unique<Function>(name, TypeId<Return>(), nullptr, true);
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(Caller<BoundFunc>);
Expand All @@ -167,8 +167,7 @@ struct Binding<Return(*)(Arguments...), typename std::enable_if<std::is_void<Ret
template <FunctionSignature BoundFunc>
static std::unique_ptr<Function> BindFunction(const char *name, CallingType aCaller)
{
auto function = std::make_unique<Function>(name, TypeId<Return>(), TypeId<ObjectType>(), false);
function->AddParameter(TypeId<ObjectType>());
auto function = std::make_unique<Function>(name, TypeId<Return>(), nullptr, false);
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(aCaller);
Expand All @@ -179,7 +178,7 @@ struct Binding<Return(*)(Arguments...), typename std::enable_if<std::is_void<Ret
template <FunctionSignature BoundFunc>
static std::unique_ptr<Function> BindFunction(const char *name)
{
auto function = std::make_unique<Function>(name, TypeId<Return>(), TypeId<ObjectType>(), true);
auto function = std::make_unique<Function>(name, TypeId<Return>(), nullptr, true);
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(Caller<BoundFunc>);
Expand Down
190 changes: 134 additions & 56 deletions Reflection/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ class Type
using Destructor = void(*)(void*);

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


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

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

~Type();

Expand Down Expand Up @@ -89,6 +92,16 @@ class Type
return mFields.FindFirst(name)->second.get();
}

Type* GetPointerTo()
{
return mPointerTo;
}

Type* GetReferenceTo()
{
return mPointerTo;
}

private:
CacheOrderedSet<std::string, std::unique_ptr<Function>> mFunctions;
CacheOrderedSet<std::string, std::unique_ptr<Property>> mProperties;
Expand All @@ -100,75 +113,68 @@ class Type
DefaultConstructor mDefaultConstructor;
CopyConstructor mCopyConstructor;
Destructor mDestructor;
};

#include "Function.hpp"
#include "Property.hpp"
#include "Field.hpp"

Type *mPointerTo;
Type *mReferenceTo;
};

template <typename T>
inline Type::Type(const char *aName, T *)
: mName(aName),
mHash(std::hash<std::string>{}(mName)),
mAllocatedSize(SizeOf<T>()),
mStoredSize(SizeOf<T>()),
mDefaultConstructor(GenericDefaultConstruct<T>),
mCopyConstructor(GenericCopyConstruct<T>),
mDestructor(GenericDestruct<T>)
template<typename T>
struct TypeIdentification
{
}
static inline Type* TypeId()
{
return T::GetStaticType();
}
};


template <typename T>
inline Type::Type(T *)
: 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>)
{
}
template<typename T>
inline Type* TypeId();

inline Type::~Type()
template<typename T>
struct TypeIdentification<T*>
{
static inline Type* TypeId()
{
static Type type = Type{ ::TypeId<T>(), false, static_cast<T*>(nullptr) };

return &type;
}
};

}

inline void Type::AddFunction(std::unique_ptr<Function> aFunction)
template<typename T>
struct TypeIdentification<T&>
{
mFunctions.Emplace(aFunction->GetName(), std::move(aFunction));
}
static inline Type* TypeId()
{
static Type type = Type{ ::TypeId<T>(), true, static_cast<T*>(nullptr) };

inline void Type::AddProperty(std::unique_ptr<Property> aProperty)
{
mProperties.Emplace(aProperty->GetName(), std::move(aProperty));
}
return &type;
}
};

inline void Type::AddField(std::unique_ptr<Field> aField)
{
mFields.Emplace(aField->GetName(), std::move(aField));
}

template<typename T>
Type* TypeId()
inline Type* TypeId()
{
return T::GetStaticType();
return TypeIdentification<T>::TypeId();
}

#define DeclareExternalType(Name) \
namespace Types \
{ \
extern Type Name##_Type; \
} \
\
template <> \
inline Type* TypeId<Name>() \
{ \
return &Types::Name##_Type; \
}
#define DeclareExternalType(Name) \
namespace Types \
{ \
extern Type Name##_Type; \
} \
\
\
template<> \
struct TypeIdentification<Name> \
{ \
static inline Type* TypeId() \
{ \
return &Types::Name##_Type; \
} \
};


#define DefineExternalType(Name) \
Expand Down Expand Up @@ -204,4 +210,76 @@ Type* Name::GetType() { return &sType; };
class Base
{
virtual Type *GetType() = 0;
};
};


#include "Function.hpp"
#include "Property.hpp"
#include "Field.hpp"


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


template <typename T>
inline Type::Type(T *)
: 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>)
{
}


template <typename T>
inline Type::Type(Type *aType, bool aReference, 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>)
{
if (aReference)
{
mReferenceTo = aType;
}
else
{
mPointerTo = aType;
}
}

inline Type::~Type()
{

}

inline void Type::AddFunction(std::unique_ptr<Function> aFunction)
{
mFunctions.Emplace(aFunction->GetName(), std::move(aFunction));
}

inline void Type::AddProperty(std::unique_ptr<Property> aProperty)
{
mProperties.Emplace(aProperty->GetName(), std::move(aProperty));
}

inline void Type::AddField(std::unique_ptr<Field> aField)
{
mFields.Emplace(aField->GetName(), std::move(aField));
}
3 changes: 3 additions & 0 deletions Reflection/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ int main()
{
Type test = Type(static_cast<Test::Test2::Animal*>(nullptr));


auto check = TypeId<Test::Test2::Animal****&>();

auto test3 = &Test::Test2::Animal::x;

auto i8test = TypeId<i8>();
Expand Down

0 comments on commit 0e61073

Please sign in to comment.