Skip to content

Commit a8049ce

Browse files
committed
organizing metadata: added functor_cast.
1 parent 87233b5 commit a8049ce

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

ReflectionTemplateLib/rtl/dispatch/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
set(LOCAL_HEADERS
55

66
"${CMAKE_CURRENT_SOURCE_DIR}/functor.h"
7+
"${CMAKE_CURRENT_SOURCE_DIR}/functor_cast.h"
78

89
"${CMAKE_CURRENT_SOURCE_DIR}/method_ptr.h"
910
"${CMAKE_CURRENT_SOURCE_DIR}/method_ptr.hpp"

ReflectionTemplateLib/rtl/dispatch/functor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ namespace rtl::dispatch
6666

6767
friend dispatch::lambda_base;
6868

69+
template<fn_void, class...>
70+
friend struct functor_cast;
71+
6972
template<class ...>
7073
friend struct dispatch::lambda_function;
7174

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 "functor.h"
15+
16+
namespace rtl::dispatch
17+
{
18+
template<fn_void fn_void_v, class...signature_t>
19+
struct functor_cast
20+
{
21+
const functor& m_functor;
22+
23+
template<erase erase_v>
24+
constexpr auto to_method()
25+
{
26+
if constexpr (erase_v == erase::t_method)
27+
{
28+
using lambda_t = function_lambda<fn_void_v, erase::t_method, signature_t...>;
29+
return static_cast<lambda_t&>(*m_functor.m_lambdas[functor::index::erased_method]);
30+
}
31+
}
32+
33+
template<erase erase_v, class known_t>
34+
constexpr auto to_method()
35+
{
36+
if constexpr (erase_v == erase::t_return)
37+
{
38+
using target_t = known_t;
39+
using lambda_t = method_lambda<fn_void_v, erase::t_ctor, target_t, signature_t...>;
40+
return static_cast<lambda_t&>(*m_functor.m_lambdas[functor::index::erased_return]);
41+
}
42+
else if constexpr (erase_v == erase::t_target)
43+
{
44+
using return_t = known_t;
45+
using lambda_t = method_lambda<fn_void_v, erase::t_return, return_t, signature_t...>;
46+
return static_cast<lambda_t&>(*m_functor.m_lambdas[functor::index::erased_target]);
47+
}
48+
}
49+
50+
template<erase erase_v>
51+
constexpr auto to_function()
52+
{
53+
if constexpr (erase_v == erase::t_ctor)
54+
{
55+
using lambda_t = function_lambda<fn_void_v, erase::t_ctor, signature_t...>;
56+
return static_cast<lambda_t&>(*m_functor.m_lambdas[functor::index::erased_ctor]);
57+
}
58+
else if constexpr (erase_v == erase::t_return)
59+
{
60+
using lambda_t = function_lambda<fn_void_v, erase::t_return, signature_t...>;
61+
return static_cast<lambda_t&>(*m_functor.m_lambdas[functor::index::erased_return]);
62+
}
63+
}
64+
};
65+
}

ReflectionTemplateLib/rtl/rtl_forward_decls.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,20 @@ namespace rtl
115115
yes
116116
};
117117

118+
template<fn_void, class...signature_t>
119+
struct functor_cast;
120+
118121
enum class erase {
119122
t_ctor,
120123
t_target,
121124
t_return,
122125
t_method
123126
};
124127

125-
template<fn_void, erase, class...args_t>
128+
template<fn_void, erase, class...signature_t>
126129
struct function_lambda;
127130

128-
template<fn_void, erase, class known_t, class...args_t>
131+
template<fn_void, erase, class known_t, class...signature_t>
129132
struct method_lambda;
130133

131134
template<class return_t, class ...signature_t>

0 commit comments

Comments
 (0)