An open API service indexing awesome lists of open source software.

https://github.com/lb--/cloning

My personal take on the clone pattern in C++.
https://github.com/lb--/cloning

Last synced: 4 months ago
JSON representation

My personal take on the clone pattern in C++.

Awesome Lists containing this project

README

          

cloning [![travis](https://travis-ci.org/LB--/cloning.svg?branch=cloning)](https://travis-ci.org/LB--/cloning)
=======
This library is my personal take on the clone pattern. `CloneImplementor<>` uses the CRTP to help reduce boilerplate. `ClonePtr` is a copyable smart pointer for cloneable types.

_This library requires that your compiler support C++1z (the C++ standard after C++14)_

## Usage
### CMake
From the `cmake` directory, copy the `FindLB` directory to a place in your `CMAKE_MODULE_PATH`.
Then, add `find_package(LB/cloning REQUIRED)` to your CMake script.
You may need to set the CMake variable `LB/cloning_ROOT` if you installed to a nonstandard location.
Finally, link to the `LB::cloning` imported target with `target_link_libraries()`.

### C++
`#include `

#### Making a class cloneable
If you want to make a class cloneable, it and all its derived classes should derive from `CloneImplementor<>`.
The template parameter is your deriving class, e.g. `CloneImplementor`.

Once you have derived from `CloneImplementor<>`, you need to override the private virtual member function `clone()` (or omit it for abstract classes).
All `CloneImplementor<>` does for you is provide the static `Clone<>()` member function which is used to properly clone a class.

Example cloneable class:
```cpp
struct MyClass
: LB::cloning::CloneImplementor
{
protected:
MyClass(MyClass const &from) noexcept;
private:
virtual MyClass *clone() const noexcept override
{
return new MyClass(*this);
}
};
```
You need to properly implement the protected copy constructor to account for parent classes and deriving classes.
Note that abstract classes do not need to implement the `clone()` member function as it will never (should never) be used.

#### Cloning cloneable types
To 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:
```cpp
void f(SomeDerivedType const &v) noexcept
{
auto clone0 = SomeDerivedType::Clone<>(v); //unique_ptr
auto clone1 = SomeParentType::Clone(v); //shared_ptr

g(*clone0);
h(std::move(clone1));
}
```