Pretty Good Sum Type  1.0.0
recursive_wrapper.hpp
Go to the documentation of this file.
1 #if !defined(RECURSIVE_WRAPPER_77D62153_38D6_4311_8B5C_384D740BDF6E_H)
2 # define RECURSIVE_WRAPPER_77D62153_38D6_4311_8B5C_384D740BDF6E_H
3 
11 
12 #include <pgs/type_traits.hpp>
13 
14 #include <utility> // std::forward<>()
15 
16 namespace pgs {
17 
19 template <class T>
20 class recursive_wrapper; //fwd. decl.
22 
28 template <class T>
30 {};
31 
34 template <class T>
36 {};
37 
39 template <class T>
41 
44 //
46 template <class T>
48  typedef T type;
49 };
50 
53 template <class T>
55  typedef T type;
56 };
58 
61 template <class W>
62 using recursive_wrapper_unwrap_t =
64 
67 template <class T, class U>
69  and_<
72  >;
73 
76 template <class T, class U>
79 
81 template <class T>
83 private:
84  T* p_;
85 
86 private:
87  recursive_wrapper& assign (T const& rhs);
88 
89 public:
90  typedef T type;
91 
92 public:
93 
95  template <class... Args> recursive_wrapper (Args&&... args);
99  recursive_wrapper (type const& rhs);
103  recursive_wrapper(type&& rhs);
106 
110  recursive_wrapper& operator=(type const& rhs);
114  recursive_wrapper& operator=(type&& rhs);
115 
117  void swap(recursive_wrapper& rhs) noexcept;
118 
119  type& get();
120  type const& get() const;
121  type* get_pointer();
122  type const* get_pointer() const;
123 };
124 
126 template <class T>
127 bool operator== (
128  recursive_wrapper<T> const& lhs, recursive_wrapper<T> const& rhs) {
129  return lhs.get () == rhs.get ();
130 }
131 
134 template <class T>
135 bool operator!= (
136  recursive_wrapper<T> const& lhs, recursive_wrapper<T> const& rhs) {
137  return !(lhs.get () == rhs.get ());
138 }
139 
140 }//namespace pgs
141 
143 namespace pgs {
144 
145 template<class T>
146  template <class... Args>
147 recursive_wrapper<T>::recursive_wrapper (Args&&... args) :
148  p_(new T (std::forward<Args>(args)...)) {
149 }
150 
151 template<class T>
152 recursive_wrapper<T>::recursive_wrapper (recursive_wrapper const& rhs) :
153  p_ (new T (rhs.get ())) {
154 }
155 
156 template<class T>
157 recursive_wrapper<T>::recursive_wrapper (T const& rhs) : p_ (new T (rhs)) {
158 }
159 
160 template<class T>
161 recursive_wrapper<T>::recursive_wrapper(recursive_wrapper&& rhs) :
162  p_ (new T (std::move (rhs.get ()))) {
163 }
164 
165 template <class T>
166 recursive_wrapper<T>::recursive_wrapper(T&& rhs) : p_ (new T (std::move (rhs))) {
167 }
168 
169 template <class T>
171  delete p_;
172 }
173 
174 template <class T>
175 recursive_wrapper<T>&
176  recursive_wrapper<T>::operator=(recursive_wrapper const& rhs){
177  return assign (rhs.get());
178 }
179 
180 template <class T>
181 recursive_wrapper<T>& recursive_wrapper<T>::operator=(T const& rhs) {
182  return assign (rhs);
183 }
184 
185 template <class T>
186 recursive_wrapper<T>& recursive_wrapper<T>::assign (T const& rhs) {
187  this->get() = rhs; return *this;
188 }
189 
190 template <class T>
191 void recursive_wrapper<T>::swap(recursive_wrapper& rhs) noexcept {
192  std::swap (p_, rhs.p_);
193 }
194 
195 template <class T>
196 recursive_wrapper<T>&
197 recursive_wrapper<T>::operator=(recursive_wrapper&& rhs) noexcept {
198  swap (rhs);
199  return *this;
200 }
201 
202 template <class T>
203 recursive_wrapper<T>&
205  get() = std::move (rhs);
206  return *this;
207 }
208 
209 template <typename T>
210 inline void swap(
211  recursive_wrapper<T>& lhs, recursive_wrapper<T>& rhs) noexcept {
212  lhs.swap(rhs);
213 }
214 
215 template <typename T>
216 T& recursive_wrapper<T>::get() { return *get_pointer(); }
217 
218 template <typename T>
219 T const& recursive_wrapper<T>::get() const { return *get_pointer(); }
220 
221 template <typename T>
222 T* recursive_wrapper<T>::get_pointer() { return p_; }
223 
224 template <typename T>
225 T const* recursive_wrapper<T>::get_pointer() const { return p_; }
226 
227 }//namespace pgs
228 
230 
231 #endif
recursive_wrapper(Args &&...args)
Forwarding ctor (heap allocates type instance)
Primary template of a metafunction to classify a type as recursive_wrapper<> or not.
Definition: recursive_wrapper.hpp:29
void swap(recursive_wrapper &rhs) noexcept
Swap with recursive_wrapper
Parameter pack conjunction.
Definition: logical.hpp:83
T type
For T not a recursive wrapper.
Definition: recursive_wrapper.hpp:48
T swap(T...args)
Definition: logical.hpp:13
type & get()
Accessor to the type instance.
Definition: type_traits.hpp:26
~recursive_wrapper()
Destructor (frees heap-allocated type instance)
T type
Alias type for T
Definition: recursive_wrapper.hpp:90
T move(T...args)
T type
Definition: recursive_wrapper.hpp:55
At this time contains only a metafunction for determining if a given type F is "callable" on a parame...
types
Definition: recursive_wrapper.hpp:82
Primary template of a metafunction to compute the type contained by a recursive_wrapper<>.
Definition: recursive_wrapper.hpp:47
type * get_pointer()
Accessor to the type instance.
T forward(T...args)
recursive_wrapper & operator=(recursive_wrapper const &rhs)
Copy-assign from recursive_wrapper