{"id":13521237,"url":"https://github.com/16bit-ykiko/magic-cpp","last_synced_at":"2025-04-06T11:08:43.902Z","repository":{"id":212660449,"uuid":"732015562","full_name":"16bit-ykiko/magic-cpp","owner":"16bit-ykiko","description":"A C++20 header-only library that supports powerful reflection for C++","archived":false,"fork":false,"pushed_at":"2024-06-22T05:34:34.000Z","size":5050,"stargazers_count":291,"open_issues_count":4,"forks_count":17,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T09:08:30.058Z","etag":null,"topics":["c-plus-plus","c-plus-plus-20","cplusplus","cplusplus-20","cpp","cpp20","easy-to-use","enum","field","header-only","no-dependencies","powerful","reflection","struct","typename"],"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/16bit-ykiko.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":"2023-12-15T12:32:12.000Z","updated_at":"2025-03-23T09:02:14.000Z","dependencies_parsed_at":"2024-05-16T12:52:03.115Z","dependency_job_id":"ee7e6bfc-037e-471d-a75e-6563185dbed6","html_url":"https://github.com/16bit-ykiko/magic-cpp","commit_stats":null,"previous_names":["16bit-ykiko/magic-cpp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/16bit-ykiko%2Fmagic-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/16bit-ykiko%2Fmagic-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/16bit-ykiko%2Fmagic-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/16bit-ykiko%2Fmagic-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/16bit-ykiko","download_url":"https://codeload.github.com/16bit-ykiko/magic-cpp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247471520,"owners_count":20944158,"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":["c-plus-plus","c-plus-plus-20","cplusplus","cplusplus-20","cpp","cpp20","easy-to-use","enum","field","header-only","no-dependencies","powerful","reflection","struct","typename"],"created_at":"2024-08-01T06:00:31.271Z","updated_at":"2025-04-06T11:08:43.867Z","avatar_url":"https://github.com/16bit-ykiko.png","language":"C++","readme":"![cover](docs/assets/cover.png)\n\nmagic-cpp is a header-only C++ library. It aims to make it easier for you to use C++, including a series of functions such as visualizing type names, reflection of structs and enumerations, etc. It can help you get rid of the compiler's error messages which are difficult to read\n\n[English](README.md) | [简体中文](README.zh.md)\n\n- [Visualizing type](#visualizing-type)\n  - [Basic usage](#basic-usage)\n  - [Custom type name](#custom-type-name)\n  - [Configurability](#configurability)\n  - [Other features](#other-features)\n\n\n# Visualizing type\n`#include \u003cmagic/visualize.h\u003e`to use features below, `C++17` is minimum required\n\n## Basic usage\nwhen using template programming, you often encounter the dilemma of type mismatch, especially when using libraries like `ranges`, templates are often deeply nested and difficult to read. Do not worry, `magic-cpp` can help you visualize the type, making it easier for human to understand the type. Consider the following example\n```cpp\nusing T = int (*(*(*)(int*))[4])(int*); // hard to understand\nstd::cout \u003c\u003c magic::visualize\u003cT\u003e() \u003c\u003c std::endl;\n```\nOutput:\n\n![visualize](docs/assets/sample_ptr.png)\n\nOr you may encounter this when writing code\n```cpp\nusing T = std::function\u003cint(const std::vector\u003cint\u003e\u0026, std::tuple\u003cint, int, int\u003e)\u003e; // hard to understand\nstd::cout \u003c\u003c magic::visualize\u003cT\u003e() \u003c\u003c std::endl;\n```\nOutput:\n\n![visualize](docs/assets/std_function.png)\n\nAlmost all commonly used templates can be converted into such a tree representation, which is clear at a glance. Of course, the full expansion of some types is very long and not what you expected, for example, the output effect of `std::string` on `gcc` is like this\n\n![visualize](docs/assets/full_std_string.png)\n\n## Custom type name\nAs you can see, there is a lot of information we don't want to see. It doesn't matter! Provide a custom type name through explicit specialization\n```cpp\ntemplate\u003c\u003e\nstruct magic::type_info\u003cstd::string\u003e\n{\n    inline static std::string name = \"std::string\";\n};\n```\nIn this way, when encountering `std::string`, only the following will be displayed\n\n![visualize](docs/assets/std_string.png)\n\nIt's the custom name, isn't it convenient? I have pre-defined some commonly used type aliases such as `std::size_t`, `std::string`, `std::vector` in `customization.h`. If you need it, you can try to modify or add it yourself\n\n## Configurability\nConsidering that some terminals do not support color, or do not support `utf` characters, the display will appear garbled, so we provide options to turn off these functions\n```cpp\nmagic::VisualizeOption option;\noption.utf_support = false;     // do not use utf8 characters\noption.color_support = false; // turn off color support\noption.full_name = true;        // use full name instead of custom alias\nstd::cout \u003c\u003c magic::visualize\u003cstd::string\u003e(option) \u003c\u003c std::endl;\n```\nOutput:\n\n![visualize](docs/assets/noutf_nocolor_full_std_string.png)\n\nif you want to customize the color scheme, you can use the `HighlightConfig` structure\n```cpp\nstruct HighlightConfig\n{\n    std::uint32_t type;     // type: int, double, ...\n    std::uint32_t nttp;     // non type template parameter: 1, 2, ...\n    std::uint32_t tmpl;     // template: std::vector, ...\n    std::uint32_t builtin;  // built-in compound type: ptr, ref...\n    std::uint32_t modifier; // modifier: const, volatile, ...\n    std::uint32_t tag;      // tag: R: , M: , ...\n};\n\n// default color scheme is Dark\nconstexpr static inline HighlightConfig Dark = {\n    .type = 0xE5C07B,     // yellow\n    .nttp = 0xD19A66,     // orange\n    .tmpl = 0x0087CE,     // blue\n    .builtin = 0xC678DD,  // purple\n    .modifier = 0x98C379, // green\n    .tag = 0x5C6370,      // gray\n};\n\n// there is also a built-in Light style color scheme\n```\nYou can also customize the color scheme yourself, and then pass it to the `visualize` function\n```cpp\nmagic::VisualizeOption option; // default option\nstd::cout \u003c\u003c magic::visualize\u003cstd::string\u003e(option, magic::Light) \u003c\u003c std::endl;\n```\n## Other features\nBesides visualizing types, we also support some other operations\n\nretrieving a type's `display_name`\n```cpp\n\n\n```\n\nretrieving `raw_name` in compile time\n```cpp\ntemplate\u003ctypename T\u003e\nstruct Point\n{\n    T start;\n    T end;\n};\n\n// retrieving a type's raw_name\nconstexpr auto name = magic::raw_name_of\u003cPoint\u003cint\u003e\u003e();\n// name =\u003e \"Point\u003cint\u003e\"\n\n// retrieving a non-type template parameter's raw_name\nconstexpr auto name2 = magic::raw_name_of\u003c1\u003e();\n// name2 =\u003e \"1\"\n\n// retrieving a template's raw_name\nconstexpr auto name3 = magic::raw_name_of_template\u003cPoint\u003cint\u003e\u003e();\n// name3 =\u003e \"Point\"\n\n// retrieving a member's raw_name, C++20 or higher is required\nPoint point;\nconstexpr auto name4 = magic::raw_name_of_member\u003c\u0026point.start\u003e();\n// name4 =\u003e \"start\"\n\nconstexpr auto name5 = magic::raw_name_of_member\u003c\u0026point.end\u003e();\n// name5 =\u003e \"end\"\n\n\nenum class Color\n{\n    RED,\n    GREEN,\n    BLUE,\n};\n\n// retrieving an enumeration's raw_name\nconstexpr auto name6 = magic::raw_name_of\u003cColor::RED\u003e();\n// name6 =\u003e \"RED\"\n```\nPlease note that the content obtained by these methods may be different on different compilers, please do not use them to build the core part of the code.","funding_links":[],"categories":["序列化"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F16bit-ykiko%2Fmagic-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F16bit-ykiko%2Fmagic-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F16bit-ykiko%2Fmagic-cpp/lists"}