Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/Documentation'
Browse files Browse the repository at this point in the history
  • Loading branch information
playmer committed Mar 5, 2017
2 parents 0e61073 + 8a178ca commit 002d0cc
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 101 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
22 changes: 12 additions & 10 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 Expand Up @@ -210,7 +212,7 @@ struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std:
static std::unique_ptr<Function> BindFunction(const char *name)
{
auto function = std::make_unique<Function>(name, TypeId<Return>(), TypeId<ObjectType>(), false);
function->AddParameter(TypeId<ObjectType>());
function->AddParameter(TypeId<ObjectType*>());
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(Caller<BoundFunc>);
Expand All @@ -222,7 +224,7 @@ struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std:
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>());
function->AddParameter(TypeId<ObjectType*>());
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(aCaller);
Expand Down Expand Up @@ -253,7 +255,7 @@ struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std:
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>());
function->AddParameter(TypeId<ObjectType*>());
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(aCaller);
Expand All @@ -265,7 +267,7 @@ struct Binding<Return(ObjectType::*)(Arguments...), typename std::enable_if<std:
static std::unique_ptr<Function> BindFunction(const char *name)
{
auto function = std::make_unique<Function>(name, TypeId<Return>(), TypeId<ObjectType>(), false);
function->AddParameter(TypeId<ObjectType>());
function->AddParameter(TypeId<ObjectType*>());
ParseArguments<Arguments...>::Parse(function.get());

function->SetCaller(Caller<BoundFunc>);
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)
127 changes: 90 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;
}

void 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,12 @@ DeclareExternalType(u32)
DeclareExternalType(u64)
DeclareExternalType(float)
DeclareExternalType(double)
DeclareExternalType(std::string)

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

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


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


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

0 comments on commit 002d0cc

Please sign in to comment.