clone-tools Build Status Documentation Status

A small, header-only Modern C++ library to make cloning (ie copying polymorphic types) easy

Resources

Purpose

clone_ptr<> is just like std::unique_ptr<> but with natural copy semantics which are provided by demanding that the wrapped type provides a clone() method (ie a pseudo polymorphic-copy-constructor).

Features

  • Zero overhead over unique_ptr for equivalent operations (ie modern compilers should easily optimise the extra code away)
  • Header-only
  • Simple to use

How to use

Step 1
Add a clone() method (ie a pseudo polymorphic-copy-constructor) to your class hierarchy

struct my_base {
    virtual ~my_base() noexcept = default; ///< Make dtor virtual

    virtual std::unique_ptr<my_base> clone() const = 0;
};

struct my_derived : public my_base {
    virtual std::unique_ptr<my_base> clone() const override final {
        return { std::make_unique<my_derived>( *this ) };
    }
};

(...but prefer using the NVI pattern and clone::make_uptr_clone(), see here for more).

Step 2
Create and use a clone_ptr just like a unique_ptr:

using clone::clone_ptr;
using clone::make_clone;

const clone_ptr<my_base> ptr1 = make_clone<my_derived>();

// Or equivalently...
const auto ptr2 = make_clone<my_derived, my_base>();

Step 3
Enjoy completely natural copy semantics

const clone_ptr<my_base> ptr_copy{ ptr1 };

...which copies the pointed-to object using its clone() method.

To find out more, please dive into the docs.

Feedback

Please tell us your clone-tools bugs/suggestions here.

Supported compilers

The library should work on any conformant C++11 / C++14 compiler. At present, the tests are built and run on Travis-CI under:

  • Linux, Clang 3.5.0 (with libc++)
  • Linux, GCC 4.8.4 (with libstdc++)
  • Mac, Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)

 

 

 

 

TODOCUMENT :

  • document all headers
  • all gubbins
  • examples on all pages
  • motivation and simple usage
  • comparison with other clone_ptrs
  • comparison with polymorphic_value