Skip to content

Commit 5762021

Browse files
committed
functor/lambda index added to type metadata.
1 parent 0af7adf commit 5762021

22 files changed

Lines changed: 404 additions & 206 deletions

RTLBenchmarkApp/src/ReflectedCall.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,37 @@ namespace cxx
1212

1313
namespace
1414
{
15-
static rtl::Function GetMessage = cxx::mirror().getFunction("getMessage").value();
16-
static rtl::Function SendMessage = cxx::mirror().getFunction("sendMessage").value();
15+
static rtl::Function GetMessage;
16+
static rtl::Function SendMessage;
1717

18-
static rtl::Method NodeGetMessage = cxx::mirror().getRecord("Node")->getMethod("getMessage").value();
19-
static rtl::Method NodeSendMessage = cxx::mirror().getRecord("Node")->getMethod("sendMessage").value();
18+
static rtl::Method NodeGetMessage;
19+
static rtl::Method NodeSendMessage;
2020

2121
static rtl::RObject nodeObj = []()
2222
{
23+
GetMessage = cxx::mirror().getFunction("getMessage").value();
24+
25+
SendMessage = cxx::mirror().getFunction("sendMessage").value();
26+
2327
auto Node = cxx::mirror().getRecord("Node").value();
28+
29+
NodeGetMessage = Node.getMethod("getMessage").value();
30+
31+
NodeSendMessage = Node.getMethod("sendMessage").value();
32+
2433
auto [err, robj] = Node.create<rtl::alloc::Stack>();
34+
2535
if (robj.isEmpty()) {
26-
std::cout << "[0] nodeObj empty! \n";
36+
std::cout << "[0] error: " << rtl::to_string(err) << "\n";
2737
}
38+
2839
return std::move(robj);
2940
}();
3041
}
3142

3243

