Skip to content

Commit

Permalink
Merge pull request #10 from GuusKemperman/removed-explicit-params
Browse files Browse the repository at this point in the history
Removed explicit params
  • Loading branch information
GuusKemperman authored Aug 14, 2024
2 parents 329ea62 + 7284e57 commit 331b167
Show file tree
Hide file tree
Showing 46 changed files with 416 additions and 442 deletions.
4 changes: 2 additions & 2 deletions Include/Assets/Core/AssetHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ namespace CE
[](const AssetHandle<U>& value, nullptr_t)
{
return value == nullptr;
}, OperatorType::equal, MetaFunc::ExplicitParams<const AssetHandle<U>&, nullptr_t>{});
}, OperatorType::equal);

return refType;
}
Expand Down Expand Up @@ -405,7 +405,7 @@ namespace CE
[](const WeakAssetHandle<U>& value, nullptr_t)
{
return value == nullptr;
}, OperatorType::equal, MetaFunc::ExplicitParams<const WeakAssetHandle<U>&, nullptr_t>{});
}, OperatorType::equal);

return refType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ void CE::ReflectParticleComponentType(MetaType& type)
{
reg.AddComponent<ComponentType>(newEntity, std::move(*component));
}
}, Internal::sTransferOwnershipName, MetaFunc::ExplicitParams<Registry&, entt::entity, entt::entity>{});
}, Internal::sTransferOwnershipName);

BindEvent(type, sOnEndPlay, &Internal::OnParticleComponentDestruct);
ReflectComponentType<ComponentType>(type);
Expand Down
1 change: 1 addition & 0 deletions Include/Engine/Precomp/Precomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#endif


#include "entt/entity/component.hpp"
#include "entt/entity/entity.hpp"
#include "entt/entity/registry.hpp"
Expand Down
72 changes: 38 additions & 34 deletions Include/Meta/Fwd/MetaFuncFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ namespace CE
std::string mName{};
};

template<class T>
concept StringLike = std::is_convertible_v<T, std::string_view>;

