From 1ab6e15b4560d08813c030f765d124390b18f101 Mon Sep 17 00:00:00 2001 From: Alexey Odinokov Date: Mon, 27 May 2024 20:57:30 -0500 Subject: [PATCH] added example to the main Readme --- README.md | 51 +++++++++++++++++++++++++++++++++- examples/c_app_simplest/main.c | 4 +-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 896b30d..f23c219 100644 --- a/README.md +++ b/README.md @@ -11,5 +11,54 @@ Reflection implementation is based on [DWARF](https://en.wikipedia.org/wiki/DWAR **Features**: -* 'Deep' functionality for printing, copying, comparing, and freeing memory of complex data structures: [demo link](/doc/demo/README.md#how-to-demo). +* Reflection for all native C types. +* 'Deep' functionality for printing, copying, comparing, and freeing memory of complex data structures. * Supported on Ubuntu, macOS, Windows (msys2) with gcc or clang + +[**Example**](/examples/c_app_simplest/): + +```c +#include // printf +#include // free +#include // M_PI, M_E +#include "metac/reflect.h" + +struct test { + int y; + char c; + double pi; + double e; + short _uninitialized_field; +}; + +int main(){ + // we need to use this construction to wrap variable declaration + // to get its type information + WITH_METAC_DECLLOC(decl_location, + struct test t = { + .y = -10, + .c = 'a', + .pi = M_PI, + .e = M_E, + }; + ) + metac_value_t *p_val = METAC_VALUE_FROM_DECLLOC(decl_location, t); + + char * s; + s = metac_entry_cdecl(metac_value_entry(p_val)); + // next will output "struct test t = " + printf("%s = ", s); + free(s); + + s = metac_value_string(p_val); + // next will output "{.y = -10, .c = 'a', .pi = 3.141593, .e = 2.718282, ._uninitialized_field = 0,};\n" + printf("%s;\n", s); + free(s); + + metac_value_delete(p_val); + + return 0; +} +``` + +To get more details please refer to the [How to](/doc/demo/README.md#how-to-demo) document. \ No newline at end of file diff --git a/examples/c_app_simplest/main.c b/examples/c_app_simplest/main.c index a74e465..00ba861 100644 --- a/examples/c_app_simplest/main.c +++ b/examples/c_app_simplest/main.c @@ -1,6 +1,6 @@ -#include // printf +#include // printf #include // free -#include // M_PI, M_E +#include // M_PI, M_E #include "metac/reflect.h" struct test {