{"id":21995832,"url":"https://github.com/lb--/cloning","last_synced_at":"2026-03-19T21:29:38.027Z","repository":{"id":148327925,"uuid":"42648581","full_name":"LB--/cloning","owner":"LB--","description":"My personal take on the clone pattern in C++.","archived":false,"fork":false,"pushed_at":"2016-03-22T12:22:27.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"cloning","last_synced_at":"2025-03-15T03:41:51.871Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"CMake","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/LB--.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":"2015-09-17T09:56:08.000Z","updated_at":"2023-09-08T17:01:46.000Z","dependencies_parsed_at":"2023-05-19T19:45:40.074Z","dependency_job_id":null,"html_url":"https://github.com/LB--/cloning","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/LB--%2Fcloning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LB--%2Fcloning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LB--%2Fcloning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LB--%2Fcloning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LB--","download_url":"https://codeload.github.com/LB--/cloning/tar.gz/refs/heads/cloning","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245053715,"owners_count":20553381,"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-11-29T21:18:48.159Z","updated_at":"2026-01-04T22:49:10.323Z","avatar_url":"https://github.com/LB--.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"cloning [![travis](https://travis-ci.org/LB--/cloning.svg?branch=cloning)](https://travis-ci.org/LB--/cloning)\n=======\nThis library is my personal take on the clone pattern. `CloneImplementor\u003c\u003e` uses the CRTP to help reduce boilerplate. `ClonePtr` is a copyable smart pointer for cloneable types.\n\n_This library requires that your compiler support C++1z (the C++ standard after C++14)_\n\n## Usage\n### CMake\nFrom the `cmake` directory, copy the `FindLB` directory to a place in your `CMAKE_MODULE_PATH`.\nThen, add `find_package(LB/cloning REQUIRED)` to your CMake script.\nYou may need to set the CMake variable `LB/cloning_ROOT` if you installed to a nonstandard location.\nFinally, link to the `LB::cloning` imported target with `target_link_libraries()`.\n\n### C++\n`#include \u003cLB/cloning/cloning.hpp\u003e`\n\n#### Making a class cloneable\nIf you want to make a class cloneable, it and all its derived classes should derive from `CloneImplementor\u003c\u003e`.\nThe template parameter is your deriving class, e.g. `CloneImplementor\u003cMyClass\u003e`.\n\nOnce you have derived from `CloneImplementor\u003c\u003e`, you need to override the private virtual member function `clone()` (or omit it for abstract classes).\nAll `CloneImplementor\u003c\u003e` does for you is provide the static `Clone\u003c\u003e()` member function which is used to properly clone a class.\n\nExample cloneable class:\n```cpp\nstruct MyClass\n: LB::cloning::CloneImplementor\u003cMyClass\u003e\n{\nprotected:\n\tMyClass(MyClass const \u0026from) noexcept;\nprivate:\n\tvirtual MyClass *clone() const noexcept override\n\t{\n\t\treturn new MyClass(*this);\n\t}\n};\n```\nYou need to properly implement the protected copy constructor to account for parent classes and deriving classes.\nNote that abstract classes do not need to implement the `clone()` member function as it will never (should never) be used.\n\n#### Cloning cloneable types\nTo properly clone an instance of a cloneable class, you need to call the static `Clone` member function from the class type you want the resulting clone to be accessible through:\n```cpp\nvoid f(SomeDerivedType const \u0026v) noexcept\n{\n\tauto clone0 = SomeDerivedType::Clone\u003c\u003e(v); //unique_ptr\u003cSomeDerivedType\u003e\n\tauto clone1 = SomeParentType::Clone\u003cstd::shared_ptr\u003e(v); //shared_ptr\u003cSomeParentType\u003e\n\n\tg(*clone0);\n\th(std::move(clone1));\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flb--%2Fcloning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flb--%2Fcloning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flb--%2Fcloning/lists"}