TeaScript meets reflectcpp: Reflect C++ structs into and back from TeaScript
TeaScript is a modern multi-paradigm scripting language, realized and available as a C++20 Library, which can be embedded in C++ Applications.
The main branch ([https://github.com/Florian-Thake/TeaScript-Cpp-Library](https://github.com/Florian-Thake/TeaScript-Cpp-Library)) contains now a reflection feature as a preview. *(note: The last release 0.16.0 does not contain it yet, use the main branch head.).* The feature is optional and has the [reflectcpp](https://github.com/getml/reflect-cpp) library ad dependency. Instructions are in the example code [reflectcpp\_demo.cpp](https://github.com/Florian-Thake/TeaScript-Cpp-Library/blob/main/demo/reflectcpp_demo.cpp).
With that, you can reflect C++ structs without macros, without registration and without any other prerequisites directly into TeaScript like this
**Example**
Imagine you have the following C++ struct and instance:
// some C++ struct (note the self reference in children)
struct Person
{
std::string first_name;
std::string last_name;
int age{0};
std::vector<Person> children;
};
// create an example instance of the C++ struct.
auto homer = Person{.first_name = "Homer",
.last_name = "Simpson",
.age = 45};
homer.children.emplace_back( Person{"Maggie", "Simpson", 1} );
homer.children.emplace_back( Person{"Bart", "Simpson", 10} );
If you want to use a copy of homer in TeaScript you can reflect it in without macros, registration or other prerequisites like this:
// create the default teascript engine.
teascript::Engine engine;
// import the C++ struct instance into TeaScript.
teascript::reflect::into_teascript( engine, "homer", homer );
// nothing more to do, thats all!
Now, within TeaScript we can use 'homer' *(note: This is TeaScript code, not C++)*:
tuple_print( homer, "homer", 10 ) // prints all (nested) elements with name and value
// access some fields
homer.first_name // "Homer"
homer.age // 45
homer["last_name"] // "Simpson" (alternative way of accessing elements by key; by index and ."key name" is also possible)
homer.children[0].first_name // "Maggie"
homer.children[1].first_name // "Bart"
// NOW modifying it by adding Lisa as a child
_tuple_append( homer.children, _tuple_named_create( ("first_name", "Lisa"), ("last_name", "Simpson"), ("age", 8), ("children", json_make_array() ) ) )
// create a shared reference to Lisa
def lisa @= homer.children[2]
Now we can reflect Lisa back into a new C++ Person struct instance via this code:
// exporting from TeaScript into a new C++ struct instance!
// !!!
Person lisa = teascript::reflect::from_teascript<Person>( engine, "lisa" );
// !!! - Thats all !
Use lisa in C++ freely now. This is possible in **C++20** with the current supported minimum compiler versions VS 2022, g++11 and clang-14.
The C++ structs and its members are imported as TeaScript's Tuples.
You can learn more about them in 2 release blog posts as they got published / extended: Tuple / Named Tuple: [Part I](https://tea-age.solutions/2023/03/10/release-of-teascript-0-10-0/), [Part II](https://tea-age.solutions/2023/12/11/release-of-teascript-0-11-0/)
**Some key features of TeaScript**
* [Json Support](https://tea-age.solutions/2024/09/01/release-of-teascript-0-15-0/#json_support)
* [Coroutine like usage](https://tea-age.solutions/2024/05/15/release-of-teascript-0-14-0/#coroutine_like_execution)
* [Bidirectional interoperability](https://tea-age.solutions/teascript/overview-and-highlights/#bidirectional_interoperability)
* [Web Server / Client](https://tea-age.solutions/2024/09/01/release-of-teascript-0-15-0/#web_client_server_preview)
* [Uniform Definition Syntax](https://tea-age.solutions/teascript/teascript-language-documentation/#uniform_definition_syntax_and_lambdas)
* [Copy Assign VS Shared
Post #24731
18