/*
FuncResult contains information about wether the function call succeeded. Examples of reasons
why a call can fail are if one of the arguments is of the wrong type, if the number of arguments
Expand Down Expand Up @@ -129,8 +132,8 @@ namespace CE
MetaFunc func2{[]() { return 42; }, "ReturnTheAnswerToLifeTheUniverseAndEverything"};
// For lambdas and other functors with parameters, the parameters have to be explicitely provided
MetaFunc func3{[](int32 i, int32 j) { return i + j; }, "LambdaWithParams", MetaFunc::ExplicitParams<int32, int32>{} };
MetaFunc func4{std::equal<int32>(), "Functor", MetaFunc::ExplicitParams<int32, int32>{} };
MetaFunc func3{[](int32 i, int32 j) { return i + j; }, "LambdaWithParams" };
MetaFunc func4{std::equal<int32>(), "Functor" };
// In order to distinquish between overloads, you may have to cast it to the correct signature first.
// This is not specifically related to the MetaFunc class, but a restraint enforced by C++ itself.
Expand Down Expand Up @@ -160,47 +163,48 @@ namespace CE
const Params& parameters);

private:
struct ExplicitlyUseThisConstructor{};
explicit MetaFunc(InvokeT&& func,
NameOrType&& nameOrOp,
std::initializer_list<TypeTraits> paramTypes,
std::initializer_list<std::string_view> paramNames,
FuncId funcId,
ExplicitlyUseThisConstructor ctor);

MetaFunc(InvokeT&& func,
NameOrTypeInit nameOrType,
Params&& paramsAndReturnAtBack,
uint32 funcId);
OperatorType op,
std::initializer_list<TypeTraits> paramTypes,
std::initializer_list<std::string_view> paramNames,
FuncId funcId);

MetaFunc(InvokeT&& func,
std::string_view name,
std::initializer_list<TypeTraits> paramTypes,
std::initializer_list<std::string_view> paramNames,
FuncId funcId);

public:
template<typename Ret, typename... ParamsT, typename... ParamAndRetNames>
template<typename Ret, typename... ParamsT, typename NameOrOperator, StringLike... ParamAndRetNames>
MetaFunc(std::function<Ret(ParamsT...)>&& func,
NameOrTypeInit typeOrName,
NameOrOperator&& nameOrOp,
ParamAndRetNames&&... paramAndRetNames);

template<typename Functor, typename NameOrOperator, StringLike... ParamAndRetNames>
MetaFunc(Functor&& functor,
NameOrOperator&& nameOrType,
ParamAndRetNames&&... paramAndRetNames);

// Mutable member function
template<typename Ret, typename Obj, typename... ParamsT, typename... ParamAndRetNames>
MetaFunc(Ret(Obj::* func)(ParamsT...),
const NameOrTypeInit typeOrName, ParamAndRetNames&&... paramAndRetNames);
template<typename Ret, typename Obj, typename... ParamsT, typename NameOrOperator, StringLike... ParamAndRetNames>
MetaFunc(Ret(Obj::* func)(ParamsT...), NameOrOperator&& nameOrOp, ParamAndRetNames&&... paramAndRetNames);

// Const member function
template<typename Ret, typename Obj, typename... ParamsT, typename... ParamAndRetNames>
MetaFunc(Ret(Obj::* func)(ParamsT...) const,
const NameOrTypeInit typeOrName, ParamAndRetNames&&... paramAndRetNames);
template<typename Ret, typename Obj, typename... ParamsT, typename NameOrOperator, StringLike... ParamAndRetNames>
MetaFunc(Ret(Obj::* func)(ParamsT...) const, NameOrOperator&& nameOrOp, ParamAndRetNames&&... paramAndRetNames);

// Static function
template<typename Ret, typename... ParamsT, typename... ParamAndRetNames>
MetaFunc(Ret(*func)(ParamsT...),
const NameOrTypeInit typeOrName, ParamAndRetNames&&... paramAndRetNames);

// Functors without arguments
template<typename T, typename... ParamAndRetNames, std::enable_if_t<std::is_invocable_v<T>, bool> = true>
MetaFunc(const T& functor,
const NameOrTypeInit typeOrName, ParamAndRetNames&&... paramAndRetNames);

template<typename... T>
struct ExplicitParams
{
};

// Functors with arguments.
template<typename T, typename... ParamsT, typename... ParamAndRetNames>
MetaFunc(const T& functor,
const NameOrTypeInit typeOrName, const ExplicitParams<ParamsT...>,
ParamAndRetNames&&... paramAndRetNames);
template<typename Ret, typename... ParamsT, typename NameOrOperator, StringLike... ParamAndRetNames>
MetaFunc(Ret(*func)(ParamsT...), NameOrOperator&& typeOrName, ParamAndRetNames&&... paramAndRetNames);

MetaFunc(MetaFunc&& other) noexcept;
MetaFunc(const MetaFunc&) = delete;
Expand Down Expand Up @@ -340,10 +344,10 @@ namespace CE
static DynamicArg PackSingle(Arg&& arg);

template<>
STATIC_SPECIALIZATION DynamicArg PackSingle(const MetaAny& other);
DynamicArg PackSingle(const MetaAny& other);

template<>
STATIC_SPECIALIZATION DynamicArg PackSingle(MetaAny& other);
DynamicArg PackSingle(MetaAny& other);

private:
template<typename Ret, typename... ParamsT>
Expand Down
2 changes: 1 addition & 1 deletion Include/Meta/Fwd/MetaFuncIdFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace CE
FuncId MakeFuncId(TypeTraits returnType, const std::vector<TypeTraits>& parameters);

template<typename T>
constexpr FuncId MakeFuncId();
CONSTEVAL FuncId MakeFuncId();
};
21 changes: 4 additions & 17 deletions Include/Meta/Fwd/MetaReflectFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,13 @@ namespace CE

void RegisterReflectFunc(TypeId typeId, MetaType(*reflectFunc)());

template<typename T>
bool InitStaticDummy();

template<typename T>
struct ReflectAtStartup
{
inline static bool sDummy = []
{
static_assert(!sHasExternalReflect<T> || !sHasInternalReflect<T>, "Both an internal and external reflect function.");
static_assert(sHasExternalReflect<T> || sHasInternalReflect<T>,
R"(No external or internal reflect function. You need to make sure the Reflect function is included from wherever you are trying to reflect it.
If you are trying to reflect an std::vector<AssetHandle<Material>>, you need to include AssetHandle.h, Material.h and ReflectVector.h.)");

if constexpr (sHasInternalReflect<T>)
{
Internal::RegisterReflectFunc(MakeTypeId<T>(), ReflectAccess::GetReflectFunc<T>());
}
else if constexpr (sHasExternalReflect<T>)
{
Internal::RegisterReflectFunc(MakeTypeId<T>(), &Reflector<T>::Reflect);
}
return true;
}();
inline static bool sDummy = InitStaticDummy<T>();
};
}

Expand Down
12 changes: 8 additions & 4 deletions Include/Meta/Fwd/MetaTypeFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,22 @@ namespace CE
typevec2.AddField(&vec2::z, "Z");
*/
template<typename... Args>
MetaField& AddField(Args&& ...args);
MetaField& AddField(Args&&... args);

MetaField& AddField(MetaField&& field);

