Skip to content

Commit

Permalink
added example to the main Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
aodinokov committed May 28, 2024
1 parent 515b72c commit 1ab6e15
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h> // printf
#include <stdlib.h> // free
#include <math.h> // 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.
4 changes: 2 additions & 2 deletions examples/c_app_simplest/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stdio.h> // printf
#include <stdio.h> // printf
#include <stdlib.h> // free
#include <math.h> // M_PI, M_E
#include <math.h> // M_PI, M_E
#include "metac/reflect.h"

struct test {
Expand Down

0 comments on commit 1ab6e15

Please sign in to comment.