Kagome
Polkadot Runtime Engine in C++17
box.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_BOX_HPP
7 #define KAGOME_BOX_HPP
8 
9 #include <iostream>
10 #include <type_traits>
11 
12 template <typename T>
13 struct Box {
14  using Type = T;
15 
16  template <typename... A>
17  explicit Box(A &&... args) : t_{std::forward<A>(args)...} {}
18  Box(Box &box) : Box{std::move(box)} {}
19 
20  Box(Box &&box) : t_{std::move(*box.t_)} {
21  if constexpr (!std::is_pod_v<T>) box.t_ = std::nullopt;
22  }
23 
24  Box &operator=(Box &val) {
25  return operator=(std::move(val));
26  }
27 
28  Box &operator=(Box &&box) {
29  t_ = std::move(*box.t_);
30  if constexpr (!std::is_pod_v<T>) box.t_ = std::nullopt;
31  return *this;
32  }
33 
34  auto clone() const {
35  assert(t_);
36  return Box<T>{*t_};
37  }
38 
39  T &mut_value() & {
40  assert(t_);
41  return *t_;
42  }
43 
44  T const &value() const & {
45  assert(t_);
46  return *t_;
47  }
48 
49  private:
50  std::optional<T> t_;
51 };
52 
53 #endif // KAGOME_BOX_HPP
Box(Box &box)
Definition: box.hpp:18
Definition: box.hpp:13
Box(A &&...args)
Definition: box.hpp:17
T Type
Definition: box.hpp:14
Box & operator=(Box &val)
Definition: box.hpp:24
Box & operator=(Box &&box)
Definition: box.hpp:28
auto clone() const
Definition: box.hpp:34
Box(Box &&box)
Definition: box.hpp:20
T & mut_value()&
Definition: box.hpp:39
std::optional< T > t_
Definition: box.hpp:50
T const & value() const &
Definition: box.hpp:44