Skip to content

Commit 3f1ba3c

Browse files
authored
Merge pull request #33 from ReflectCxx/release-2.0.CleanSfinae
replaced SFINAE with if constexpr(), cleaner approach
2 parents 1ca8ae1 + 7abff66 commit 3f1ba3c

File tree

5 files changed

+47
-148
lines changed

5 files changed

+47
-148
lines changed

ReflectionTemplateLib/common/Constants.h

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,6 @@ namespace rtl {
4141

4242
constexpr const char* NAMESPACE_GLOBAL = "namespace_global";
4343

44-
#define GETTER(_varType, _name, _var) \
45-
inline constexpr const _varType& get##_name() const { \
46-
return _var; \
47-
}
48-
49-
#define GETTER_REF(_varType, _name, _var) \
50-
inline _varType& get##_name() const { \
51-
return _var; \
52-
}
53-
54-
template<class _type>
55-
using enable_if_void = typename std::enable_if< std::is_same<_type, void>::value >::type;
56-
57-
template<class _type>
58-
using enable_if_non_void = typename std::enable_if< !std::is_same<_type, void>::value >::type;
59-
60-
template<class _typeA, class _typeB>
61-
using enable_if_same = typename std::enable_if< std::is_same<_typeA, _typeB>::value >::type;
62-
63-
template<class _type, class _typeB>
64-
using enable_if_not_same = typename std::enable_if< !std::is_same<_type, _typeB>::value >::type;
65-
6644
struct CtorName
6745
{
6846
static const std::string ctor(const std::string& pRecordName) {
@@ -81,4 +59,16 @@ namespace rtl {
8159
return (pRecordName + "::" + pRecordName + "(const " + pRecordName + "&)");
8260
}
8361
};
62+
63+
64+
#define GETTER(_varType, _name, _var) \
65+
inline constexpr const _varType& get##_name() const { \
66+
return _var; \
67+
}
68+
69+
70+
#define GETTER_REF(_varType, _name, _var) \
71+
inline _varType& get##_name() const { \
72+
return _var; \
73+
}
8474
}

ReflectionTemplateLib/detail/inc/SetupFunction.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ namespace rtl {
1212
protected:
1313

1414
template<class _returnType, class ..._signature>
15-
static const detail::FunctorId addFunctor(_returnType(*pFunctor)(_signature...),
16-
enable_if_void<_returnType> *_= nullptr);
17-
18-
template<class _returnType, class ..._signature>
19-
static const detail::FunctorId addFunctor(_returnType(*pFunctor)(_signature...),
20-
enable_if_non_void<_returnType> *_= nullptr);
15+
static const detail::FunctorId addFunctor(_returnType(*pFunctor)(_signature...));
2116
};
2217
}
2318
}

