{"id":27781249,"url":"https://github.com/gilzoide/functor2c","last_synced_at":"2025-10-08T13:23:38.924Z","repository":{"id":290065915,"uuid":"973267682","full_name":"gilzoide/functor2c","owner":"gilzoide","description":"Single header templates for wrapping C++ functors as opaque userdata plus function pointers for C interop","archived":false,"fork":false,"pushed_at":"2025-04-26T16:33:09.000Z","size":175,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-02T23:04:52.795Z","etag":null,"topics":["c-interop","cpp","cpp11","cpp17","function-pointer","function-pointers","functor","single-file","single-file-library","single-header","single-header-library"],"latest_commit_sha":null,"homepage":"https://gilzoide.github.io/functor2c/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gilzoide.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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,"zenodo":null},"funding":{"github":["gilzoide"],"patreon":null,"open_collective":null,"ko_fi":"gilzoide","tidelift":null,"community_bridge":null,"liberapay":"gilzoide","issuehunt":null,"lfx_crowdfunding":null,"polar":null,"buy_me_a_coffee":null,"thanks_dev":null,"custom":null}},"created_at":"2025-04-26T16:16:09.000Z","updated_at":"2025-04-28T09:27:09.000Z","dependencies_parsed_at":"2025-04-26T17:27:27.887Z","dependency_job_id":"3b0040f3-26c6-4f90-99e8-be6e48849cc7","html_url":"https://github.com/gilzoide/functor2c","commit_stats":null,"previous_names":["gilzoide/functor2c"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gilzoide/functor2c","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Ffunctor2c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Ffunctor2c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Ffunctor2c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Ffunctor2c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gilzoide","download_url":"https://codeload.github.com/gilzoide/functor2c/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Ffunctor2c/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278950056,"owners_count":26074148,"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","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-interop","cpp","cpp11","cpp17","function-pointer","function-pointers","functor","single-file","single-file-library","single-header","single-header-library"],"created_at":"2025-04-30T12:43:43.222Z","updated_at":"2025-10-08T13:23:38.890Z","avatar_url":"https://github.com/gilzoide.png","language":"C++","readme":"# functor2c\nSingle header templates for wrapping C++ functors as opaque userdata plus function pointers for C interop.\n\n\n## Features\n- Easily wrap functors such as `std::function` or lambdas as function pointers to use in C APIs\n- Supports functors with parameters and return values of any type\n- Provides deleter functionality to avoid memory leaks, including overloads that return smart pointers\n- Requires C++11 or newer\n- Automatic arguments / return type deduction when used in C++17\n\n\n## Example usage\n```cpp\n#include \"functor2c.hpp\"\n\n// Let's use as an example Lua's allocator function `lua_Alloc`\n// Note that it accepts an opaque userdata as the first parameter (prefix)\ntypedef void *(*lua_Alloc)(void *ud, void *ptr, size_t osize, size_t nsize);\n\n// 1. Define the functor for it\nauto alloc_func = [](void *ptr, size_t osize, size_t nsize) {\n    ptr = /* implementation ... */;\n    return ptr;\n};\n\n// 2. Now create the opaque userdata + function pointers for it\n// 2.a) C++17 supports type deduction and structured bindings\nauto [userdata, invoke_fptr, delete_fptr] = functor2c::prefix_invoker_deleter(alloc_func);\n// 2.b) C++11 requires specifying functor type as \u003cReturnType, ArgumentTypes...\u003e\nvoid *userdata;\nlua_Alloc invoke_fptr;\nvoid (*delete_fptr)(void*);\nstd::tie(userdata, invoke_fptr, delete_fptr) = functor2c::prefix_invoker_deleter\u003cvoid*, void*, size_t, size_t\u003e(alloc_func);\n\n// 3. Pass the invoke function pointer + opaque userdata to C APIs\nlua_setallocf(L, invoke_fptr, userdata);\n\n// 4. Delete userdata to avoid memory leaks.\n// Note: optionally use `prefix_invoker_unique` to get userdata as a unique_ptr\n// and `prefix_invoker_shared` to get userdata as a shared_ptr.\ndelete_fptr(userdata);\n```\n\nIn case the C API expects the opaque userdata as last argument instead of first, use `suffix_invoker_*` functions instead of `prefix_invoker_*`.\n```cpp\n// For example Box2D 3.1.0 custom filter callback.\n// Note that it accepts an opaque userdata as the last parameter (suffix)\ntypedef bool b2CustomFilterFcn(b2ShapeId shapeIdA, b2ShapeId shapeIdB, void* context);\n\nauto filter_callback = [](b2ShapeId shapeIdA, b2ShapeId shapeIdB) {\n    /* implementation ... */\n    return true;\n};\nauto [userdata, invoke_fptr] = functor2c::suffix_invoker_unique(filter_callback);\nb2World_SetCustomFilterCallback(world_id, invoke_fptr, userdata.get());\n```\n\n\n## Integrating with CMake\nYou can integrate functor2c with CMake targets by adding a copy of this repository and linking with the `functor2c` target:\n```cmake\nadd_subdirectory(path/to/functor2c)\ntarget_link_libraries(my_awesome_target functor2c)\n```\n","funding_links":["https://github.com/sponsors/gilzoide","https://ko-fi.com/gilzoide","https://liberapay.com/gilzoide"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilzoide%2Ffunctor2c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgilzoide%2Ffunctor2c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilzoide%2Ffunctor2c/lists"}