Skip to content

Commit 15800a5

Browse files
committed
new constructor-api, wip.
1 parent 337eaf2 commit 15800a5

File tree

11 files changed

+136
-68
lines changed

11 files changed

+136
-68
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ auto cxx_mirror = rtl::CxxMirror({
6767
```
6868
The `cxx_mirror` object is your gateway to runtime reflection — it lets you query, introspect, and even instantiate types without any compile-time knowledge. It can live anywhere — in any translation unit, quietly resting in a corner of your codebase, remaining dormant until first access. All you need is to expose the `cxx_mirror` wherever reflection is required.
6969

70-
And what better way to do that than a **Singleton**: *`(MyReflection.h)`*
70+
And what better way to do that than a **Singleton**, *`(MyReflection.h)`*
7171
```c++
7272
namespace rtl { class CxxMirror; } // Forward declaration, no includes here!
7373
struct cxx { static rtl::CxxMirror& mirror(); }; // The Singleton.
7474
```
75-
define and register everything in an isolated translation unit. *`(MyReflection.cpp)`*
75+
define and register everything in an isolated translation unit, *`(MyReflection.cpp)`*
7676
```c++
7777
#include <rtl/builder.h> // Reflection builder interface.
7878
@@ -100,7 +100,7 @@ std::cout << p.getName();
100100
#include <rtl/access.h> // Reflection access interface.
101101
#include "MyReflection.h"
102102
103-
main() // THESE API'S WORKS BUT DEPRECATED.
103+
main() // THESE APIs WORKS BUT DEPRECATED.
104104
{
105105
// Look up the class by name
106106
std::optional<rtl::Record> classPerson = cxx::mirror().getRecord("Person");

ReflectionTemplateLib/rtl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Top-level headers in rtl/ (absolute paths)
44
set(LOCAL_HEADERS
55
"${CMAKE_CURRENT_SOURCE_DIR}/access.h"
6+
"${CMAKE_CURRENT_SOURCE_DIR}/builder.h"
67
"${CMAKE_CURRENT_SOURCE_DIR}/rtl_forward_decls.h"
78
"${CMAKE_CURRENT_SOURCE_DIR}/rtl_constants.h"
89
"${CMAKE_CURRENT_SOURCE_DIR}/rtl_errors.h"

ReflectionTemplateLib/rtl/dispatch/function_ptr.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ namespace rtl::dispatch
2828
return (fptr == m_functor);
2929
}
3030

31-
function_ptr(functor_t fptr, traits::uid_t p_record_uid, detail::member member_kind) :m_functor(fptr)
31+
function_ptr(functor_t fptr, traits::uid_t p_record_uid, detail::member member_kind)
32+
: m_functor(fptr)
3233
{
3334
m_record_id = p_record_uid;
3435
m_is_void = std::is_void_v<return_t>;
@@ -45,6 +46,6 @@ namespace rtl::dispatch
4546

4647
private:
4748

48-
functor_t m_functor;
49+
const functor_t m_functor;
4950
};
5051
}

ReflectionTemplateLib/rtl/dispatch/method_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace rtl::dispatch
3030
return (fptr == m_functor);
3131
}
3232

33-
method_ptr(functor_t fptr) :m_functor(fptr)
33+
method_ptr(functor_t fptr): m_functor(fptr)
3434
{
3535
m_member_kind = detail::member::NonConst;
3636

ReflectionTemplateLib/rtl/dispatch/method_ptr_const.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace rtl::dispatch
3030
return (fptr == m_functor);
3131
}
3232

33-
method_ptr(functor_t fptr) :m_functor(fptr)
33+
method_ptr(functor_t fptr): m_functor(fptr)
3434
{
3535
m_member_kind = detail::member::Const;
3636

ReflectionTemplateLib/rtl/erasure/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(LOCAL_HEADERS
66
"${CMAKE_CURRENT_SOURCE_DIR}/erasure_base.h"
77
"${CMAKE_CURRENT_SOURCE_DIR}/erasure_base.hpp"
88
"${CMAKE_CURRENT_SOURCE_DIR}/erase_return.h"
9+
"${CMAKE_CURRENT_SOURCE_DIR}/erase_constructor.h"
910
"${CMAKE_CURRENT_SOURCE_DIR}/erase_return_n_target.h"
1011
"${CMAKE_CURRENT_SOURCE_DIR}/erase_return_aware_target.h"
1112
"${CMAKE_CURRENT_SOURCE_DIR}/erase_target_aware_return.h"

ReflectionTemplateLib/rtl/erasure/aware_constructor.h

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,94 +12,91 @@
1212
#pragma once
1313

1414
#include "rtl_errors.h"
15+
#include "erase_constructor.h"
16+
1517
#include "RObjectBuilder.hpp"
1618

1719
namespace rtl::dispatch
1820
{
19-
template<class record_t>
20-
struct aware_constructor
21+
template<class record_t, class ...signature_t>
22+
struct aware_constructor : public erase_constructor<traits::normal_sign_t<signature_t>...>
2123
{
24+
using this_t = aware_constructor;
25+
using base_t = erase_constructor<traits::normal_sign_t<signature_t>...>;
26+
27+
aware_constructor(): base_t(this_t::get_allocator())
28+
{ }
29+
2230
template<class ...args_t>
23-
static Return allocator(const detail::FunctorId& pFunctorId, alloc pAllocType, const detail::FunctorId& pClonerId, args_t...params)
31+
static Return get_allocator()
2432
{
25-
if constexpr (sizeof...(args_t) == 0 && !std::is_default_constructible_v<record_t>)
26-
{ //default constructor, private or deleted.
27-
return { error::TypeNotDefaultConstructible, RObject{} };
28-
}
29-
else
33+
return [](const detail::FunctorId& pFunctorId, alloc pAllocType, const detail::FunctorId& pClonerId, args_t...params)
3034
{
31-
if (pAllocType == alloc::Stack) {
35+
if constexpr (sizeof...(args_t) == 0 && !std::is_default_constructible_v<record_t>)
36+
{ //default constructor, private or deleted.
37+
return { error::TypeNotDefaultConstructible, RObject{} };
38+
}
39+
else
40+
{
41+
if (pAllocType == alloc::Stack) {
3242

33-
if constexpr (!std::is_copy_constructible_v<record_t>)
34-
{
35-
return {
36-
error::TypeNotCopyConstructible, RObject{}
37-
};
43+
if constexpr (!std::is_copy_constructible_v<record_t>)
44+
{
45+
return { error::TypeNotCopyConstructible, RObject{} };
46+
}
47+
else
48+
{
49+
return {
50+
error::None,
51+
detail::RObjectBuilder<record_t>::template build<alloc::Stack>(
52+
record_t(std::forward<args_t>(params)...), pClonerId, true
53+
)
54+
};
55+
}
3856
}
39-
else
57+
else if (pAllocType == alloc::Heap)
4058
{
4159
return {
4260
error::None,
43-
detail::RObjectBuilder<record_t>::template build<alloc::Stack>(
44-
record_t(std::forward<args_t>(params)...), pClonerId, true
61+
detail::RObjectBuilder<record_t*>::template build<alloc::Heap>(
62+
new record_t(std::forward<args_t>(params)...), pClonerId, true
4563
)
4664
};
4765
}
4866
}
49-
else if (pAllocType == alloc::Heap)
50-
{
51-
return {
52-
error::None,
53-
detail::RObjectBuilder<record_t*>::template build<alloc::Heap>(
54-
new record_t(std::forward<args_t>(params)...), pClonerId, true
55-
)
56-
};
57-
}
58-
}
59-
return { error::EmptyRObject, RObject{} }; //dead code. compiler warning omitted.
67+
return { error::EmptyRObject, RObject{} }; //dead code. compiler warning omitted.
68+
};
6069
}
6170

6271

6372
static Return cloner(const detail::FunctorId& pFunctorId, const RObject& pOther, alloc pAllocOn)
6473
{
6574
if constexpr (std::is_copy_constructible_v<record_t>)
6675
{
67-
return [](const detail::FunctorId& pFunctorId, const RObject& pOther, alloc pAllocOn) -> Return
76+
const auto& srcObj = pOther.view<record_t>()->get();
77+
switch (pAllocOn)
6878
{
69-
const auto& srcObj = pOther.view<record_t>()->get();
70-
switch (pAllocOn)
71-
{
72-
case alloc::Stack:
73-
return {
74-
error::None,
75-
detail::RObjectBuilder<record_t>::template build<alloc::Stack>(
76-
record_t(srcObj), pFunctorId, true
77-
)
78-
};
79-
case alloc::Heap:
80-
return {
81-
error::None,
82-
detail::RObjectBuilder<record_t*>::template build<alloc::Heap>(
83-
new record_t(srcObj), pFunctorId, true
84-
)
85-
};
86-
default:
87-
return {
88-
error::EmptyRObject,
89-
RObject{}
90-
};
91-
}
92-
};
79+
case alloc::Stack:
80+
return {
81+
error::None,
82+
detail::RObjectBuilder<record_t>::template build<alloc::Stack>(
83+
record_t(srcObj), pFunctorId, true
84+
)
85+
};
86+
case alloc::Heap:
87+
return {
88+
error::None,
89+
detail::RObjectBuilder<record_t*>::template build<alloc::Heap>(
90+
new record_t(srcObj), pFunctorId, true
91+
)
92+
};
93+
default:
94+
return { error::EmptyRObject, RObject{} };
95+
}
9396
}
9497
else
9598
{
96-
return [](const detail::FunctorId& pFunctorId, const RObject& pOther, alloc pAllocOn) -> Return
97-
{
98-
return {
99-
error::TypeNotCopyConstructible,
100-
RObject{}
101-
};
102-
};
99+
return { error::TypeNotCopyConstructible, RObject{} };
103100
}
104101
}
105102
};
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 <[email protected]> *
7+
* SPDX-License-Identifier: MIT *
8+
* *
9+
*************************************************************************/
10+
11+
12+
#pragma once
13+
14+
#include <functional>
15+
16+
#include "erasure_base.h"
17+
18+
namespace rtl::dispatch
19+
{
20+
template<class ...normal_sign_t>
21+
struct erase_constructor : public erasure_base
22+
{
23+
using lambda_t = std::function<Return(const detail::FunctorId& pFunctorId, alloc pAllocType, const detail::FunctorId& pClonerId, normal_sign_t...)>;
24+
25+
GETTER_CREF(lambda_t, _hopper, m_hopper)
26+
27+
protected:
28+
29+
lambda_t m_hopper;
30+
31+
erase_constructor(const lambda_t& p_hop) noexcept
32+
: m_hopper(p_hop)
33+
{ }
34+
};
35+
}

ReflectionTemplateLib/rtl/inc/type_meta.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ namespace rtl
5757
template<class record_t, class return_t, class ...signature_t>
5858
static type_meta add_method(return_t(record_t::* p_fptr)(signature_t...) const, std::size_t p_index);
5959

60+
template<class record_t, class ...signature_t>
61+
static type_meta add_ctor();
62+
63+
template<class record_t, class ...signature_t>
64+
static type_meta add_copy_ctor();
65+
6066
template<class ..._signature>
6167
using lambda_fn_t = dispatch::lambda_function<_signature...>;
6268

ReflectionTemplateLib/rtl/inc/type_meta.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "cache_lambda_method.h"
2424
#include "cache_lambda_function.h"
2525

26+
#include "aware_constructor.h"
27+
2628
namespace rtl
2729
{
2830
template<class ...args_t>
@@ -81,4 +83,29 @@ namespace rtl
8183

8284
return type_meta(functor);
8385
}
86+
87+
88+
template<class record_t, class ...signature_t>
89+
inline type_meta type_meta::add_ctor()
90+
{
91+
//auto& fc = cache::function_ptr<Return, signature_t...>::instance();
92+
//auto& lc = cache::lambda_function<Return, signature_t...>::instance();
93+
94+
//auto fptr = &(dispatch::aware_constructor<record_t>::allocator<signature_t...>);
95+
96+
//auto& functor = fc.push(fptr, p_record_uid, p_member_kind, p_index);
97+
//auto [lambda, elambda] = lc.push(functor);
98+
99+
//functor.set_lambda(lambda);
100+
//functor.set_erasure(elambda);
101+
102+
return type_meta();
103+
}
104+
105+
106+
template<class record_t, class ...signature_t>
107+
inline type_meta type_meta::add_copy_ctor()
108+
{
109+
return type_meta();
110+
}
84111
}

0 commit comments

Comments
 (0)