ReflectionTemplateLib/detail/inc/SetupFunction.hpp

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace rtl
88
{
99
template<class _derivedType>
1010
template<class _returnType, class ..._signature>
11-
inline const detail::FunctorId SetupFunction<_derivedType>::addFunctor(_returnType(*pFunctor)(_signature...),
12-
enable_if_void<_returnType> *_)
11+
inline const detail::FunctorId SetupFunction<_derivedType>::addFunctor(_returnType(*pFunctor)(_signature...))
1312
{
1413
static std::vector<std::pair<decltype(pFunctor), std::size_t>> functorSet;
1514
const auto& updateIndex = [&](const std::size_t& pIndex) {
@@ -24,46 +23,22 @@ namespace rtl
2423
return -1;
2524
};
2625

27-
const auto& functor = [=](_signature...params)->access::RStatus
28-
{
29-
(*pFunctor)(params...);
30-
return access::RStatus(Error::None);
31-
};
32-
33-
const std::size_t& index = _derivedType::pushBack(functor, getIndex, updateIndex);
34-
return detail::FunctorId(index, TypeId<_returnType>::get(), TypeId<>::None, _derivedType::getContainerId(),
35-
_derivedType::template getSignatureStr<_returnType>());
36-
}
37-
38-
39-
template<class _derivedType>
40-
template<class _returnType, class ..._signature>
41-
inline const detail::FunctorId SetupFunction<_derivedType>::addFunctor(_returnType(*pFunctor)(_signature...),
42-
enable_if_non_void<_returnType> *_)
43-
{
44-
static std::vector<std::pair<decltype(pFunctor), std::size_t>> functorSet;
45-
const auto& updateIndex = [&](const std::size_t& pIndex) {
46-
functorSet.emplace_back(pFunctor, pIndex);
47-
};
48-
const auto& getIndex = [&]()->const std::size_t {
49-
for (const auto& fptr : functorSet) {
50-
if (fptr.first == pFunctor) {
51-
return fptr.second;
52-
}
53-
}
54-
return -1;
55-
};
56-
57-
const auto& typeId = TypeId<_returnType>::get();
26+
const auto& retTypeId = TypeId<_returnType>::get();
5827
const auto functor = [=](_signature...params)->access::RStatus
5928
{
60-
const _returnType& retObj = (*pFunctor)(params...);
61-
const TypeQ& qualifier = std::is_const<_returnType>::value ? TypeQ::Const : TypeQ::Mute;
62-
return access::RStatus(std::make_any<_returnType>(retObj), typeId, qualifier);
29+
if constexpr (std::is_same_v<_returnType, void>) {
30+
(*pFunctor)(params...);
31+
return access::RStatus(Error::None);
32+
}
33+
else {
34+
const _returnType& retObj = (*pFunctor)(params...);
35+
const TypeQ& qualifier = std::is_const<_returnType>::value ? TypeQ::Const : TypeQ::Mute;
36+
return access::RStatus(std::make_any<_returnType>(retObj), retTypeId, qualifier);
37+
}
6338
};
6439

6540
const std::size_t& index = _derivedType::pushBack(functor, getIndex, updateIndex);
66-
return detail::FunctorId(index, TypeId<_returnType>::get(), TypeId<>::None, _derivedType::getContainerId(),
41+
return detail::FunctorId(index, retTypeId, TypeId<>::None, _derivedType::getContainerId(),
6742
_derivedType::template getSignatureStr<_returnType>());
6843
}
6944
}

ReflectionTemplateLib/detail/inc/SetupMethod.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,10 @@ namespace rtl {
1212
protected:
1313

1414
template<class _recordType, class _retType, class ..._signature>
15-
static const detail::FunctorId addFunctor(_retType(_recordType::* pFunctor)(_signature...),
16-
enable_if_void<_retType> *_= nullptr);
15+
static const detail::FunctorId addFunctor(_retType(_recordType::* pFunctor)(_signature...));
1716

1817
template<class _recordType, class _retType, class ..._signature>
19-
static const detail::FunctorId addFunctor(_retType(_recordType::* pFunctor)(_signature...),
20-
enable_if_non_void<_retType> *_= nullptr);
21-
22-
template<class _recordType, class _retType, class ..._signature>
23-
static const detail::FunctorId addFunctor(_retType(_recordType::* pFunctor)(_signature...) const,
24-
enable_if_void<_retType> *_= nullptr);
25-
26-
template<class _recordType, class _retType, class ..._signature>
27-
static const detail::FunctorId addFunctor(_retType(_recordType::* pFunctor)(_signature...) const,
28-
enable_if_non_void<_retType> *_= nullptr);
18+
static const detail::FunctorId addFunctor(_retType(_recordType::* pFunctor)(_signature...) const);
2919
};
3020
}
3121
}

