VCCC  2024.05
VisualCamp Common C++ library
dtor.h
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2021/05/24.
3 # */
4 #
5 # ifndef VCCC_OPTIONAL_INTERNAL_DTOR_H_
6 # define VCCC_OPTIONAL_INTERNAL_DTOR_H_
7 #
8 # include <memory>
9 # include <type_traits>
10 # include <utility>
11 #
14 
15 namespace vccc {
16 namespace internal {
17 namespace optional {
18 
19 template<typename T, bool v = /* true */ std::is_trivially_destructible<T>::value>
20 struct dtor {
21  using value_type = T;
22 
23  constexpr dtor() noexcept
24  : null{} {}
25 
26  template<typename ...Args>
27  constexpr explicit dtor(in_place_t, Args&&... args)
28  : val(std::forward<Args>(args)...),
29  valid(true) {}
30 
31  void reset() {
32  if (valid) {
33  val.~value_type();
34  valid = false;
35  }
36  }
37 
38  constexpr inline const value_type* pointer() const noexcept { return vccc::addressof(val); }
39  constexpr inline value_type* pointer() noexcept { return vccc::addressof(val); }
40 
41  constexpr inline const value_type& ref() const& noexcept { return val; }
42  constexpr inline value_type& ref() & noexcept { return val; }
43  constexpr inline const value_type& ref() const&& noexcept { return std::move(val); }
44  constexpr inline value_type& ref() && noexcept { return std::move(val); }
45 
46  template<typename ...Args>
47  void construct(Args&&... args) {
48  ::new((void*)vccc::addressof(val)) value_type(std::forward<Args>(args)...);
49  valid = true;
50  }
51 
52  template<typename Other>
53  void construct_if(Other&& other) {
54  if (other)
55  construct(*std::forward<Other>(other));
56  }
57 
58  ~dtor() = default;
59 
60  union {
61  char null;
62  value_type val;
63  };
64  bool valid = false;
65 };
66 
67 template<typename T>
68 struct dtor<T, false> {
69  using value_type = T;
70 
71  constexpr dtor() noexcept
72  : null{} {}
73 
74  template<typename ...Args>
75  constexpr explicit dtor(in_place_t, Args&&... args)
76  : val(std::forward<Args>(args)...),
77  valid(true) {}
78 
79  void reset() {
80  if (valid) {
81  val.~value_type();
82  valid = false;
83  }
84  }
85 
86  constexpr inline const value_type* pointer() const noexcept { return vccc::addressof(val); }
87  constexpr inline value_type* pointer() noexcept { return vccc::addressof(val); }
88 
89  constexpr inline const value_type& ref() const& noexcept { return val; }
90  constexpr inline value_type& ref() & noexcept { return val; }
91  constexpr inline const value_type& ref() const&& noexcept { return std::move(val); }
92  constexpr inline value_type& ref() && noexcept { return std::move(val); }
93 
94  template<typename ...Args>
95  void construct(Args&&... args) {
96  ::new((void*)vccc::addressof(val)) value_type(std::forward<Args>(args)...);
97  valid = true;
98  }
99 
100  template<typename Other>
101  void construct_if(Other&& other) {
102  if (other)
103  construct(*std::forward<Other>(other));
104  }
105 
106  ~dtor() {
107  if (valid)
108  val.T::~T();
109  };
110 
111  union {
112  char null;
113  T val;
114  };
115  bool valid = false;
116 };
117 
118 } // namespace optional
119 } // namespace internal
120 } // namespace vccc
121 
122 # endif // VCCC_OPTIONAL_INTERNAL_DTOR_H_
std::enable_if_t< std::is_object< T >::value, T * > addressof(T &t) noexcept
Definition: addressof.hpp:33
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35