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 
38 template <typename T>
39 std::unique_ptr<T> clone(const T &object) {
40  using base_type = typename T::base_type;
41  static_assert(std::is_base_of<base_type, T>::value,
42  "T object has to derived from T::base_type");
43  auto ptr = static_cast<const base_type &>(object).clone();
44  return std::unique_ptr<T>(static_cast<T *>(ptr));
45 }
46 
53 template <typename T>
54 auto clone(T *object) {
55  return clone(*object);
56 }
57 
62 template <typename T>
63 class Cloneable {
64  public:
65  using base_type = T;
66 
67  virtual ~Cloneable() = default;
68 
69  protected:
75  virtual T *clone() const = 0;
76 
77  template <typename X>
78  friend std::unique_ptr<X> clone(const X &);
79 };
80 
81 #endif // IROHA_CLONEABLE_HPP
ModelPrimitive< TransferAsset > base_type
Definition: cloneable.hpp:65
Definition: cloneable.hpp:63
std::unique_ptr< T > clone(const T &object)
Definition: cloneable.hpp:39
virtual T * clone() const =0
virtual ~Cloneable()=default