Skip to content

Commit

Permalink
Documented object implemented, corrected one more immature variable n…
Browse files Browse the repository at this point in the history
…ame, Also worked on reflecting the reflection system and making it easi
  • Loading branch information
playmer committed Mar 5, 2017
1 parent 0e61073 commit 386bade
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Reflection/Any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Any
bool IsType()
{
auto type = TypeId<T>();
auto truth = mType == type;
auto truth = mType == type; // TODO: This will probably not work across dll boundaries.
return truth;
}

Expand Down
3 changes: 3 additions & 0 deletions Reflection/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
class Field : public Property
{
public:
DeclareType(Field)
Field(Field&) = delete;

Field(const char *aName,
std::unique_ptr<Function> aGetter,
std::unique_ptr<Function> aSetter)
Expand Down
14 changes: 8 additions & 6 deletions Reflection/Function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "Any.hpp"

class Function
class Function : public DocumentedObject
{
public:
DeclareType(Function)
Function(Function&) = delete;

using CallingFunction = Any(*)(std::vector<Any>& arguments);

Expand All @@ -23,14 +25,14 @@ class Function
std::string mName;
};

Function(const char *aName, Type *aReturnType, Type *aOwningType, bool aStatic)
Function(const char *aName, Type *aReturnType, Type *aOwningType, bool aStaticOrFree)
: mName(aName),
mReturnType(aReturnType),
mCaller(nullptr),
mOwningType(aOwningType),
mStatic(aStatic)
mStaticOrFree(aStaticOrFree)
{
if ((aOwningType == nullptr) && (aStatic == false))
if ((aOwningType == nullptr) && (mStaticOrFree == false))
{
runtime_assert(false,
"A function without an owning type is, by definition, static.");
Expand Down Expand Up @@ -90,7 +92,7 @@ class Function
Type *mReturnType;
CallingFunction mCaller;
Type *mOwningType;
bool mStatic;
bool mStaticOrFree;
};

template <typename T>
Expand Down Expand Up @@ -167,7 +169,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>(), nullptr, false);
auto function = std::make_unique<Function>(name, TypeId<Return>(), nullptr, true);
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(aCaller);
Expand Down
6 changes: 5 additions & 1 deletion Reflection/Property.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#include "Function.hpp"

// TODO: Have some requirements on the types of setters and getters.
class Property
class Property : public DocumentedObject
{
public:
DeclareType(Property)

Property(Property&) = delete;

Property(const char *aName,
std::unique_ptr<Function> aGetter,
std::unique_ptr<Function> aSetter)
Expand Down
34 changes: 33 additions & 1 deletion Reflection/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@

#include "Type.hpp"

DefineType(DocumentedObject)
{
auto documentation = BindProperty<decltype(&DocumentedObject::GetDocumentation),
&DocumentedObject::GetDocumentation,
decltype(&DocumentedObject::SetDocumentation),
&DocumentedObject::SetDocumentation>("Documentation");

GetStaticType()->AddProperty(std::move(documentation));
}

DefineType(Type)
{

}

DefineType(Function)
{

}


DefineType(Property)
{

}


DefineType(Field)
{

}

DefineExternalType(void)
DefineExternalType(i8)
Expand All @@ -13,4 +44,5 @@ DefineExternalType(u16)
DefineExternalType(u32)
DefineExternalType(u64)
DefineExternalType(float)
DefineExternalType(double)
DefineExternalType(double)
DefineExternalType(std::string)
125 changes: 88 additions & 37 deletions Reflection/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,66 @@
#include "Types.hpp"
#include "Reflection.hpp"

class Type;

class Type
class Base
{
virtual Type *GetType() = 0;
};


// 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; }; \
static void InitializeType();


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



class DocumentedObject : public Base
{
public:
DeclareType(DocumentedObject);

DocumentedObject()
: mDocumentation("")
{

}


DocumentedObject(const char *aDocumentation)
: mDocumentation(aDocumentation)
{

}

std::string GetDocumentation()
{
return mDocumentation;
}

std::string SetDocumentation(const char *aString)
{
mDocumentation = aString;
}
private:
std::string mDocumentation;
};

class Type : public DocumentedObject
{
public:
DeclareType(Type)

using DefaultConstructor = void(*)(void*);
using CopyConstructor = void(*)(void*, void*);
using Destructor = void(*)(void*);
Expand All @@ -24,6 +80,8 @@ class Type
template <typename T>
explicit Type(Type *aType, bool aReference, T *aNull);

Type(Type&) = delete;

~Type();

const std::string& Name() const { return mName; }
Expand Down Expand Up @@ -102,6 +160,11 @@ class Type
return mPointerTo;
}

Type* GetBaseType()
{
return mBaseType;
}

private:
CacheOrderedSet<std::string, std::unique_ptr<Function>> mFunctions;
CacheOrderedSet<std::string, std::unique_ptr<Property>> mProperties;
Expand All @@ -116,6 +179,7 @@ class Type

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

template<typename T>
Expand All @@ -131,12 +195,16 @@ struct TypeIdentification
template<typename T>
inline Type* TypeId();


template<typename T>
inline void InitializeType();

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

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

return &type;
}
Expand All @@ -160,25 +228,25 @@ inline Type* TypeId()
return TypeIdentification<T>::TypeId();
}

#define DeclareExternalType(Name) \
namespace Types \
{ \
extern Type Name##_Type; \
} \
\
\
template<> \
struct TypeIdentification<Name> \
{ \
static inline Type* TypeId() \
{ \
return &Types::Name##_Type; \
} \
template<typename T>
inline void InitializeType()
{
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 DefineExternalType(Name) \
Type Types::Name##_Type{#Name, static_cast<Name*>(nullptr)};
#define DefineExternalType(Name)

DeclareExternalType(void)
DeclareExternalType(i8)
Expand All @@ -191,27 +259,10 @@ DeclareExternalType(u32)
DeclareExternalType(u64)
DeclareExternalType(float)
DeclareExternalType(double)

// 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(); \
Type* GetType();

#define DefineType(Name) \
Type Name::sType{#Name, static_cast<Name*>(nullptr)}; \
Type* Name::GetStaticType() { return &sType; }; \
Type* Name::GetType() { return &sType; };
DeclareExternalType(std::string)



class Base
{
virtual Type *GetType() = 0;
};


#include "Function.hpp"
#include "Property.hpp"
Expand Down
Loading

0 comments on commit 386bade

Please sign in to comment.