Skip to content

Commit

Permalink
ETI_TEMPLATE_1_EXTERNAL , ETI_TEMPLATE_2_EXTERNAL
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Thiffeault committed Apr 1, 2024
1 parent dd8d89e commit 2643087
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 90 deletions.
64 changes: 52 additions & 12 deletions eti/eti.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,13 @@ namespace eti
const Variable* GetFunctionReturn(RETURN(OBJECT::* func)(ARGS...) const);

template<typename RETURN, typename... ARGS>
std::span<Variable> GetFunctionVariables(RETURN(*func)(ARGS...));
std::span<Variable> GetFunctionArguments(RETURN(*func)(ARGS...));

template<typename OBJECT, typename RETURN, typename... ARGS>
std::span<Variable> GetFunctionVariables(RETURN(OBJECT::* func)(ARGS...));
std::span<Variable> GetFunctionArguments(RETURN(OBJECT::* func)(ARGS...));

template<typename OBJECT, typename RETURN, typename... ARGS>
std::span<Variable> GetFunctionVariables(RETURN(OBJECT::* func)(ARGS...) const);
std::span<Variable> GetFunctionArguments(RETURN(OBJECT::* func)(ARGS...) const);

static Method MakeMethod(std::string_view name, bool isStatic, bool isConst, const Type& parent, std::function<void(void*, void*, std::span<void*>)>&& function, const Variable* _return = nullptr, std::span<const Variable> arguments = {}, std::vector<std::shared_ptr<Attribute>>&& attributes = {});

Expand Down Expand Up @@ -810,7 +810,20 @@ namespace eti
::eti::utils::CallFunction(&Self::NAME, obj, _return, args); \
}, \
::eti::internal::GetFunctionReturn(&Self::NAME), \
::eti::internal::GetFunctionVariables(&Self::NAME), \
::eti::internal::GetFunctionArguments(&Self::NAME), \
::eti::internal::GetAttributes<::eti::Attribute>(__VA_ARGS__))

#define ETI_METHOD_OVERLOAD(NAME, METHOD_TYPE, ...) \
::eti::internal::MakeMethod(#NAME, \
::eti::utils::IsMethodStatic<decltype((METHOD_TYPE)&Self::NAME)>, \
::eti::utils::IsMethodConst<decltype((METHOD_TYPE)&Self::NAME)>, \
::eti::TypeOf<Self>(), \
[](void* obj, void* _return, std::span<void*> args) \
{ \
::eti::utils::CallFunction((METHOD_TYPE)&Self::NAME, obj, _return, args); \
}, \
::eti::internal::GetFunctionReturn((METHOD_TYPE)&Self::NAME), \
::eti::internal::GetFunctionArguments((METHOD_TYPE)&Self::NAME), \
::eti::internal::GetAttributes<::eti::Attribute>(__VA_ARGS__))

#define ETI_INTERNAL_METHOD(...) \
Expand Down Expand Up @@ -1003,7 +1016,7 @@ namespace eti
}


#define ETI_TEMPLATE_1(TEMPLATE_NAME) \
#define ETI_TEMPLATE_1_EXTERNAL(TEMPLATE_NAME, PROPERTIES, METHODS, ...) \
namespace eti \
{ \
template <typename T1> \
Expand All @@ -1017,28 +1030,32 @@ namespace eti
if (initializing == false) \
{ \
initializing = true; \
type = ::eti::internal::MakeType<Self>(::eti::Kind::Template, nullptr, {}, {}, ::eti::internal::GetTypes<T1>()); \
static std::vector<::eti::Property> properties = { PROPERTIES }; \
static std::vector<::eti::Method> methods = { METHODS }; \
type = ::eti::internal::MakeType<Self>(::eti::Kind::Template, nullptr, properties, methods, ::eti::internal::GetTypes<T1>(), ::eti::internal::GetAttributes<::eti::Attribute>(__VA_ARGS__)); \
} \
return type; \
} \
}; \
}

