Skip to content

Commit

Permalink
fields work
Browse files Browse the repository at this point in the history
  • Loading branch information
playmer committed Mar 1, 2017
1 parent d46fba5 commit 274035a
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 83 deletions.
15 changes: 0 additions & 15 deletions Reflection/Any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@
#include "Type.hpp"



template<typename T>
struct remove_all_pointers : std::conditional_t<
std::is_pointer_v<T>,
remove_all_pointers<
std::remove_pointer_t<T>
>,
std::identity<T>
>
{};

template<typename T>
using remove_all_pointers_t = typename remove_all_pointers<T>::type;


class Any
{
public:
Expand Down
31 changes: 30 additions & 1 deletion Reflection/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,38 @@ class Field : public Property
std::unique_ptr<Function> aSetter)
: Property(aName, std::move(aGetter), std::move(aSetter))
{
runtime_assert((nullptr != aGetter) && (nullptr != aSetter),
runtime_assert((nullptr != mGetter) && (nullptr != mSetter),
"Both the getter and setter must be set.");
}

template<typename FieldPointerType, FieldPointerType aFieldPointer>
static Any Getter(std::vector<Any>& aArguments)
{
auto self = aArguments.at(0).As<typename DecomposeFieldPointer<FieldPointerType>::ObjectType*>();
return Any(self->*aFieldPointer);
}

template<typename FieldPointerType, FieldPointerType aFieldPointer>
static Any Setter(std::vector<Any>& aArguments)
{
auto self = aArguments.at(0).As<typename DecomposeFieldPointer<FieldPointerType>::ObjectType*>();
self->*aFieldPointer = aArguments.at(1).As<typename DecomposeFieldPointer<FieldPointerType>::FieldType>();
return Any();
}

private:
};




template<typename FieldPointerType, FieldPointerType aFieldPointer>
static std::unique_ptr<Field> BindField(const char *aName)
{
using ObjectType = typename DecomposeFieldPointer<FieldPointerType>::ObjectType;
using FieldType = typename DecomposeFieldPointer<FieldPointerType>::FieldType;
auto getter = Binding<FieldType (ObjectType::*)()>::BindFunction("Getter", Field::Getter<FieldPointerType, aFieldPointer>);
auto setter = Binding<void(ObjectType::*)(FieldType)>::BindFunction("Setter", Field::Setter<FieldPointerType, aFieldPointer>);
return std::make_unique <Field> (aName, std::move(getter), std::move(setter));
}

42 changes: 42 additions & 0 deletions Reflection/Function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ template <typename Return, typename... Arguments>
struct Binding<Return(*)(Arguments...), typename std::enable_if<std::is_void<Return>::value == false>::type >
{
using FunctionSignature = Return(*)(Arguments...);
using CallingType = Any(*)(std::vector<Any>&);

template <FunctionSignature BoundFunc>
static Any Caller(std::vector<Any>& arguments)
Expand All @@ -153,6 +154,7 @@ template <typename Return, typename... Arguments>
struct Binding<Return(*)(Arguments...), typename std::enable_if<std::is_void<Return>::value>::type >
{
using FunctionSignature = Return(*)(Arguments...);
using CallingType = Any(*)(std::vector<Any>&);

template <FunctionSignature BoundFunc>
static Any Caller(std::vector<Any>& arguments)
Expand All @@ -162,6 +164,18 @@ struct Binding<Return(*)(Arguments...), typename std::enable_if<std::is_void<Ret
return Any();
}

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>());
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(aCaller);

return std::move(function);
}

template <FunctionSignature BoundFunc>
static std::unique_ptr<Function> BindFunction(const char *name)
{
Expand All @@ -178,6 +192,7 @@ template <typename Return, typename ObjectType, typename... Arguments>
struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std::is_void<Return>::value == false>::type>
{
using FunctionSignature = Return(ObjectType::*)(Arguments...);
using CallingType = Any(*)(std::vector<Any>&);

template <FunctionSignature BoundFunc>
static Any Caller(std::vector<Any>& arguments)
Expand All @@ -190,6 +205,8 @@ struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std:
return toReturn;
}



template <FunctionSignature BoundFunc>
static std::unique_ptr<Function> BindFunction(const char *name)
{
Expand All @@ -201,13 +218,26 @@ struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std:

return std::move(function);
}


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>());
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(aCaller);

return std::move(function);
}
};


template <typename Return, typename ObjectType, typename... Arguments>
struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std::is_void<Return>::value>::type>
{
using FunctionSignature = Return(ObjectType::*)(Arguments...);
using CallingType = Any(*)(std::vector<Any>&);

template <FunctionSignature BoundFunc>
static Any Caller(std::vector<Any>& arguments)
Expand All @@ -220,6 +250,18 @@ struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std:
return Any();
}


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>());
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(aCaller);

return std::move(function);
}