/*
Constructs a MetaFunc in place using the provided Args.
Constructs a MetaFunc using the provided Args.
Example:
typeFloat.AddFunc(&sinf, "Sin");
// For more examples, take a look at the comments documenting the MetaFunc class
*/
template<typename FuncPtr, typename... Args>
MetaFunc& AddFunc(FuncPtr&& funcPtr, MetaFunc::NameOrTypeInit nameOrType, Args&& ...args);
template<typename... Args>
MetaFunc& AddFunc(Args&& ...args);

MetaFunc& AddFunc(MetaFunc&& func);

/*
Add a baseclass. This function does not check if you ACTUALLY derive
Expand Down
4 changes: 2 additions & 2 deletions Include/Meta/Fwd/MetaTypeIdFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace CE
using TypeId = uint32;

template<typename T>
constexpr TypeId MakeTypeId();
CONSTEVAL TypeId MakeTypeId();

template<typename T>
constexpr std::string_view MakeTypeName();
CONSTEVAL std::string_view MakeTypeName();
}
8 changes: 4 additions & 4 deletions Include/Meta/Fwd/MetaTypeTraitsFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ namespace CE
constexpr bool CanFormBeNullable(TypeForm form);

template<typename T>
constexpr TypeTraits MakeTypeTraits();
CONSTEVAL TypeTraits MakeTypeTraits();

template<typename T>
constexpr TypeInfo MakeTypeInfo();
CONSTEVAL TypeInfo MakeTypeInfo();

template<typename T>
constexpr TypeForm MakeTypeForm();
CONSTEVAL TypeForm MakeTypeForm();

template<typename T>
constexpr TypeId MakeStrippedTypeId();
CONSTEVAL TypeId MakeStrippedTypeId();
}
2 changes: 1 addition & 1 deletion Include/Meta/Impl/MetaFuncIdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace CE::Internal
namespace CE
{
template<typename T>
constexpr FuncId MakeFuncId()
CONSTEVAL FuncId MakeFuncId()
{
return Internal::FunctionHasher<T>::value();
}
Expand Down
62 changes: 18 additions & 44 deletions Include/Meta/Impl/MetaFuncImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace CE::Internal
static constexpr bool ValidNumOfParamsV = ValidNumOfParams<FuncSig>::template Value<ParamAndRetNames...>();
}

template<typename Ret, typename... ParamsT, typename... ParamAndRetNames>
template<typename Ret, typename... ParamsT, typename NameOrOperator, CE::StringLike... ParamAndRetNames>
CE::MetaFunc::MetaFunc(std::function<Ret(ParamsT...)>&& func,
const NameOrTypeInit typeOrName,
NameOrOperator&& nameOrOp,
ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(
InvokeT
Expand All @@ -40,60 +40,34 @@ CE::MetaFunc::MetaFunc(std::function<Ret(ParamsT...)>&& func,
return DefaultInvoke<Ret, ParamsT...>(func, args, buffer);
}
},
typeOrName,
[&]
{
std::array<TypeTraits, sizeof...(ParamsT) + 1> paramTraits{ MakeTypeTraits<ParamsT>()..., MakeTypeTraits<Ret>() };
std::array<std::string, sizeof...(ParamAndRetNames)> paramNames{ std::forward<ParamAndRetNames>(paramAndRetNames)... };

std::vector<MetaFuncNamedParam> namedParams(sizeof...(ParamsT) + 1);

for (size_t i = 0; i < sizeof...(ParamsT) + 1; i++)
{
namedParams[i].mTypeTraits = paramTraits[i];
}

for (size_t i = 0; i < sizeof...(ParamAndRetNames); i++)
{
namedParams[i].mName = std::move(paramNames[i]);
}

return namedParams;
}(),
MakeFuncId<Ret(ParamsT...)>())
std::forward<NameOrOperator>(nameOrOp),
std::initializer_list<TypeTraits>{ MakeTypeTraits<ParamsT>()..., MakeTypeTraits<Ret>() },
std::initializer_list<std::string_view>{ std::forward<ParamAndRetNames>(paramAndRetNames)...},
MakeFuncId<Ret(ParamsT...)>())
{
static_assert(Internal::ValidNumOfParamsV<Ret(ParamsT...), ParamAndRetNames...>, "Too many names provided; First one name for each parameter, and if the function does not return void, one for the return value.");
}

template <typename Ret, typename Obj, typename ... ParamsT, typename ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(Ret(Obj::* func)(ParamsT...), const NameOrTypeInit typeOrName,
ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<Ret(Obj&, ParamsT...)>{ func }, typeOrName, std::forward<ParamAndRetNames>(paramAndRetNames)...)
template <typename Functor, typename NameOrOperator, CE::StringLike ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(Functor&& functor, NameOrOperator&& nameOrOp, ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function{ std::forward<Functor>(functor) }, std::forward<NameOrOperator>(nameOrOp), std::forward<ParamAndRetNames>(paramAndRetNames)...)
{}

template <typename Ret, typename Obj, typename ... ParamsT, typename ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(Ret(Obj::* func)(ParamsT...) const, const NameOrTypeInit typeOrName,
ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<Ret(const Obj&, ParamsT...)>{ func }, typeOrName, std::forward<ParamAndRetNames>(paramAndRetNames)...)
template <typename Ret, typename Obj, typename ... ParamsT, typename NameOrOperator, CE::StringLike ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(Ret(Obj::* func)(ParamsT...), NameOrOperator&& nameOrOp, ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<Ret(Obj&, ParamsT...)>{ func }, std::forward<NameOrOperator>(nameOrOp), std::forward<ParamAndRetNames>(paramAndRetNames)...)
{}

template <typename Ret, typename ... ParamsT, typename ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(Ret(*func)(ParamsT...), const NameOrTypeInit typeOrName, ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<Ret(ParamsT...)>{ func }, typeOrName, std::forward<ParamAndRetNames>(paramAndRetNames)...)
template <typename Ret, typename Obj, typename ... ParamsT, typename NameOrOperator, CE::StringLike ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(Ret(Obj::* func)(ParamsT...) const, NameOrOperator&& nameOrOp, ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<Ret(const Obj&, ParamsT...)>{ func }, std::forward<NameOrOperator>(nameOrOp), std::forward<ParamAndRetNames>(paramAndRetNames)...)
{}

template <typename T, typename ... ParamAndRetNames, std::enable_if_t<std::is_invocable_v<T>, bool>>
CE::MetaFunc::MetaFunc(const T& functor, const NameOrTypeInit typeOrName, ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<decltype(functor())()>{ functor }, typeOrName, std::forward<ParamAndRetNames>(paramAndRetNames)...)
template <typename Ret, typename ... ParamsT, typename NameOrOperator, CE::StringLike ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(Ret(*func)(ParamsT...), NameOrOperator&& nameOrOp, ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<Ret(ParamsT...)>{ func }, std::forward<NameOrOperator>(nameOrOp), std::forward<ParamAndRetNames>(paramAndRetNames)...)
{}

template <typename T, typename ... ParamsT, typename ... ParamAndRetNames>
CE::MetaFunc::MetaFunc(const T& functor, const NameOrTypeInit typeOrName, const ExplicitParams<ParamsT...>,
ParamAndRetNames&&... paramAndRetNames) :
MetaFunc(std::function<std::invoke_result_t<T, ParamsT...>(ParamsT...)>{ functor }, typeOrName, std::forward<ParamAndRetNames>(paramAndRetNames)...)
{
}

namespace CE::Internal
{
template<typename T, size_t I>
Expand Down
21 changes: 20 additions & 1 deletion Include/Meta/Impl/MetaReflectImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,23 @@
#include "Meta/ReflectedTypes/ReflectGLM.h"
#include "Meta/ReflectedTypes/STD/ReflectString.h"
#include "Meta/ReflectedTypes/ReflectENTT.h"
#include "Meta/ReflectedTypes/ReflectNull.h"
#include "Meta/ReflectedTypes/ReflectNull.h"

template<typename T>
bool CE::Internal::InitStaticDummy()
{
static_assert(!sHasExternalReflect<T> || !sHasInternalReflect<T>, "Both an internal and external reflect function.");
static_assert(sHasExternalReflect<T> || sHasInternalReflect<T>,
R"(No external or internal reflect function. You need to make sure the Reflect function is included from wherever you are trying to reflect it.
If you are trying to reflect an std::vector<AssetHandle<Material>>, you need to include AssetHandle.h, Material.h and ReflectVector.h.)");

if constexpr (sHasInternalReflect<T>)
{
Internal::RegisterReflectFunc(MakeTypeId<T>(), ReflectAccess::GetReflectFunc<T>());
}
else if constexpr (sHasExternalReflect<T>)
{
Internal::RegisterReflectFunc(MakeTypeId<T>(), &Reflector<T>::Reflect);
}
return true;
}
4 changes: 2 additions & 2 deletions Include/Meta/Impl/MetaTypeIdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#include "Meta/Fwd/MetaTypeIdFwd.h"

template<typename T>
constexpr CE::TypeId CE::MakeTypeId()
CONSTEVAL CE::TypeId CE::MakeTypeId()
{
return entt::type_hash<T>::value();
}

template<typename T>
constexpr std::string_view CE::MakeTypeName()
CONSTEVAL std::string_view CE::MakeTypeName()
{
return entt::type_name<T>::value();
}
Loading

0 comments on commit 331b167

Please sign in to comment.