{"id":23719475,"url":"https://github.com/lcsmuller/reflect-c","last_synced_at":"2025-07-26T13:35:03.736Z","repository":{"id":107948995,"uuid":"579651992","full_name":"lcsmuller/reflect-c","owner":"lcsmuller","description":"A C89 library that adds reflection capabilities to C structs through code generation","archived":false,"fork":false,"pushed_at":"2024-12-28T23:11:02.000Z","size":81,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-29T00:18:10.250Z","etag":null,"topics":["ansi-c","c","c-preprocessor","c89","code-gen","code-generation","code-generator","preprocessor","reflection","reflection-library","rest-api"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lcsmuller.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-18T12:26:45.000Z","updated_at":"2024-12-28T23:19:27.000Z","dependencies_parsed_at":"2024-12-29T00:18:11.965Z","dependency_job_id":null,"html_url":"https://github.com/lcsmuller/reflect-c","commit_stats":null,"previous_names":["lcsmuller/cog-chef","lcsmuller/reflect-c"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Freflect-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Freflect-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Freflect-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcsmuller%2Freflect-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lcsmuller","download_url":"https://codeload.github.com/lcsmuller/reflect-c/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231918552,"owners_count":18445746,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansi-c","c","c-preprocessor","c89","code-gen","code-generation","code-generator","preprocessor","reflection","reflection-library","rest-api"],"created_at":"2024-12-30T21:52:13.585Z","updated_at":"2025-07-26T13:35:03.702Z","avatar_url":"https://github.com/lcsmuller.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reflect-C\n\nA library that adds reflection capabilities to C structs through code generation.\n\n## Features\n- Zero runtime overhead with metadata generated at compile time\n- Struct field introspection and manipulation\n- Support for nested structs, unions, and enums\n- Pointer handling with automatic dereferencing\n- Field type validation and safety\n- Support for field decorators (qualifiers, pointers, arrays)\n- Compatible with C89/ANSI C\n- No external dependencies\n- Easily integrate with JSON serialization/deserialization\n\n## Installation\n\nClone the repository:\n\n```bash\ngit clone https://github.com/lcsmuller/reflect-c.git\ncd reflect-c\n```\n\nThe library is header-only, so you can simply include it in your project.\n\n## Basic Usage\n\n1. Define your structures with reflection capabilities using the `PUBLIC` or `PRIVATE` macros:\n\n```c\n// my_types.PRE.h\n#ifdef REFLECTC_DEFINITIONS\n/*#!\n#include \u003cstdbool.h\u003e\n*/\n#endif\n\nPUBLIC(struct, person, 4, (\n    (_, _, char, *, name, _),\n    (_, _, int, _, age, _),\n    (_, _, bool, _, active, _),\n    (_, _, char, *, email, _)\n))\n```\n\n2. Generate the reflection code:\n\n```bash\n# Using your preferred method to run the code generator\n./reflect-c-gen my_types.PRE.h\n```\n\n3. Include the generated headers in your code:\n\n```c\n#include \"reflect-c.h\"\n#include \"reflect-c_GENERATED.h\"\n\nint main() {\n    struct person p = {\"John Doe\", 30, true, \"john@example.com\"};\n\n    // Create a reflection wrapper for the person struct\n    struct reflectc *r_person = reflectc_from_person(\u0026p, NULL);\n\n    // Access fields statically (fast access)\n    char *name_field = (char *)reflectc_get_fast(person, name, r_person);\n    printf(\"Name: %s\\n\", name_field);\n    // Access fields dynamically (using string names)\n    char *email_field = (char *)reflectc_get(r_person, \"email\", strlen(\"email\"));\n    printf(\"Email: %s\\n\", name_field);\n\n    // Clean up\n    reflectc_free(r_person);\n    return 0;\n}\n```\n\n## Example: JSON Serialization\n\nThe library can be easily used for JSON serialization, as demonstrated in the test code:\n\n```c\n#include \u003cstdio.h\u003e\n#include \u003cstdbool.h\u003e\n#include \"json-build/json-build.h\"\n#include \"reflect-c.h\"\n#include \"reflect-c_GENERATED.h\"\n\nvoid json_stringify(struct jsonb *jb, const struct reflectc *field,\n                   char buf[], const size_t bufsize)\n{\n    if (field-\u003edecorator.len \u0026\u0026 field-\u003eptr_value == NULL) {\n        jsonb_null(jb, buf, bufsize);\n        return;\n    }\n\n    switch (field-\u003etype) {\n    case REFLECTC_TYPES__char:\n        jsonb_string(jb, buf, bufsize, field-\u003eptr_value, strlen(field-\u003eptr_value));\n        break;\n    case REFLECTC_TYPES__int:\n        jsonb_number(jb, buf, bufsize, *(int *)field-\u003eptr_value);\n        break;\n    case REFLECTC_TYPES__bool:\n        jsonb_bool(jb, buf, bufsize, *(bool *)field-\u003eptr_value);\n        break;\n    case REFLECTC_TYPES__float:\n        jsonb_number(jb, buf, bufsize, *(float *)field-\u003eptr_value);\n        break;\n    case REFLECTC_TYPES__struct: {\n        jsonb_object(jb, buf, bufsize);\n        for (size_t i = 0; i \u003c field-\u003efields.len; ++i) {\n            const struct reflectc *f = \u0026field-\u003efields.array[i];\n            jsonb_key(jb, buf, bufsize, f-\u003ename.buf, f-\u003ename.len);\n            json_stringify(jb, f, buf, bufsize);\n        }\n        jsonb_object_pop(jb, buf, bufsize);\n    } break;\n    default:\n        break;\n    }\n}\n\nint main() {\n    // Create a sample struct\n    struct bar a = { true, 42, \"hello world\" }, *aa = \u0026a, **aaa = \u0026aa;\n    struct baz baz = { \u0026a, \u0026a, \u0026aaa, \"hello world\" };\n\n    // Create a reflection wrapper\n    struct reflectc *wrapped_baz = reflectc_from_baz(\u0026baz, NULL);\n\n    // Serialize to JSON\n    char json[1024] = {0};\n    struct jsonb jb;\n    jsonb_init(\u0026jb);\n    json_stringify(\u0026jb, wrapped_baz, json, sizeof(json));\n\n    printf(\"JSON: %s\\n\", json);\n    // Output: {\"a\":{\"boolean\":true,\"number\":42,\"string\":\"hello world\"},...}\n\n    // Clean up\n    reflectc_free(wrapped_baz);\n    return 0;\n}\n```\n\n## API Reference\n\n### Core Macros\n\n- `PUBLIC(container_type, name, field_count, fields_tuple)`: Defines a public struct/enum/union with reflection\n- `PRIVATE(container_type, name, field_count, fields_tuple)`: Defines a private struct/enum/union with reflection\n\n### Field Definition\n\nFields are defined as tuples with the following format:\n- For struct/union fields: `(qualifier, container, type, decorator, name, dimensions)`\n- For enum fields: `(enumerator, assignment_op, value)`\n\n### Generated Functions\n\nFor each reflected type `foo`, these functions are generated:\n- `reflectc_from_foo(foo *self, struct reflectc *root)`: Creates reflection data for a foo instance\n\n### Core Types\n\n- `struct reflectc`: Holds reflection metadata for a struct field\n  - `size_t size`: Size of the type in bytes\n  - `struct { const char *buf; size_t len } qualifier`: Field qualifier\n  - `struct { const char *buf; size_t len } decorator`: Field decorator (* for pointers)\n  - `struct { const char *buf; size_t len } name`: Field name\n  - `struct { const char *buf; size_t len } dimensions`: Field dimensions (for arrays)\n  - `enum reflectc_types type`: Field type\n  - `void *ptr_value`: Pointer to the actual value\n  - `struct { struct reflectc *array; size_t len } fields`: Nested fields (for structs/unions)\n  - `reflectc_from_cb from_cb`: Callback for generating child reflectc objects\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flcsmuller%2Freflect-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flcsmuller%2Freflect-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flcsmuller%2Freflect-c/lists"}