Skip to content

Commit 337eaf2

Browse files
committed
updated readme.md
1 parent c952c4c commit 337eaf2

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

README.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,14 @@ Yes — `rtl::function`’s dispatch is faster than `std::function`.
5151

5252
## A Quick Preview: Reflection That Looks and Feels Like C++
5353

54-
```c++
55-
#include <rtl/rtl.h> // Reflection access interface.
56-
```
5754
Create an instance of `CxxMirror`, passing all type information directly to its constructor — and you’re done!
5855
```c++
5956
auto cxx_mirror = rtl::CxxMirror({
6057
// Register free(C-Style) function -
6158
rtl::type().function("complexToStr").build(complexToStr),
6259
// Register class 'Person' ('record' is general term used for 'struct/class') -
6360
rtl::type().record<Person>("Person").build(), // Registers default/copy ctor as well.
64-
// Register user-defined ctor -
61+
// Register user defined ctor -
6562
rtl::type().member<Person>().constructor<std::string, int>().build(),
6663
// Register methods -
6764
rtl::type().member<Person>().method("setAge").build(&Person::setAge),
@@ -70,12 +67,15 @@ auto cxx_mirror = rtl::CxxMirror({
7067
```
7168
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.
7269

73-
And what better way to do that than a **Singleton**:
70+
And what better way to do that than a **Singleton**: *`(MyReflection.h)`*
7471
```c++
75-
struct cxx { static rtl::CxxMirror& mirror(); };
72+
namespace rtl { class CxxMirror; } // Forward declaration, no includes here!
73+
struct cxx { static rtl::CxxMirror& mirror(); }; // The Singleton.
7674
```
77-
define and register everything in an isolated translation unit.
75+
define and register everything in an isolated translation unit. *`(MyReflection.cpp)`*
7876
```c++
77+
#include <rtl/builder.h> // Reflection builder interface.
78+
7979
rtl::CxxMirror& cxx::mirror() {
8080
static auto cxx_mirror = rtl::CxxMirror({
8181
/* ...register all types here... */
@@ -96,36 +96,44 @@ std::cout << p.getName();
9696
**With reflection:**
9797
9898
```c++
99-
// Look up the class by name
100-
std::optional<rtl::Record> classPerson = cxx::mirror().getRecord("Person");
10199
102-
if (classPerson) // Check has_value() before use.
100+
#include <rtl/access.h> // Reflection access interface.
101+
#include "MyReflection.h"
102+
103+
main() // THESE API'S WORKS BUT DEPRECATED.
103104
{
104-
// Create a stack-allocated instance. Returns- std::pair<rtl::error, rtl::RObject>
105-
auto [err, robj] = classPerson->create<alloc::Stack>("John", 42);
106-
if (err == rtl::error::None) //Construction successful.
105+
// Look up the class by name
106+
std::optional<rtl::Record> classPerson = cxx::mirror().getRecord("Person");
107+
108+
if (classPerson) // Check has_value() before use.
107109
{
108-
// Call setAge(43) on the reflected object
109-
std::optional<rtl::Method> setAge = classPerson->getMethod("setAge");
110-
if (setAge) {
111-
// Binds rtl::RObject & rtl::Method, calls with args.
112-
auto [err, ret] = setAge->bind(robj).call(43); //'setAge' is void ('ret' empty).
113-
if (err == rtl::error::None) { /* Operation succeeded. */ }
114-
}
110+
// Create a stack-allocated instance. Returns- std::pair<rtl::error, rtl::RObject>
111+
auto [err, robj] = classPerson->create<alloc::Stack>("John", 42);
112+
if (err == rtl::error::None) //Construction successful.
113+
{
114+
// Call setAge(43) on the reflected object
115+
std::optional<rtl::Method> setAge = classPerson->getMethod("setAge");
116+
if (setAge) {
117+
// Binds rtl::RObject & rtl::Method, calls with args.
118+
auto [err, ret] = setAge->bind(robj).call(43); //'setAge' is void ('ret' empty).
119+
if (err == rtl::error::None) { /* Operation succeeded. */ }
120+
}
115121
116-
// Call getName(), which returns std::string
117-
std::optional<rtl::Method> getName = classPerson->getMethod("getName");
118-
if (getName) {
119-
//Returns- std::pair<rtl::error, rtl::RObject>
120-
auto [err, ret] = getName->bind(robj).call();
121-
if (err == rtl::error::None && ret.canViewAs<std::string>())
122-
{
123-
std::optional<rtl::view<std::string>> viewStr = ret.view<std::string>();
124-
std::cout << viewStr->get(); // safe. validated above.
122+
// Call getName(), which returns std::string
123+
std::optional<rtl::Method> getName = classPerson->getMethod("getName");
124+
if (getName) {
125+
//Returns- std::pair<rtl::error, rtl::RObject>
126+
auto [err, ret] = getName->bind(robj).call();
127+
if (err == rtl::error::None && ret.canViewAs<std::string>())
128+
{
129+
std::optional<rtl::view<std::string>> viewStr = ret.view<std::string>();
130+
std::cout << viewStr->get(); // safe. validated above.
131+
}
125132
}
126133
}
127134
}
128135
}
136+
129137
```
130138
### `Heap` vs `Stack` Allocation and Lifetime Management
131139

0 commit comments

Comments
 (0)