ReflectionTemplateLib/detail/inc/SetupMethod.hpp

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,7 @@ namespace rtl
1010
{
1111
template<class _derivedType>
1212
template<class _recordType, class _retType, class ..._signature>
13-
inline const detail::FunctorId SetupMethod<_derivedType>::addFunctor(_retType(_recordType::* pFunctor)(_signature...),
14-
enable_if_void<_retType> *_)
15-
{
16-
static std::vector<std::pair<decltype(pFunctor), std::size_t>> functorSet;
17-
const auto& updateIndex = [&](const std::size_t& pIndex) {
18-
functorSet.emplace_back(pFunctor, pIndex);
19-
};
20-
const auto& getIndex = [&]()->const std::size_t {
21-
for (const auto& fptr : functorSet) {
22-
if (fptr.first == pFunctor) {
23-
return fptr.second;
24-
}
25-
}
26-
return -1;
27-
};
28-
29-
const auto functor = [=](const std::any& pTargetObj, _signature...params)->access::RStatus
30-
{
31-
_recordType* target = std::any_cast<_recordType*>(pTargetObj);
32-
(target->*pFunctor)(params...);
33-
return access::RStatus(Error::None);
34-
};
35-
36-
const std::size_t& index = _derivedType::pushBack(functor, getIndex, updateIndex);
37-
return detail::FunctorId(index, TypeId<_retType>::get(), TypeId<_recordType>::get(), _derivedType::getContainerId(),
38-
_derivedType::template getSignatureStr<_recordType, _retType>());
39-
}
40-
41-
42-
template<class _derivedType>
43-
template<class _recordType, class _retType, class ..._signature>
44-
inline const detail::FunctorId SetupMethod<_derivedType>::addFunctor(_retType(_recordType::* pFunctor)(_signature...),
45-
enable_if_non_void<_retType> *_)
13+
inline const detail::FunctorId SetupMethod<_derivedType>::addFunctor(_retType(_recordType::* pFunctor)(_signature...))
4614
{
4715
static std::vector<std::pair<decltype(pFunctor), std::size_t>> functorSet;
4816
const auto& updateIndex = [&](const std::size_t& pIndex) {
@@ -61,52 +29,27 @@ namespace rtl
6129
const auto functor = [=](const std::any& pTargetObj, _signature...params)->access::RStatus
6230
{
6331
_recordType* target = std::any_cast<_recordType*>(pTargetObj);
64-
const _retType& retObj = (target->*pFunctor)(params...);
65-
const TypeQ& qualifier = std::is_const<_retType>::value ? TypeQ::Const : TypeQ::Mute;
66-
return access::RStatus(std::make_any<_retType>(retObj), retTypeId, qualifier);
67-
};
6832

69-
const std::size_t& index = _derivedType::pushBack(functor, getIndex, updateIndex);
70-
return detail::FunctorId(index, retTypeId, TypeId<_recordType>::get(), _derivedType::getContainerId(),
71-
_derivedType::template getSignatureStr<_recordType, _retType>());
72-
}
73-
74-
75-
template<class _derivedType>
76-
template<class _recordType, class _retType, class ..._signature>
77-
inline const detail::FunctorId SetupMethod<_derivedType>::addFunctor(_retType(_recordType::* pFunctor)(_signature...) const,
78-
enable_if_void<_retType> *_)
79-
{
80-
static std::vector<std::pair<decltype(pFunctor), std::size_t>> functorSet;
81-
const auto& updateIndex = [&](const std::size_t& pIndex) {
82-
functorSet.emplace_back(pFunctor, pIndex);
83-
};
84-
const auto& getIndex = [&]()->const std::size_t {
85-
for (const auto& fptr : functorSet) {
86-
if (fptr.first == pFunctor) {
87-
return fptr.second;
88-
}
33+
if constexpr (std::is_same_v<_retType, void>) {
34+
(target->*pFunctor)(params...);
35+
return access::RStatus(Error::None);
36+
}
37+
else {
38+
const _retType& retObj = (target->*pFunctor)(params...);
39+
const TypeQ& qualifier = std::is_const<_retType>::value ? TypeQ::Const : TypeQ::Mute;
40+
return access::RStatus(std::make_any<_retType>(retObj), retTypeId, qualifier);
8941
}
90-
return -1;
91-
};
92-
93-
const auto functor = [=](const std::any& pTargetObj, _signature...params)->access::RStatus
94-
{
95-
_recordType* target = std::any_cast<_recordType*>(pTargetObj);
96-
((static_cast<const _recordType*>(target))->*pFunctor)(params...);
97-
return access::RStatus(Error::None);
9842
};
9943

10044
const std::size_t& index = _derivedType::pushBack(functor, getIndex, updateIndex);
101-
return detail::FunctorId(index, TypeId<_retType>::get(), TypeId<_recordType>::get(), _derivedType::getContainerId(),
45+
return detail::FunctorId(index, retTypeId, TypeId<_recordType>::get(), _derivedType::getContainerId(),
10246
_derivedType::template getSignatureStr<_recordType, _retType>());
10347
}
10448

10549

10650
template<class _derivedType>
10751
template<class _recordType, class _retType, class ..._signature>
108-
inline const detail::FunctorId SetupMethod<_derivedType>::addFunctor(_retType(_recordType::* pFunctor)(_signature...) const,
109-
enable_if_non_void<_retType> *_)
52+
inline const detail::FunctorId SetupMethod<_derivedType>::addFunctor(_retType(_recordType::* pFunctor)(_signature...) const)
11053
{
11154
static std::vector<std::pair<decltype(pFunctor), std::size_t>> functorSet;
11255
const auto& updateIndex = [&](const std::size_t& pIndex) {
@@ -126,9 +69,15 @@ namespace rtl
12669
const auto functor = [=](const std::any& pTargetObj, _signature...params)->access::RStatus
12770
{
12871
_recordType* target = std::any_cast<_recordType*>(pTargetObj);
129-
const TypeQ& qualifier = std::is_const<_retType>::value ? TypeQ::Const : TypeQ::Mute;
130-
const _retType& retObj = ((static_cast<const _recordType*>(target))->*pFunctor)(params...);
131-
return access::RStatus(std::make_any<_retType>(retObj), retTypeId, constRetTypeId, qualifier);
72+
if constexpr (std::is_same_v<_retType, void>) {
73+
((static_cast<const _recordType*>(target))->*pFunctor)(params...);
74+
return access::RStatus(Error::None);
75+
}
76+
else {
77+
const TypeQ& qualifier = std::is_const<_retType>::value ? TypeQ::Const : TypeQ::Mute;
78+
const _retType& retObj = ((static_cast<const _recordType*>(target))->*pFunctor)(params...);
79+
return access::RStatus(std::make_any<_retType>(retObj), retTypeId, constRetTypeId, qualifier);
80+
}
13281
};
13382

13483
const std::size_t& index = _derivedType::pushBack(functor, getIndex, updateIndex);

0 commit comments

Comments
 (0)