33-
namespace
34-
{
44+
namespace
45+
{
3546
static auto _test0 = []()
3647
{
3748
auto err = SendMessage(bm::g_longStr).err;

ReflectionTemplateLib/access/inc/Function.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace rtl
5858
//simple linear-search, efficient for small set of elements.
5959
for (const auto& functorId : m_functorIds) {
6060
if (functorId.getSignatureId() == pSignatureId) [[likely]] {
61-
return functorId.getIndex();
61+
return functorId.getLambdaIndex();
6262
}
6363
}
6464
return rtl::index_none;

ReflectionTemplateLib/access/inc/Record.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ namespace rtl {
9494
{
9595
static_assert(_alloc != rtl::alloc::None, "Instance cannot be created with 'rtl::alloc::None' option.");
9696
const auto& method = m_methods.at(detail::ctor_name(m_recordName));
97-
std::size_t copyCtorIndex = method.getFunctorIds()[detail::Index::CopyCtor].getIndex();
97+
std::size_t copyCtorIndex = method.getFunctorIds()[detail::Index::CopyCtor].getLambdaIndex();
9898
return method.invokeCtor(_alloc, copyCtorIndex, std::forward<_ctorArgs>(params)...);
9999
}
100100

ReflectionTemplateLib/access/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SET(COMMON_HEADERS
1212
"${PROJECT_SOURCE_DIR}/common/Constants.h"
1313
"${PROJECT_SOURCE_DIR}/common/rtl_traits.h"
1414
"${PROJECT_SOURCE_DIR}/common/error_codes.h"
15+
"${PROJECT_SOURCE_DIR}/common/forward_decls.h"
1516
"${PROJECT_SOURCE_DIR}/common/ConversionUtils.h"
1617
"${PROJECT_SOURCE_DIR}/common/RTLibInterface.h"
1718
)

ReflectionTemplateLib/access/src/CxxMirror.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace rtl
3232
{
3333
const Record& record = itr->second;
3434
Method ctors = record.getMethod(detail::ctor_name(record.getRecordName())).value();
35-
std::size_t copyCtorIndex = ctors.getFunctors()[detail::Index::CopyCtor].getIndex();
35+
std::size_t copyCtorIndex = ctors.getFunctors()[detail::Index::CopyCtor].getLambdaIndex();
3636
const_cast<RObject&>(pTarget).m_objectId.m_clonerIndex = copyCtorIndex;
3737
return error::None;
3838
}

ReflectionTemplateLib/access/src/CxxMirrorToJson.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ static const std::string toJson(const FunctorId& pFunctorId)
2626
{
2727
std::stringstream sout;
2828
sout << "{\"containerId\": \"" << std::to_string(pFunctorId.getSignatureId()) << "\",";
29-
sout << "\"index\": \"" << std::to_string(pFunctorId.getIndex()) << "\",";
29+
sout << "\"lambdaIndex\": \"" << std::to_string(pFunctorId.getLambdaIndex()) << "\",";
30+
if (pFunctorId.getFunctorIndex() != rtl::index_none) {
31+
sout << "\"functorIndex\": \"" << std::to_string(pFunctorId.getFunctorIndex()) << "\",";
32+
}
33+
else {
34+
sout << "\"functorIndex\": \"-1\",";
35+
}
3036
if (pFunctorId.getRecordId() != TypeId<>::None) {
3137
sout << "\"recordId\": \"" << std::to_string(pFunctorId.getRecordId()) << "\",";
3238
}

ReflectionTemplateLib/builder/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Create a variable containing the source files for your target
22

33
SET(COMMON_HEADERS
4-
"${PROJECT_SOURCE_DIR}/common/Constants.h"
4+
"${PROJECT_SOURCE_DIR}/common/rtl_traits.h"
5+
"${PROJECT_SOURCE_DIR}/common/error_codes.h"
6+
"${PROJECT_SOURCE_DIR}/common/forward_decls.h"
57
)
68

79
SET(LOCAL_HEADERS
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
namespace rtl {
15+
16+
struct Return;
17+
18+
class RObject;
19+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 "Constants.h"
15+
#include "FunctorRegistry.h"
16+
17+
18+
namespace rtl::detail
19+
{
20+
template<methodQ>
21+
struct functor_cache;
22+
}
23+
24+
namespace rtl::detail
25+
{
26+
template<>
27+
struct functor_cache<methodQ::None>
28+
{
29+
template<class return_t, class ...signature_ts>
30+
static functor_registry<return_t, signature_ts...>& get()
31+
{
32+
static functor_registry<return_t, signature_ts...> functors;
33+
return functors;
34+
}
35+
};
36+
37+
template<>
38+
struct functor_cache<methodQ::Const>
39+
{
40+
template<class record_t, class return_t, class ...signature_ts>
41+
static functor_registry_m<methodQ::Const, record_t, return_t, signature_ts...>& get()
42+
{
43+
static functor_registry_m<methodQ::Const, record_t, return_t, signature_ts...> functors;
44+
return functors;
45+
}
46+
};
47+
48+
template<>
49+
struct functor_cache<methodQ::NonConst>
50+
{
51+
template<class record_t, class return_t, class ...signature_ts>
52+
static functor_registry_m<methodQ::NonConst, record_t, return_t, signature_ts...>& get()
53+
{
54+
static functor_registry_m<methodQ::NonConst, record_t, return_t, signature_ts...> functors;
55+
return functors;
56+
}
57+
};
58+
}

ReflectionTemplateLib/detail/inc/FunctorContainer.h

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

18-
#include "LambdaRegistry.h"
18+
#include "LambdaCache.h"
1919
#include "Constants.h"
2020
#include "CallReflector.h"
2121
#include "SetupFunction.h"
@@ -40,8 +40,6 @@ namespace rtl {
4040
using FunctionLambda = std::function < Return(_signature...) >;
4141
public:
4242

43-
using lambda_t = detail::lambda_registry<_signature...>;
44-
4543
//every FunctorContainer<...> will have a unique-id.
4644
FORCE_INLINE static std::size_t getContainerId() {
4745
static const std::size_t containerId = generate_unique_id();
@@ -62,14 +60,6 @@ namespace rtl {
6260
"(" + TypeId<_signature...>::toString() + ")");
6361
}
6462

65-
66-
static lambda_t& lambdaCache()
67-
{
68-
static lambda_t lambdaRegistry;
69-
return lambdaRegistry;
70-
}
71-
72-
7363
private:
7464

7565
//vector holding lambdas
@@ -85,25 +75,26 @@ namespace rtl {
8575
pUpdate (lambda updating the already registered functors/ctor/d'tor set)
8676
@return: index of newly added or already existing lambda in vector 'm_functors'.
8777
*/ static std::pair<std::size_t, detail::lambda_hop*> pushBack(const FunctionLambda& pFunctor,
88-
std::function<const std::size_t()> pGetIndex,
89-
std::function<void(const std::size_t&)> pUpdate)
78+
std::function<const std::size_t()> pGetIndex,
79+
std::function<void(const std::size_t&)> pUpdate)
9080
{
9181
//critical section, thread safe.
9282
static std::mutex mtx;
9383
std::lock_guard<std::mutex> lock(mtx);
9484

85+
auto& lamdba_store = lambda_cache<methodQ::None>::get<_signature...>();
9586
std::size_t index = pGetIndex();
87+
9688
if (index == rtl::index_none)
9789
{
98-
index = lambdaCache().get().size();
99-
100-
lambdaCache().push(pFunctor);
90+
index = lamdba_store.get().size();
10191

92+
lamdba_store.push(pFunctor);
10293
getFunctorTable().push_back(pFunctor);
10394

10495
pUpdate(index);
10596
}
106-
return { index, &lambdaCache() };
97+
return { index, &lamdba_store };
10798
}
10899

109100
//friends :)

0 commit comments

Comments
 (0)