template <FunctionSignature BoundFunc>
static std::unique_ptr<Function> BindFunction(const char *name)
{
Expand Down
83 changes: 22 additions & 61 deletions Reflection/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,6 @@
#include "Types.hpp"
#include "Reflection.hpp"

template <typename T>
inline void GenericDestruct(void* aMemory)
{
(static_cast<T*>(aMemory))->~T();
}

template <typename T>
inline void GenericDefaultConstruct(void* aMemory)
{
new (aMemory) T();
}

template <typename T>
inline void GenericCopyConstruct(void* aObject, void* aMemory)
{
new (aMemory) T(*static_cast<T*>(aObject));
}


template <>
inline void GenericDestruct<void>(void* aMemory)
{
}

template <>
inline void GenericDefaultConstruct<void>(void* aMemory)
{
}

template <>
inline void GenericCopyConstruct<void>(void* aObject, void* aMemory)
{
}

class Type
{
Expand Down Expand Up @@ -77,6 +44,7 @@ class Type

void AddFunction(std::unique_ptr<Function> aFunction);
void AddProperty(std::unique_ptr<Property> aProperty);
void AddField(std::unique_ptr<Field> aField);

CacheOrderedSet<std::string, std::unique_ptr<Function>>::range GetFunctionRange(const char *aName)
{
Expand Down Expand Up @@ -106,9 +74,25 @@ class Type
return mProperties.FindFirst(name)->second.get();
}


CacheOrderedSet<std::string, std::unique_ptr<Property>>::range GetFieldRange(const char *aName)
{
std::string name{ aName };

return mFields.FindAll(name);
}

Property* GetFirstField(const char *aName)
{
std::string name{ aName };

return mFields.FindFirst(name)->second.get();
}

private:
CacheOrderedSet<std::string, std::unique_ptr<Function>> mFunctions;
CacheOrderedSet<std::string, std::unique_ptr<Property>> mProperties;
CacheOrderedSet<std::string, std::unique_ptr<Property>> mFields;
std::string mName;
size_t mHash;
size_t mAllocatedSize;
Expand All @@ -122,34 +106,6 @@ class Type
#include "Property.hpp"
#include "Field.hpp"

//template<>
//inline Type::Type<void>(const char *aName, void *)
// : mName(aName),
// mHash(std::hash<std::string>{}(mName)),
// mAllocatedSize(0),
// mStoredSize(0),
// mDefaultConstructor(nullptr),
// mCopyConstructor(nullptr),
// mDestructor(nullptr)
//{
//
//}



template<typename T>
inline auto SizeOf()
{
return sizeof(T);
}

template<>
inline auto SizeOf<void>()
{
return 0;
}



template <typename T>
inline Type::Type(const char *aName, T *)
Expand Down Expand Up @@ -191,6 +147,11 @@ 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));
}

template<typename T>
Type* TypeId()
{
Expand Down
73 changes: 71 additions & 2 deletions Reflection/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,75 @@ class Field;
// The fact that I actually have to do this makes me sick.
using namespace std::string_literals;

// Helper to call the constructor of a type.
inline void GenericDoNothing(byte *aMemory)
template<typename T>
inline size_t SizeOf()
{
return sizeof(T);
}

template<>
inline size_t SizeOf<void>()
{
return 0;
}


template <typename T>
inline void GenericDestruct(void* aMemory)
{
(static_cast<T*>(aMemory))->~T();
}

template <typename T>
inline void GenericDefaultConstruct(void* aMemory)
{
new (aMemory) T();
}

template <typename T>
inline void GenericCopyConstruct(void* aObject, void* aMemory)
{
new (aMemory) T(*static_cast<T*>(aObject));
}


template <>
inline void GenericDestruct<void>(void* aMemory)
{
}

template <>
inline void GenericDefaultConstruct<void>(void* aMemory)
{
}

template <>
inline void GenericCopyConstruct<void>(void* aObject, void* aMemory)
{
}


template<typename T>
struct remove_all_pointers : std::conditional_t<std::is_pointer_v<T>,
remove_all_pointers<std::remove_pointer_t<T>>,
std::identity<T>>
{
};

template<typename T>
using remove_all_pointers_t = typename remove_all_pointers<T>::type;



template <typename Return, typename Arg = Return>
struct DecomposeFieldPointer {};

template <typename Object, typename Field>
struct DecomposeFieldPointer<Field Object::*>
{
using ObjectType = Object;
using FieldType = Field;
};


template <typename Return, typename Arg = Return>
Expand Down Expand Up @@ -70,6 +133,12 @@ inline void runtime_assert(bool aValue, const char *aMessage = "")
}
}

// Helper to call the constructor of a type.
inline void GenericDoNothing(byte *aMemory)
{
}


enum class StringComparison
{
String1Null, // (We check this first)
Expand Down
Loading

0 comments on commit 274035a

Please sign in to comment.