{"id":17039040,"url":"https://github.com/dcnick3/foreign-struct","last_synced_at":"2025-03-22T23:44:56.406Z","repository":{"id":107292080,"uuid":"394766380","full_name":"DCNick3/foreign-struct","owner":"DCNick3","description":null,"archived":false,"fork":false,"pushed_at":"2021-08-18T10:46:04.000Z","size":65,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-28T03:29:53.228Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DCNick3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-08-10T19:54:38.000Z","updated_at":"2021-08-18T10:46:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"361d784c-f40a-49bc-98eb-bb92dca7e090","html_url":"https://github.com/DCNick3/foreign-struct","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/DCNick3%2Fforeign-struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DCNick3%2Fforeign-struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DCNick3%2Fforeign-struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DCNick3%2Fforeign-struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DCNick3","download_url":"https://codeload.github.com/DCNick3/foreign-struct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245036121,"owners_count":20550661,"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-14T08:58:29.307Z","updated_at":"2025-03-22T23:44:56.382Z","avatar_url":"https://github.com/DCNick3.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"![workflow badge](https://github.com/DCNick3/foreign-struct/actions/workflows/cmake.yml/badge.svg)\n\n# foreign-struct (name subject to change)\n\nC++20 header-only library to interoperate with C objects of any layout, all in standard C++.\n\nNo macros, no (enforced) code generation, no external dependencies, widely extensible.\n\nThis project was born as an effort to implement Win32 APIs in a way that would work with any compiler on any platform, not necessarily layout-compatible. It expects the compiler to support used integer sizes though.\n\n## concepts\n\nIn general, it works by converting the \"target\" data representation to \"host\" (materialization) and back (unmaterialization).\nThis may seem inefficient, but compilers are smart enough to optimize \"materialize, modify, unmaterialize\" chains of operations into in-place modification.\n\nOut-of-the box it supports the following object kinds:\n- integers\n- enums\n- structures\n- unions\n- arrays\n\nWhile integers, enums and arrays do not require any prior definitions, you would have to supply some info for structures and unions.\n\n### structure definition\n\nFor struct you would need to specify the list of fields, their offsets and overall size of the structure. Currently there is no way to automatically lay out the fields.\n\n```C++\nusing def = foreign::target_struct_def;\nusing field = foreign::target_struct_field;\n\nusing test_struct_def = def\u003c\n  // specify the structure size\n  16,\n  // specify the type and offset of fields\n  field\u003cstd::uint64_t, 0\u003e,\n  field\u003cstd::int32_t, 8\u003e\n\u003e;\n```\n\nThe structure can have gaps in it (i.e. regions that are not covered by any fields). This is usually needed to support structure alignment.\n\n\nAfter having the structure definition you two options for accessing your members: you either access them via indeces like an `std::tuple`:\n```C++\nusing test_struct = target_struct_unnamed\u003ctest_struct_def\u003e;\n```\n\nor define a wrapper class which assigns names to the fields:\n\n```C++\nstruct test_struct : public target_struct_base\u003ctest_struct_def, test_struct\u003e {\n    inline test_struct(mat_tuple \u0026\u0026values)\n            :\n            value1(std::move(std::get\u003c0\u003e(values))),\n            value2(std::move(std::get\u003c1\u003e(values))),\n            value3(std::move(std::get\u003c2\u003e(values))),\n            value4(std::move(std::get\u003c3\u003e(values))) {}\n\n    int32_t value1;\n    uint64_t value2;\n    test_enum value3;\n    test_union value4;\n\n    using field_accessor = target_struct_base::field_accessor_base\u003c\n            \u0026test_struct::value1,\n            \u0026test_struct::value2,\n            \u0026test_struct::value3,\n            \u0026test_struct::value4\n    \u003e;\n};\n```\n\nNotice the `field_accessor` nested type that allows the library internals to access the fields for unmaterialization. (TODO: do we need the constructor, or can we get away with field_accesor?)\n\nAs one can see, this requires quite a lot of boilerplate, so it might be a good idea to generate this code from existing C API definitions\n\n## Extensibility\n\nCan be extended to support different endianness, different types representation, custom pointers, etc (TODO: how?)\n\nTODO: write more examples, mention `target_rw_mat_holder` (name subject to change), exaplain how any why unions are to be used, work out the ideomatic use-cases\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcnick3%2Fforeign-struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcnick3%2Fforeign-struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcnick3%2Fforeign-struct/lists"}