{"id":24203396,"url":"https://github.com/m0t9/c-generics","last_synced_at":"2025-06-27T11:41:15.825Z","repository":{"id":119470222,"uuid":"583704220","full_name":"m0t9/c-generics","owner":"m0t9","description":"Idea of generic containers (templates) implementation on pure C ⚙️","archived":false,"fork":false,"pushed_at":"2023-09-10T11:35:29.000Z","size":24,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-03T11:44:29.143Z","etag":null,"topics":["c","generics","generics-c","macros","preprocessing","stl-c","templates-c"],"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/m0t9.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2022-12-30T16:24:01.000Z","updated_at":"2024-04-08T19:20:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"54bb6f58-61d4-465a-a1ea-6771f9ebf7cb","html_url":"https://github.com/m0t9/c-generics","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"purl":"pkg:github/m0t9/c-generics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m0t9%2Fc-generics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m0t9%2Fc-generics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m0t9%2Fc-generics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m0t9%2Fc-generics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m0t9","download_url":"https://codeload.github.com/m0t9/c-generics/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m0t9%2Fc-generics/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262249365,"owners_count":23281947,"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","generics","generics-c","macros","preprocessing","stl-c","templates-c"],"created_at":"2025-01-13T22:34:50.478Z","updated_at":"2025-06-27T11:41:15.774Z","avatar_url":"https://github.com/m0t9.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# c-generics\n\n## ❓ What is this repository about\nC language does not support generics and templates. This repository introduces one of the possible (and convenient) ways to implement generic containers using C preprocessing macros.\n\n## 🧰 How to implement custom generic data structure\n1. Header file with any generic container implementation **must** include `instantiation.h` file, which allows programmer to use simpler and standardized typed container initialization.\n2. Container ***instantiation*** should be implemented in implicit and explicit ways. Implicit instantiation is a `#define` directive that accepts arguments such as structure name, type (data type that structure will contain), arguments (optional instantiation parametres). This instantiation will explicitly create a ***template*** of data structure of the given type. This define name **must** have the form `_do_instantiate_\u003cstruct_name\u003e`.\n3. Explicit instantiation is a convenient way for programmer to create a template of data structure. This `#define` may have the form `instantiate_\u003cstruct_name\u003e(type)` and substitute `INSTANTIATION_INSTANTIATE_CONTAINER(\u003cstruct_name\u003e, \u003ctype\u003e, )`.\n4. To create an ***instance*** of previously described container header file **must** implement directive `#define \u003cstruct_name\u003e(\u003ctype\u003e)` that declares a type named `INSTANTIATION_GET_CONTAINER_NAME(\u003cstruct_name\u003e, \u003ctype\u003e)`.\n5. Container methods can be implemented in different ways (either `#define` or regular functions). It may depend on container type or your ideology :)\n\nYou can check implementation details in our examples of [stack implementation](https://github.com/m0t9/c-generics/blob/master/stack.h) and [vector implementation](https://github.com/m0t9/c-generics/blob/master/vector.h).\n\n## 🚩 Usage\n### Example for [stack](https://github.com/m0t9/c-generics/blob/master/stack.h)\n#### Code\n```c\n#include \u003cstdio.h\u003e\n#include \"stack.h\"\n\ntypedef struct box {\n    int weight;\n    char *content;\n} box;\n\ninstantiate_stack(box);\n\nint main() {\n    stack(box) storage = init_stack();\n\n    box toolbox = {.weight = 5, .content = \"tools\"};\n    box clothes = {.weight = 3, .content = \"t-shirts, pants\"};\n    box paper = {.weight = 1, .content = \"sheets of paper\"};\n\n    stack_push(\u0026storage, toolbox);\n    stack_push(\u0026storage, clothes);\n    stack_push(\u0026storage, paper);\n\n    printf(\"%lld boxes in storage\\n\", stack_size(\u0026storage));\n    while (!stack_empty(\u0026storage)) {\n        printf(\"Box with %s and weight %d kg.\\n\", stack_top(\u0026storage).content, stack_top(\u0026storage).weight);\n        stack_pop(\u0026storage);\n    }\n}\n```\n#### Output\n```\n3 boxes in storage\nBox with sheets of paper and weight 1 kg.\nBox with t-shirts, pants and weight 3 kg.\nBox with tools and weight 5 kg.\n```\n### Example for [vector](https://github.com/m0t9/c-generics/blob/master/vector.h)\n#### Code\n```c\n#include \u003cstdio.h\u003e\n#include \"vector.h\"\n\ninstantiate_vector((char)(*));\n\nint main() {\n    vector((char)(*)) shopping_list = init_vector();\n\n    vector_push(\u0026shopping_list, \"Potatoes\");\n    vector_push(\u0026shopping_list, \"Carrots\");\n    vector_push(\u0026shopping_list, \"Meat\");\n    vector_pop(\u0026shopping_list);\n    vector_push(\u0026shopping_list, \"Milk\");\n\n    printf(\"%d products in list\\n\", vector_size(\u0026shopping_list));\n    for (int i = 0; i \u003c vector_size(\u0026shopping_list); i++) {\n        printf(\"%d. %s\\n\", i + 1, shopping_list[i]);\n    }\n\n    vector_destroy(\u0026shopping_list);\n    return 0;\n}\n```\n#### Output\n```\n3 products in list\n1. Potatoes\n2. Carrots\n3. Milk\n```\n## 🌟 I express my gratitude to [purplesyringa](https://github.com/purplesyringa) for her great contribution!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm0t9%2Fc-generics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm0t9%2Fc-generics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm0t9%2Fc-generics/lists"}