{"id":13521194,"url":"https://github.com/Ubpa/USRefl","last_synced_at":"2025-03-31T20:30:51.433Z","repository":{"id":41114789,"uuid":"278037868","full_name":"Ubpa/USRefl","owner":"Ubpa","description":"Header-only, tiny (99 lines) and powerful C++20 static reflection library.","archived":false,"fork":false,"pushed_at":"2021-08-07T13:20:25.000Z","size":250,"stargazers_count":670,"open_issues_count":0,"forks_count":75,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-28T21:08:27.299Z","etag":null,"topics":["clang","cpp","cpp20","gcc","morden-cpp","msvc","reflection","static","static-reflection"],"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/Ubpa.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":"2020-07-08T08:47:29.000Z","updated_at":"2025-03-24T12:21:16.000Z","dependencies_parsed_at":"2022-07-13T10:50:33.449Z","dependency_job_id":null,"html_url":"https://github.com/Ubpa/USRefl","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ubpa%2FUSRefl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ubpa%2FUSRefl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ubpa%2FUSRefl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ubpa%2FUSRefl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ubpa","download_url":"https://codeload.github.com/Ubpa/USRefl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246535785,"owners_count":20793322,"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":["clang","cpp","cpp20","gcc","morden-cpp","msvc","reflection","static","static-reflection"],"created_at":"2024-08-01T06:00:30.558Z","updated_at":"2025-03-31T20:30:51.100Z","avatar_url":"https://github.com/Ubpa.png","language":"C++","readme":"```\n\n __    __       _______..______       _______  _______  __      \n|  |  |  |     /       ||   _  \\     |   ____||   ____||  |     \n|  |  |  |    |   (----`|  |_)  |    |  |__   |  |__   |  |     \n|  |  |  |     \\   \\    |      /     |   __|  |   __|  |  |     \n|  `--'  | .----)   |   |  |\\  \\----.|  |____ |  |     |  `----.\n \\______/  |_______/    | _| `._____||_______||__|     |_______|\n                                                                \n\n```\n\n[![repo-size](https://img.shields.io/github/languages/code-size/Ubpa/USRefl?style=flat)](https://github.com/Ubpa/USRefl/archive/master.zip) [![tag](https://img.shields.io/github/v/tag/Ubpa/USRefl)](https://github.com/Ubpa/USRefl/tags) [![license](https://img.shields.io/github/license/Ubpa/USRefl)](LICENSE) [![compiler explorer](https://img.shields.io/badge/compiler_explorer-online-blue)](https://godbolt.org/z/ecKvx3) \n\n⭐ Star us on GitHub — it helps!\n\n# USRefl\n\n\u003e **U**bpa **S**tatic **R**eflection\n\nHeader-only, tiny (99 lines) and powerful C++20 static reflection library.\n\n## Feature\n\n- **header-only**, **tiny (99 lines)** and **powerful** ([USRefl_99.h](include/USRefl_99.h) (MSVC \u0026 GCC), [USRefl_99_clang.h](include/USRefl_99_clang.h) (Clang))\n- **noninvasive** \n- basic\n  - (non-static / static) member variable\n  - (non-static / static) member function\n- **attribute** \n- **enum** \n  - string \u003c-\u003e enumerator\n  - static dispatch\n- **template** \n- **inheritance** \n  - diamond inheritance\n  - iterate bases recursively\n  - virtual inheritance\n- **parser** \n\n## How to use\n\n\u003e run it online : [**compiler explorer**](https://godbolt.org/z/oWM8bf) \n\nSuppose you need to reflect `struct Vec` \n\n```c++\nstruct Vec {\n  float x;\n  float y;\n  float norm() const { return std::sqrt(x*x + y*y); }\n};\n```\n\n### Manual registration\n\n```c++\ntemplate\u003c\u003e\nstruct Ubpa::USRefl::TypeInfo\u003cVec\u003e :\n  TypeInfoBase\u003cVec\u003e\n{\n  static constexpr AttrList attrs = {};\n  static constexpr FieldList fields = {\n    Field {TSTR(\"x\")   , \u0026Type::x   },\n    Field {TSTR(\"y\")   , \u0026Type::y   },\n    Field {TSTR(\"norm\"), \u0026Type::norm},\n  };\n};\n```\n\u003e if you use 99-lines version, you need to add a line\n\u003e\n\u003e ```c++\n\u003e static constexpr std::string_view name = \"...\";\n\u003e ```\n\n## Iterate over members\n\n```c++\nTypeInfo\u003cVec\u003e::fields.ForEach([](const auto\u0026 field) {\n  std::cout \u003c\u003c field.name \u003c\u003c std::endl;\n});\n```\n\n### Set/get variables\n\n```c++\nstd::invoke(TypeInfo\u003cVec\u003e::fields.Find(TSTR(\"y\")).value, v) = 4.f;\nstd::invoke(TypeInfo\u003cVec\u003e::fields.Find(TSTR(\"x\")).value, v) = 3.f;\nstd::cout\n  \u003c\u003c \"x: \"\n  \u003c\u003c std::invoke(TypeInfo\u003cVec\u003e::fields.Find(TSTR(\"x\")).value, v)\n  \u003c\u003c std::endl;\n```\n\n### Invoke Methods\n\n```c++\nstd::cout\n  \u003c\u003c \"norm: \"\n  \u003c\u003c std::invoke(TypeInfo\u003cVec\u003e::fields.Find(TSTR(\"norm\")).value, v)\n  \u003c\u003c std::endl;\n```\n\n### Iterate over variables\n\n```c++\nTypeInfo\u003cVec\u003e::ForEachVarOf(v, [](const auto\u0026 field, const auto\u0026 var) {\n  std::cout \u003c\u003c field.name \u003c\u003c \": \" \u003c\u003c var \u003c\u003c std::endl;\n});\n```\n\n### other example\n\n- 99 line: [USRefl_99.h](include/USRefl_99.h), [test](src/test/06_99/main.cpp), [**online**](https://godbolt.org/z/ecKvx3) (test all examples below)\n- [template](src/test/01_template/main.cpp) \n- [static](src/test/02_static/main.cpp) \n- [func](src/test/03_func/main.cpp) \n- [enum](src/test/04_enum/main.cpp) \n- [inheritance](src/test/05_inheritance/main.cpp) \n- [virtual inheritance](src/test/07_virtual/main.cpp) \n- [attribute name as type](src/test/10_type_attr/main.cpp) \n- AutoRefl : [app](src/AutoRefl), [example](src/test/09_AutoRefl/00_basic) ([Vec.h](src/test/09_AutoRefl/00_basic/Vec.h), Vec_AutoRefl.inl (generated by AutoRefl), [main.cpp](src/test/09_AutoRefl/00_basic/main.cpp))\n\n## Integration\n\nYou can choose one of the following two methods\n\n- ⭐ **method 0**: add the required file\n  - MSVC and GCC: [USRefl_99.h](include/USRefl_99.h) \n  - Clang : [USRefl_99_clang.h](include/USRefl_99_clang.h) \n- ⭐ **method 1**: cmake install,  use `find package(USRefl REQUIRED)` to get imported target `Ubpa::USRefl_core` \n\n## Compiler compatibility\n\n- Clang/LLVM \u003e= 10\n- GCC \u003e= 10\n- MSVC \u003e= 1926 (not fully support virtual inheritance because of [a MSVC bug](https://developercommunity.visualstudio.com/content/problem/1116835/member-pointer-of-a-class-with-a-virtual-base-1.html))\n\n## Licensing\n\nYou can copy and paste the license summary from below.\n\n```\nMIT License\n\nCopyright (c) 2020 Ubpa\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n","funding_links":[],"categories":["反射"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUbpa%2FUSRefl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FUbpa%2FUSRefl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUbpa%2FUSRefl/lists"}