Skip to content

Commit 1ab6e15

Browse files
committed
added example to the main Readme
1 parent 515b72c commit 1ab6e15

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

README.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,54 @@ Reflection implementation is based on [DWARF](https://en.wikipedia.org/wiki/DWAR
1111

1212
**Features**:
1313

14-
* 'Deep' functionality for printing, copying, comparing, and freeing memory of complex data structures: [demo link](/doc/demo/README.md#how-to-demo).
14+
* Reflection for all native C types.
15+
* 'Deep' functionality for printing, copying, comparing, and freeing memory of complex data structures.
1516
* Supported on Ubuntu, macOS, Windows (msys2) with gcc or clang
17+
18+
[**Example**](/examples/c_app_simplest/):
19+
20+
```c
21+
#include <stdio.h> // printf
22+
#include <stdlib.h> // free
23+
#include <math.h> // M_PI, M_E
24+
#include "metac/reflect.h"
25+
26+
struct test {
27+
int y;
28+
char c;
29+
double pi;
30+
double e;
31+
short _uninitialized_field;
32+
};
33+
34+
int main(){
35+
// we need to use this construction to wrap variable declaration
36+
// to get its type information
37+
WITH_METAC_DECLLOC(decl_location,
38+
struct test t = {
39+
.y = -10,
40+
.c = 'a',
41+
.pi = M_PI,
42+
.e = M_E,
43+
};
44+
)
45+
metac_value_t *p_val = METAC_VALUE_FROM_DECLLOC(decl_location, t);
46+
47+
char * s;
48+
s = metac_entry_cdecl(metac_value_entry(p_val));
49+
// next will output "struct test t = "
50+
printf("%s = ", s);
51+
free(s);
52+
53+
s = metac_value_string(p_val);
54+
// next will output "{.y = -10, .c = 'a', .pi = 3.141593, .e = 2.718282, ._uninitialized_field = 0,};\n"
55+
printf("%s;\n", s);
56+
free(s);
57+
58+
metac_value_delete(p_val);
59+
60+
return 0;
61+
}
62+
```
63+
64+
To get more details please refer to the [How to](/doc/demo/README.md#how-to-demo) document.

examples/c_app_simplest/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include <stdio.h> // printf
1+
#include <stdio.h> // printf
22
#include <stdlib.h> // free
3-
#include <math.h> // M_PI, M_E
3+
#include <math.h> // M_PI, M_E
44
#include "metac/reflect.h"
55

66
struct test {

0 commit comments

Comments
 (0)