#define ETI_TEMPLATE_2(TEMPLATE_NAME) \
#define ETI_TEMPLATE_2_EXTERNAL(TEMPLATE_NAME, PROPERTIES, METHODS, ...) \
namespace eti \
{ \
template <typename T1, typename T2> \
struct TypeOfImpl<TEMPLATE_NAME<T1, T2>> \
{ \
static const ::eti::Type& GetTypeStatic() \
{ \
using Self = TEMPLATE_NAME<T1, T2>; \
using Self = TEMPLATE_NAME<T1,T2>; \
static bool initializing = false; \
static ::eti::Type type; \
if (initializing == false) \
{ \
initializing = true; \
type = ::eti::internal::MakeType<Self>(::eti::Kind::Template, nullptr, {}, {}, ::eti::internal::GetTypes<T1, T2>()); \
static std::vector<::eti::Property> properties = { PROPERTIES }; \
static std::vector<::eti::Method> methods = { METHODS }; \
type = ::eti::internal::MakeType<Self>(::eti::Kind::Template, nullptr, properties, methods, ::eti::internal::GetTypes<T1,T2>(), ::eti::internal::GetAttributes<::eti::Attribute>(__VA_ARGS__)); \
} \
return type; \
} \
Expand Down Expand Up @@ -1119,19 +1136,19 @@ namespace eti


template<typename RETURN, typename... ARGS>
std::span<Variable> GetFunctionVariables(RETURN(*)(ARGS...))
std::span<Variable> GetFunctionArguments(RETURN(*)(ARGS...))
{
return internal::GetVariables<ARGS...>();
}

template<typename OBJECT, typename RETURN, typename... ARGS>
std::span<Variable> GetFunctionVariables(RETURN(OBJECT::*)(ARGS...))
std::span<Variable> GetFunctionArguments(RETURN(OBJECT::*)(ARGS...))
{
return internal::GetVariables<ARGS...>();
}

template<typename OBJECT, typename RETURN, typename... ARGS>
std::span<Variable> GetFunctionVariables(RETURN(OBJECT::*)(ARGS...) const)
std::span<Variable> GetFunctionArguments(RETURN(OBJECT::*)(ARGS...) const)
{
return internal::GetVariables<ARGS...>();
}
Expand Down Expand Up @@ -1747,4 +1764,27 @@ class Object
virtual ~Object(){}
};

ETI_TEMPLATE_1_EXTERNAL
(std::vector,
ETI_PROPERTIES(),
ETI_METHODS
(
ETI_METHOD(size),
ETI_METHOD_OVERLOAD(at, T1& (std::vector<T1>::*)(const std::vector<T1>::size_type)),
ETI_METHOD_OVERLOAD(push_back, void (std::vector<T1>::*)(const T1&)),
ETI_METHOD_OVERLOAD(erase, std::vector<T1>::iterator (std::vector<T1>::*)(std::vector<T1>::const_iterator ))
)
)

ETI_TEMPLATE_2_EXTERNAL
(
std::map,
ETI_PROPERTIES(),
ETI_METHODS
(
ETI_METHOD(size)
/*ETI_METHOD(insert)*/
)
)

#endif // #if ETI_COMMON_TYPE
78 changes: 0 additions & 78 deletions unittest/eti_playground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,11 @@
#include <eti/eti.h>
#include <iostream>


#define ETI_METHOD_OVERLOAD(NAME, METHOD_TYPE, ...) \
::eti::internal::MakeMethod(#NAME, \
::eti::utils::IsMethodStatic<decltype((METHOD_TYPE)&Self::NAME)>, \
::eti::utils::IsMethodConst<decltype((METHOD_TYPE)&Self::NAME)>, \
::eti::TypeOf<Self>(), \
[](void* obj, void* _return, std::span<void*> args) \
{ \
::eti::utils::CallFunction((METHOD_TYPE)&Self::NAME, obj, _return, args); \
}, \
::eti::internal::GetFunctionReturn((METHOD_TYPE)&Self::NAME), \
::eti::internal::GetFunctionVariables((METHOD_TYPE)&Self::NAME), \
::eti::internal::GetAttributes<::eti::Attribute>(__VA_ARGS__))

#define ETI_TEMPLATE_1_EXTERNAL(TEMPLATE_NAME, PROPERTIES, METHODS, ...) \
namespace eti \
{ \
template <typename T1> \
struct TypeOfImpl<TEMPLATE_NAME<T1>> \
{ \
static const ::eti::Type& GetTypeStatic() \
{ \
using Self = TEMPLATE_NAME<T1>; \
static bool initializing = false; \
static ::eti::Type type; \
if (initializing == false) \
{ \
initializing = true; \
static std::vector<::eti::Property> properties = { PROPERTIES }; \
static std::vector<::eti::Method> methods = { METHODS }; \
type = ::eti::internal::MakeType<Self>(::eti::Kind::Template, nullptr, properties, methods, ::eti::internal::GetTypes<T1>(), ::eti::internal::GetAttributes<::eti::Attribute>(__VA_ARGS__)); \
} \
return type; \
} \
}; \
}

typedef int& (std::vector<int>::* AtType)(const size_t);

ETI_TEMPLATE_1_EXTERNAL
(std::vector,
ETI_PROPERTIES(),
ETI_METHODS
(
ETI_METHOD(size),
//ETI_METHOD_OVERLOAD(at, AtType),
ETI_METHOD_OVERLOAD(push_back, void (std::vector<T1>::*)(const T1&)),
ETI_METHOD_OVERLOAD(erase, std::vector<int>::iterator (std::vector<int>::*)(std::vector<int>::const_iterator ))
)
)


using namespace eti;

namespace playground
{
TEST_CASE("playground")
{
std::vector<int> v;
int i = 2;

//std::vector<int>::erase()

const Type& type = TypeOf<std::vector<int>>();

typedef void (std::vector<int>::*PushBackType)(const int&);
//auto ptrFunc = (void (std::vector<int>::*)(const int&)) & std::vector<int>::push_back;

//auto ptrFunc2 = (std::vector<int>::iterator (std::vector<int>::*)(std::vector<int>::const_iterator )) & std::vector<int>::erase;

//auto ptrFunc3 = (int& (std::vector<int>::*)(const size_t )) & std::vector<int>::at;

const Method* pushBack = type.GetMethod("push_back");
pushBack->CallMethod(v,NoReturn, &i);
REQUIRE(v.size() == 1);
REQUIRE(v[0] == 2);

const Method* erase = type.GetMethod("erase");
std::vector<int>::iterator itResult;
std::vector<int>::const_iterator at = v.begin();
erase->CallMethod(v, &itResult, at);
REQUIRE(v.size() == 0);

// v.at
}
}
53 changes: 53 additions & 0 deletions unittest/eti_unittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,4 +1296,57 @@ namespace test_24

}
}

