hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
cloneable.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_CLONEABLE_HPP
7 #define IROHA_CLONEABLE_HPP
8 
9 #include <memory>
10 
32 template <typename T>
33 class Cloneable;
34 
41 template <typename T>
42 std::unique_ptr<T> clone(const T &object) {
43  using base_type = typename T::base_type;
44  static_assert(std::is_base_of<base_type, T>::value,
45  "T object has to derived from T::base_type");
46  auto ptr = static_cast<const Cloneable<base_type> &>(object).clone();
47  return std::unique_ptr<T>(static_cast<T *>(ptr));
48 }
49 
56 template <typename T>
57 auto clone(T *object) {
58  return clone(*object);
59 }
60 
65 template <typename T>
66 class Cloneable {
67  public:
68  using base_type = T;
69 
70  virtual ~Cloneable() = default;
71 
72  protected:
78  virtual T *clone() const = 0;
79 
80  template <typename X>
81  friend std::unique_ptr<X> clone(const X &);
82 };
83 
84 #endif // IROHA_CLONEABLE_HPP
T base_type
Definition: cloneable.hpp:68
Definition: cloneable.hpp:33
std::unique_ptr< T > clone(const T &object)
Definition: cloneable.hpp:42
virtual T * clone() const =0
virtual ~Cloneable()=default