Skip to content

Commit 0af7adf

Browse files
committed
integrated functor/lambda static cache and other types.
1 parent d1c80a3 commit 0af7adf

14 files changed

Lines changed: 485 additions & 315 deletions

RTLBenchmarkApp/src/ReflectedCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace
2222
{
2323
auto Node = cxx::mirror().getRecord("Node").value();
2424
auto [err, robj] = Node.create<rtl::alloc::Stack>();
25-
if (nodeObj.isEmpty()) {
25+
if (robj.isEmpty()) {
2626
std::cout << "[0] nodeObj empty! \n";
2727
}
2828
return std::move(robj);

ReflectionTemplateLib/detail/inc/CallReflector.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@ namespace rtl::detail {
3333
*/ template<class ..._params>
3434
FORCE_INLINE static Return forwardCall(const detail::FunctorId& pFunctorId, _params&&..._args)
3535
{
36-
// static_cast to derived type, gaurateed safe by design.
37-
//auto lambdaTable = static_cast<_derivedType::lambda_t*>(pFunctorId.m_lambdaTable);
38-
//return lambdaTable->get()[pFunctorId.m_index](std::forward<_params>(_args)...);
39-
4036
//'getFunctors()' must be implemented by _derivedType (FunctorContainer).
41-
return _derivedType::getFunctors()[pFunctorId.m_index](std::forward<_params>(_args)...);
37+
return _derivedType::getFunctors()[pFunctorId.m_lambdaIndex](std::forward<_params>(_args)...);
4238
}
4339

4440

ReflectionTemplateLib/detail/inc/FunctorContainer.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <vector>
1616
#include <functional>
1717

18-
#include "LambdaTable.h"
18+
#include "LambdaRegistry.h"
1919
#include "Constants.h"
2020
#include "CallReflector.h"
2121
#include "SetupFunction.h"
@@ -28,10 +28,10 @@ namespace rtl {
2828
//forward decl
2929
class ReflectionBuilder;
3030

31-
/* @class: FunctorContainer
32-
@param: '_signature...' (combination of any types)
33-
* container class for holding lambda's wrapping functor, constructor calls of same signatures.
34-
* maintains a std::vector<std::function> with static lifetime.
31+
/* @class: FunctorContainer
32+
@param: '_signature...' (combination of any types)
33+
* container class for holding lambda_hop's wrapping functor, constructor calls of same signatures.
34+
* maintains a std::vector<std::function> with static lifetime.
3535
*/ template<class ..._signature>
3636
class FunctorContainer : public SetupFunction<FunctorContainer<_signature...>>,
3737
public SetupConstructor<FunctorContainer<_signature...>>,
@@ -40,7 +40,7 @@ namespace rtl {
4040
using FunctionLambda = std::function < Return(_signature...) >;
4141
public:
4242

43-
using lambda_t = detail::functors<_signature...>;
43+
using lambda_t = detail::lambda_registry<_signature...>;
4444

4545
//every FunctorContainer<...> will have a unique-id.
4646
FORCE_INLINE static std::size_t getContainerId() {
@@ -62,6 +62,14 @@ namespace rtl {
6262
"(" + TypeId<_signature...>::toString() + ")");
6363
}
6464

65+
66+
static lambda_t& lambdaCache()
67+
{
68+
static lambda_t lambdaRegistry;
69+
return lambdaRegistry;
70+
}
71+
72+
6573
private:
6674

6775
//vector holding lambdas
@@ -70,18 +78,13 @@ namespace rtl {
7078
return functorTable;
7179
}
7280

73-
static lambda_t& lambdaCache()
74-
{
75-
static lambda_t functorsCache;
76-
return functorsCache;
77-
}
7881

7982
/* @method: pushBack
8083
@params: pFunctor (lambda containing functor or constructor call)
8184
pGetIndex (lambda providing index if the functor is already registered)
8285
pUpdate (lambda updating the already registered functors/ctor/d'tor set)
8386
@return: index of newly added or already existing lambda in vector 'm_functors'.
84-
*/ static std::pair<std::size_t, detail::lambda_table*> pushBack(const FunctionLambda& pFunctor,
87+
*/ static std::pair<std::size_t, detail::lambda_hop*> pushBack(const FunctionLambda& pFunctor,
8588
std::function<const std::size_t()> pGetIndex,
8689
std::function<void(const std::size_t&)> pUpdate)
8790
{
@@ -94,7 +97,7 @@ namespace rtl {
9497
{
9598
index = lambdaCache().get().size();
9699

97-
lambdaCache().pushBack(pFunctor);
100+
lambdaCache().push(pFunctor);
98101

99102
getFunctorTable().push_back(pFunctor);
100103

ReflectionTemplateLib/detail/inc/FunctorId.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace rtl::detail
1818
{
19-
class lambda_table;
19+
class lambda_hop;
2020

2121
/* @class: FunctorId
2222
* 'FunctorId' object is generated for every functor (member/non-member function pointer) registered.
@@ -28,7 +28,9 @@ namespace rtl::detail
2828
*/ struct FunctorId
2929
{
3030
//index of the functor in the functor-table.
31-
std::size_t m_index;
31+
std::size_t m_lambdaIndex;
32+
33+
std::size_t m_functorIndex;
3234

3335
//return type-id of the functor registered.
3436
std::size_t m_returnId;
@@ -42,9 +44,9 @@ namespace rtl::detail
4244
//signature of functor as string. platform dependent, may not be very much readable format.
4345
std::string m_signature;
4446

45-
lambda_table* m_lambdaTable = nullptr;
47+
lambda_hop* m_lambdas = nullptr;
4648

47-
GETTER(std::size_t, Index, m_index)
49+
GETTER(std::size_t, Index, m_lambdaIndex)
4850
GETTER(std::size_t, ReturnId, m_returnId);
4951
GETTER(std::size_t, RecordId, m_recordId);
5052
GETTER(std::size_t, SignatureId, m_containerId)
@@ -63,15 +65,18 @@ namespace rtl::detail
6365
*/ std::size_t getHashCode() const
6466
{
6567
return std::stoull(std::to_string(m_containerId) +
66-
std::to_string(m_index) +
68+
std::to_string(m_lambdaIndex) +
6769
std::to_string(m_recordId) +
6870
std::to_string(m_returnId));
6971
}
7072

7173
const bool operator==(const FunctorId& pOther) const
7274
{
73-
return (m_index == pOther.m_index && m_returnId == pOther.m_returnId &&
74-
m_recordId == pOther.m_recordId && m_containerId == pOther.m_containerId &&
75+
return (m_returnId == pOther.m_returnId &&
76+
m_recordId == pOther.m_recordId &&
77+
m_containerId == pOther.m_containerId &&
78+
m_lambdaIndex == pOther.m_lambdaIndex &&
79+
m_functorIndex == pOther.m_functorIndex &&
7580
m_signature == pOther.m_signature);
7681
}
7782
};
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*************************************************************************
2+
* *
3+
* Reflection Template Library (RTL) - Modern C++ Reflection Framework *
4+
* https://github.com/ReflectCxx/ReflectionTemplateLibrary-CPP *
5+
* *
6+
* Copyright (c) 2025 Neeraj Singh <reflectcxx@outlook.com> *
7+
* SPDX-License-Identifier: MIT *
8+
* *
9+
*************************************************************************/
10+
11+
12+
#pragma once
13+
14+
#include <vector>
15+
16+
namespace rtl {
17+
struct Return;
18+
}
19+
20+
21+
namespace rtl::detail
22+
{
23+
class functor_hop {};
24+
}
25+
26+
27+
namespace rtl::detail
28+
{
29+
template<class return_t, class ...signature_ts>
30+
class functor_registry : public functor_hop
31+
{
32+
using functor_t = return_t(*)(signature_ts...);
33+
34+
std::vector<std::pair<functor_t, std::size_t>> m_functors;
35+
36+
public:
37+
38+
functor_t operator[](std::size_t index) {
39+
return m_functors[index].first;
40+
}
41+
42+
void push(functor_t fptr, std::size_t lambda_index) {
43+
m_functors.emplace_back(fptr, lambda_index);
44+
}
45+
46+
std::size_t find(functor_t fptr)
47+
{
48+
//linear search, efficient for small set.
49+
for (const auto& itr : m_functors) {
50+
if (itr.first == fptr) {
51+
//functor already registered, return its 'index'.
52+
return itr.second;
53+
}
54+
}
55+
//functor is not already registered, return '-1'.
56+
return rtl::index_none;
57+
}
58+
};
59+
}
60+
61+
62+
namespace rtl::detail
63+
{
64+
template<class record_t, class return_t, class ...signature_ts>
65+
class method_registry : public functor_hop
66+
{
67+
using functor_t = return_t(record_t::*)(signature_ts...);
68+
69+
std::vector<std::pair<functor_t, std::size_t>> m_functors;
70+
71+
public:
72+
73+
functor_t operator[](std::size_t index) {
74+
return m_functors[index].first;
75+
}
76+
77+
void push(functor_t fptr, std::size_t lambda_index) {
78+
m_functors.emplace_back(fptr, lambda_index);
79+
}
80+
81+
std::size_t find(functor_t fptr)
82+
{
83+
//linear search, efficient for small set.
84+
for (const auto& itr : m_functors) {
85+
if (itr.first == fptr) {
86+
//functor already registered, return its 'index'.
87+
return itr.second;
88+
}
89+
}
90+
//functor is not already registered, return '-1'.
91+
return rtl::index_none;
92+
}
93+
};
94+
}
95+
96+
97+
namespace rtl::detail
98+
{
99+
template<class record_t, class return_t, class ...signature_ts>
100+
class const_method_registry : public functor_hop
101+
{
102+
using functor_t = return_t(record_t::*)(signature_ts...) const;
103+
104+
std::vector<std::pair<functor_t, std::size_t>> m_functors;
105+
106+
public:
107+
108+
functor_t operator[](std::size_t index) {
109+
return m_functors[index].first;
110+
}
111+
112+
void push(functor_t fptr, std::size_t lambda_index) {
113+
m_functors.emplace_back(fptr, lambda_index);
114+
}
115+
116+
std::size_t find(functor_t fptr)
117+
{
118+
//linear search, efficient for small set.
119+
for (const auto& itr : m_functors) {
120+
if (itr.first == fptr) {
121+
//functor already registered, return its 'index'.
122+
return itr.second;
123+
}
124+
}
125+
//functor is not already registered, return '-1'.
126+
return rtl::index_none;
127+
}
128+
};
129+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*************************************************************************
2+
* *
3+
* Reflection Template Library (RTL) - Modern C++ Reflection Framework *
4+
* https://github.com/ReflectCxx/ReflectionTemplateLibrary-CPP *
5+
* *
6+
* Copyright (c) 2025 Neeraj Singh <reflectcxx@outlook.com> *
7+
* SPDX-License-Identifier: MIT *
8+
* *
9+
*************************************************************************/
10+
11+
12+
#pragma once
13+
14+
#include "FunctorId.h"
15+
#include "FunctorRegistry.h"
16+
17+
namespace rtl {
18+
struct Return;
19+
}
20+
21+
namespace rtl::detail::bridge
22+
{
23+
template<class ...signature_ts>
24+
struct lambda_def
25+
{
26+
template<class return_t>
27+
static auto get()
28+
{
29+
return [](const FunctorId& functorId, signature_ts&&...params)
30+
{
31+
32+
};
33+
}
34+
};
35+
}

0 commit comments

Comments
 (0)