{"id":16811944,"url":"https://github.com/chakaz/reflang","last_synced_at":"2025-04-07T06:06:59.984Z","repository":{"id":42680917,"uuid":"77028012","full_name":"chakaz/reflang","owner":"chakaz","description":"Reflang - Modern C++ reflection using libclang","archived":false,"fork":false,"pushed_at":"2021-07-26T06:48:07.000Z","size":321,"stargazers_count":371,"open_issues_count":2,"forks_count":38,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-31T04:07:25.839Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/chakaz.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}},"created_at":"2016-12-21T07:18:04.000Z","updated_at":"2025-03-27T06:31:39.000Z","dependencies_parsed_at":"2022-09-02T22:11:39.467Z","dependency_job_id":null,"html_url":"https://github.com/chakaz/reflang","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakaz%2Freflang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakaz%2Freflang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakaz%2Freflang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chakaz%2Freflang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chakaz","download_url":"https://codeload.github.com/chakaz/reflang/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601447,"owners_count":20964864,"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":[],"created_at":"2024-10-13T10:20:07.549Z","updated_at":"2025-04-07T06:06:59.969Z","avatar_url":"https://github.com/chakaz.png","language":"C++","funding_links":[],"categories":["[Libraries](#contents)"],"sub_categories":["ETC"],"readme":"# Reflang - Modern C++ reflection using libclang\n\n[![Build Status](https://travis-ci.org/chakaz/reflang.svg?branch=master)](https://travis-ci.org/chakaz/reflang)\n\n## TL;DR\n**Reflang is a reflection library \u0026 tool for modern C++.**\n\nThe tool parses your C++ code using libclang, then generates *human-readable*\ncode around it to enable reflection.\n\nThe library is extremely lightweight, containing utility interfaces, classes \u0026\nfunctions.\n\nReflang's library has no dependencies other than C++14 with STL. Reflang's\ntool's only other dependency is libclang (which is not required after code\ngeneration).\n\n## More details\n\n### Functions\nSuppose you have the following function:\n\n```cpp\n\tbool Func(int a, float b);\n```\n\nWith Reflang you could do:\n\n```cpp\n\t// Note: no need to include header of Func().\n\tstd::vector\u003cIFunction*\u003e functions = reflang::registry::GetFunctionByName(\"Func\");\n\tIFunction* f = functions[0];\n\tObject ret = (*f)(1234, 5678.0f);\n\tif (ret.GetT\u003cbool\u003e())\n\t{\n\t\t// ...\n\t}\n```\n\n### Classes\nSuppose you have the following `class`:\n\n```cpp\n\tclass MyClass\n\t{\n\tpublic:\n\t\tint field = 0;\n\t\tstatic int static_field;\n\t\tvoid method();\n\t\tstatic void static_method();\n\t};\n```\n\nWith Reflang you could do:\n\n```cpp\n\treflang::Class\u003cMyClass\u003e metadata;\n\tMyClass c;\n\n\t// Modify / use c's 'field'.\n\treflang::Reference ref = metadata.GetField(c, \"field\");\n\tref.GetT\u003cint\u003e() = 10;\n\n\t// Modify / use 'static_field'.\n\tref = metadata.GetStaticField(\"static_field\")\n\tref.GetT\u003cint\u003e() = 10;\n\n\t// Execute 'method()'.\n\tauto methods = metadata.GetMethod(\"method\");\n\t(*methods[0])(c);\n\n\t// Execute 'static_method()'.\n\tauto methods = metadata.GetStaticMethod(\"static_method\");\n\t(*methods[0])();\n```\n\nOr even without `#include`ing `MyClass`:\n\n```cpp\n\t// Note: no need to include header of MyClass.\n\tIClass* metadata = reflang::registry::GetClassByName(\"MyClass\");\n\n\t// Assign 10 to MyClass::static_field.\n\tmetadata-\u003eGetStaticField(\"static_field\").GetT\u003cint\u003e() = 10;\n\n\t// Invoke MyClass::static_method().\n\tauto static_methods = metadata-\u003eGetStaticMethod(\"static_method\");\n\t(*static_methods[0])();\n\n\treflang::Reference c = // get Reference wrapping a MyClass from somewhere.\n\n\t// Assign 10 to c.field.\n\tmetadata-\u003eGetField(c, \"field\").GetT\u003cint\u003e() = 10;\n\n\t// Invoke c.method().\n\tauto methods = metadata-\u003eGetMethod(\"method\");\n\t(*methods[0])(c);\n```\n\n### Enums\nSuppose you have the following `enum`:\n\n```cpp\n\tenum class MyEnum\n\t{\n\t\tValue1 = 4,\n\t\tValue2,\n\t\tValue3,\n\t\tValue4 = Value3,\n\t\tValue5 = 0,\n\t\tValue6 = 12\n\t};\n```\n\nWith Reflang you could do:\n\n```cpp\n\tfor (auto it : reflang::Enum\u003cMyEnum\u003e::Iterate())\n\t{\n\t\t// Do something with MyEnum values...\n\t}\n\n\tstd::string string_value = reflang::Enum\u003cMyEnum\u003e::Translate(MyEnum::Value2);\n\t// string_value == \"Value2\".\n\n\tMyEnum e\n\tif (reflang::Enum\u003cMyEnum\u003e::TryTranslate(string_value, e))\n\t{\n\t\t// e == MyEnum::Value2.\n\t}\n```\n\nOr even without `#include`ing `MyEnum`:\n\n```cpp\n\t// Note: no need to include header of MyEnum.\n\tIEnum* e = reglang::registry::GetEnumByName(\"MyEnum\");\n\n\t// Iterate over {\"Value1\", \"Value2\", \"Value3\", \"Value5\", \"Value6\"}.\n\tfor (const auto\u0026 s : e-\u003eGetStringValues())\n\t{\n\t\t// ...\n\t}\n\n\t// Iterate over {4, 5, 6, 0, 12}.\n\tfor (const auto\u0026 s : e-\u003eGetStringValues())\n\t{\n\t\t// ...\n\t}\n```\n\n## How it works\nReflang is made of 2 components:\n* **Code generator**, which uses libclang to parse your C++ code and generate\n  reflection information. You can easily integrate this tool in your build\n  process;\n* **Support library**, which allows generic and abstract coding (\"find class by\n  name\", \"invoke function by name\", etc).\n\nReflang understands your code exactly like clang does, so which results in\naccurate parsing of your code.\n\n## End-to-end Examples\nCheck out the [tests/](tests/) directory, which shows pretty much all features\nsupported by Reflang.\n\nEach test has 4 files:\n* `X.src.hpp` -- source code which will be used for reflection;\n* `X.gen.hpp` -- generated reflection code declarations for `X.src.hpp`;\n* `X.gen.cpp` -- generated reflection code definitions for `X.src.hpp`;\n* `X.test.cpp` -- test code which tests the above.\n\n## Using\n*This section is incomplete. Please check later for more elaborated\ndocumentation.*\n\nRough instructions:\n* Clone the repository;\n* Build it using CMake;\n* A new executable, *reflang* is now available - use it to generate code;\n* Link your program with the generated code and with *libreflang*, which has\n  been built by CMake as well;\n* Profit.\n\nExamples of running the tool:\n\n```bash\n# print help\n$ ./reflang\n\n# generate code for test.hpp, write both hpp and cpp to stdout\n$ ./reflang test.hpp\n\n# specify output files\n$ ./reflang --out-cpp test.reflang.cpp --out-hpp test.reflang.hpp test.hpp\n\n# only list classes, functions, etc available for generation\n$ ./reflang --list-only true test.hpp\n\n# only generate code for classes, functions, etc not in std::\n$ ./reflang --exclude \"std::.*\" test.hpp\n\n# only generate code for classes, functions, etc beginning with My\n$ ./reflang --include \"My.*\" test.hpp\n\n# pass flags supported by libclang\n$ ./reflang test.hpp -- -std=c++14 -DSOMETHING -I./include/\n```\n\n## Supported Features \u0026 Limitations\n\u0026nbsp;\t| Feature\n--------|-----------------------------------------------\n✓\t\t| *Supported*: Invoking global / namespace functions\n✓\t\t| *Supported*: Function overloads\n✓\t\t| *Supported*: Iterating classes' fields\n✓\t\t| *Supported*: Iterating classes' static fields\n✓\t\t| *Supported*: Iterating classes' methods\n✓\t\t| *Supported*: Iterating classes' static methods\n✓\t\t| *Supported*: Getting a class field / method / statics by name\n✓\t\t| *Supported*: Iterating enum values\n✓\t\t| *Supported*: Get vector of enum values\n✓\t\t| *Supported*: Get vector of enum string names\n✓\t\t| *Supported*: Converting enum \u003c-\u003e string\n✓\t\t| *Supported*: Use `class` / `function` / `enum` without `#include` (through registry)\n⌛\t\t| *Soon*: Get function's / method's argument names \u0026 types\n⌛\t\t| *Soon*: Get function's / method's return type\n⌛\t\t| *Soon*: Overloaded class methods\n⌛\t\t| *Soon*: Construct classes through reflection\n⌛\t\t| *Soon*: Find types by regex\n⌛\t\t| *Soon*: Respect functions / methods default values\n⌛\t\t| *Soon*: Support for operators\n✕\t\t| *Not supported*: `template` classes / functions\n✕\t\t| *Not supported*: Functions taking non-const reference arguments (yet?)\n✕\t\t| *Not supported*: Functions taking rvalue references arguments\n✕\t\t| *Not supported*: Private methods / fields\n✕\t\t| *Not supported*: Anything in anonymous namespace\n\n## Bugs, pull requests, feedback\nPlease check this section later.\n\n## Kind Words \u0026 Donations\nPlease check this section later.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchakaz%2Freflang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchakaz%2Freflang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchakaz%2Freflang/lists"}