// vector
TEST_CASE("test_25")
{
std::vector<int> vector;
int i = 2;

const Type& type = TypeOf<std::vector<int>>();

// push back
const Method* pushBackMethod = type.GetMethod("push_back");
pushBackMethod->CallMethod(vector,NoReturn, &i);
REQUIRE(vector.size() == 1);
REQUIRE(vector[0] == 2);

// at (operator[])
const Method* atMethod = type.GetMethod("at");
int* atValue;
atMethod->CallMethod(vector, &atValue, (size_t)0);
REQUIRE(*atValue == 2);
*atValue = 1;
REQUIRE(vector[0] == 1);

// size
const Method* sizeMethod = type.GetMethod("size");
size_t size;
sizeMethod->CallMethod(vector, &size);
REQUIRE(size == 1);

// erase
const Method* eraseMethod = type.GetMethod("erase");
std::vector<int>::iterator itResult;
std::vector<int>::const_iterator begin = vector.begin();
eraseMethod->CallMethod(vector, &itResult, begin);
REQUIRE(vector.size() == 0);
}

// map
TEST_CASE("test_26")
{
std::map<std::string, int> map;
map["1212"] = 1212;

const Type& type = TypeOf<std::map<std::string, int>>();

// size
const Method* sizeMethod = type.GetMethod("size");
size_t size;
sizeMethod->CallMethod(map, &size);
REQUIRE(size == 1);

map.insert(std::make_pair("3434", 3434));
}
}

0 comments on commit 2643087

Please sign in to comment.