Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oktonion/cppmeta
one header tiny C++ type meta-information library for reflection and serialization
https://github.com/oktonion/cppmeta
cpp03 cpp98 header-only library metainformation reflection serialization single-header
Last synced: 2 days ago
JSON representation
one header tiny C++ type meta-information library for reflection and serialization
- Host: GitHub
- URL: https://github.com/oktonion/cppmeta
- Owner: oktonion
- License: mit
- Created: 2022-06-20T06:40:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-14T07:37:37.000Z (3 months ago)
- Last Synced: 2024-06-14T08:50:50.444Z (3 months ago)
- Topics: cpp03, cpp98, header-only, library, metainformation, reflection, serialization, single-header
- Language: C++
- Homepage:
- Size: 204 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cppmeta
one header tiny C++ type meta-information library for reflection and serialization for classes, functions, types- single header
- no external dependencies except for small set of C++98 std library headers
- terms used for registering and retrieving meta-information are standard complient----------
Usage examples
----------## Compile-time reflection
### Manual registration
```cpp
#includestruct type_for_ct_reflection
{
type_for_ct_reflection() {}
type_for_ct_reflection(int, float) {}
type_for_ct_reflection(int, int) {}int data1;
void func1(float) {}
template
void templ_func1(T) {}
};template<>
struct cppmeta::reflect
{
template
void operator()() const
{
cppmeta::reflect::
name = "type_for_ct_reflection",
members =
member("type_for_ct_reflection()", &class_::default_constructor),
member("type_for_ct_reflection(int, float)", &class_::constructor),
member("constructor2", &class_::constructor),
member("~type_for_ct_reflection", &class_::destructor),
member("data1", &type_for_ct_reflection::data1),
member("func1", &type_for_ct_reflection::func1),
member("templ_func1(int)", &type_for_ct_reflection::templ_func1)
;
}
};int some_data_ct;
template<>
struct cppmeta::reflect_ct
{
template
void operator()() const
{
meta_info::
objects +=
object("some_data_ct", &some_data_ct);
;
}
};
```### Class member pointer resolve
```cpp
using namespace cppmeta;
int type_for_ct_reflection::* data1_tmp =
resolve::member("data1").value;
type_for_ct_reflection obj;
obj.data1 == obj.*data1_tmp; // true
```## Runtime reflection
### Manual registration
```cpp
#includestruct type_for_rt_reflection
{
type_for_rt_reflection() {}
type_for_rt_reflection(int, float) {}
type_for_rt_reflection(int, int) {}int data1;
void func1(float) {}
template
void templ_func1(T) {}
};struct type_for_rt_reflection_child
: type_for_rt_reflection
{
int data2;
};int some_data_rt = 0;
const int some_data_rt_const = 0;enum enum_for_rt_reflection
{
rt_1,
rt_2
};void MyCode()
{
using namespace cppmeta;reflect::
name = "type_for_rt_reflection",
members =
member("type_for_rt_reflection()", &class_::default_constructor)
,member("data1", &type_for_rt_reflection::data1)
;reflect::
name = "type_for_rt_reflection_child",
members =
member("type_for_rt_reflection_child()", &class_::default_constructor)
, member("data2", &type_for_rt_reflection_child::data2)
, reflect::members // add parent members to child (optional)
;reflect::
objects +=
object("some_data_rt", &some_data_rt)
, object("some_data_rt_ref", some_data_rt)
, object("some_data_rt_const", some_data_rt_const)
,values +=
constant("max_int", 2048)
;reflect::
name = "enum_for_rt_reflection",
values =
constant("rt_1", rt_1),
constant("rt_2", rt_2)
;
}```
### Class member pointer resolve
```cpp
int type_for_rt_reflection::* data1_tmp =
resolve::member("data1").value;type_for_rt_reflection obj;
obj.data1 == obj.*data1_tmp; // trueint type_for_rt_reflection_child::* child_data1_tmp =
resolve::member("data1").value;type_for_rt_reflection_child obj_child;
obj_child.data1 == obj_child.*child_data1_tmp; // true
```### Object by value resolve
```cpp
int some_data_rt_resolved =
resolve::object("some_data_rt").value;
const int& some_data_rt_resolved =
resolve::object("some_data_rt_const").value;
```### Object by const value resolve
```cpp
const int& some_data_rt_const_resolved =
resolve::object("some_data_rt").value;
const int& some_data_rt_const_resolved =
resolve::object("some_data_rt_const").value;
```
### Object by reference resolve
```cpp
int& some_data_rt_ref_resolved =
resolve::object("some_data_rt").value;
some_data_rt_ref_resolved = 42; // changes `some_data_rt`
const int& some_data_rt_ref_const_resolved =
resolve::object("some_data_rt_const").value;
```### Object by pointer resolve
```cpp
int* some_data_rt_ptr_resolved =
resolve::object("some_data_rt").value;
(*some_data_rt_ref_resolved) = 42; // changes `some_data_rt`
const int* some_data_rt_ptr_const_resolved =
resolve::object("some_data_rt_const").value;
```### Const value resolve
```cpp
const int& max_int_const =
resolve::constant("max_int").value;
```### Enum values resolve
```cpp
const enum_for_rt_reflection& rt_1_const =
resolve::value("rt_1").value;
```### Invoke functions
```cpp
// in progress
```