LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
cloneable.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 #ifndef LBANN_UTILS_CLONEABLE_HPP_INCLUDED
27 #define LBANN_UTILS_CLONEABLE_HPP_INCLUDED
28 
29 #include <h2/meta/core/SFINAE.hpp>
30 #include <memory>
31 #include <type_traits>
32 #include <vector>
33 
45 namespace lbann {
46 
54 template <typename Base>
55 struct AsVirtualBase : virtual Base
56 {
57  using Base::Base;
58 };
59 
69 template <typename T>
71 {
72 };
73 
80 template <typename T>
82 
93 template <typename T, typename... Base>
94 class Cloneable : public Base...
95 {
96 public:
98  std::unique_ptr<T> clone() const
99  {
100  return std::unique_ptr<T>{static_cast<T*>(this->do_clone_())};
101  }
102 
103 private:
105  virtual Cloneable* do_clone_() const override
106  {
107  return new T(static_cast<T const&>(*this));
108  }
109 }; // class Cloneable
110 
111 template <typename T, typename Base>
112 class Cloneable<T, Base> : public Base
113 {
114 public:
116  std::unique_ptr<T> clone() const
117  {
118  return std::unique_ptr<T>{static_cast<T*>(this->do_clone_())};
119  }
120 
121 protected:
122  using Base::Base;
123 
124 private:
126  virtual Cloneable* do_clone_() const override
127  {
128  return new T(static_cast<T const&>(*this));
129  }
130 }; // class Cloneable
131 
133 template <typename T>
134 class Cloneable<T>
135 {
136 public:
137  virtual ~Cloneable() = default;
138 
139  std::unique_ptr<T> clone() const
140  {
141  return std::unique_ptr<T>{static_cast<T*>(this->do_clone_())};
142  }
143 
144 private:
145  Cloneable* do_clone_() const { return new T(static_cast<T const&>(*this)); }
146 }; // class Cloneable<T>
147 
158 template <typename T, typename... Base>
159 class Cloneable<HasAbstractFunction<T>, Base...> : public Base...
160 {
161 public:
162  std::unique_ptr<T> clone() const
163  {
164  return std::unique_ptr<T>{static_cast<T*>(this->do_clone_())};
165  }
166 
167 private:
168  virtual Cloneable* do_clone_() const = 0;
169 };
170 
171 template <typename T, typename Base>
172 class Cloneable<HasAbstractFunction<T>, Base> : public Base
173 {
174 public:
175  std::unique_ptr<T> clone() const
176  {
177  return std::unique_ptr<T>{static_cast<T*>(this->do_clone_())};
178  }
179 
180 protected:
181  using Base::Base;
182 
183 private:
184  virtual Cloneable* do_clone_() const = 0;
185 };
186 
188 template <typename T>
190 {
191 public:
192  virtual ~Cloneable() = default;
193 
194  std::unique_ptr<T> clone() const
195  {
196  return std::unique_ptr<T>{static_cast<T*>(this->do_clone_())};
197  }
198 
199 private:
200  virtual Cloneable* do_clone_() const = 0;
201 }; // class Cloneable<T>
202 
218 template <typename T>
220 
221 #ifndef DOXYGEN_SHOULD_SKIP_THIS
222 // The obvious case; I'd be concerned if this were ever called.
223 template <typename... Ts>
224 struct IsCloneableT<Cloneable<Ts...>> : std::true_type
225 {
226 };
227 
228 namespace details {
229 
230 struct definitely_not_a_unique_ptr;
231 
232 template <typename T>
233 auto has_right_clone(T const& x) -> decltype(x.clone());
234 
235 definitely_not_a_unique_ptr has_right_clone(...);
236 
237 } // namespace details
238 
239 template <typename T>
240 struct IsCloneableT
241  : std::is_same<decltype(details::has_right_clone(std::declval<T>())),
242  std::unique_ptr<T>>
243 {
244 };
245 #endif // DOXYGEN_SHOULD_SKIP_THIS
246 
247 template <typename T>
248 constexpr bool IsCloneable_v()
249 {
250  return IsCloneableT<T>::value;
251 };
252 
253 template <typename T>
254 inline constexpr bool IsCloneable = IsCloneable_v<T>();
255 
256 template <typename T>
257 struct IsCloneablePtrT : std::false_type
258 {
259 };
260 
261 template <typename T>
263 {
264 };
265 
266 template <typename T>
267 struct IsCloneablePtrT<std::shared_ptr<T>> : IsCloneableT<std::remove_cv_t<T>>
268 {
269 };
270 
271 template <typename T, typename DeleterT>
272 struct IsCloneablePtrT<std::unique_ptr<T, DeleterT>>
273  : IsCloneableT<std::remove_cv_t<T>>
274 {
275 };
276 
277 template <typename T>
278 constexpr bool IsCloneablePtr_v()
279 {
281 }
282 
283 template <typename T>
284 inline constexpr bool IsCloneablePtr = IsCloneablePtr_v<T>();
285 
289 template <typename T, typename... Bases>
291 
292 template <typename CloneablePtrT,
293  h2::meta::EnableWhen<IsCloneablePtr<CloneablePtrT>, int> = 1>
294 auto clone_all(std::vector<CloneablePtrT> const& things)
295 {
296  std::vector<CloneablePtrT> cloned_things;
297  cloned_things.reserve(things.size());
298  for (auto const& t : things) {
299  if (t)
300  cloned_things.emplace_back(t->clone());
301  else
302  cloned_things.emplace_back(nullptr);
303  }
304  return cloned_things;
305 }
306 
307 } // namespace lbann
308 #endif // LBANN_UTILS_CLONEABLE_HPP_INCLUDED
Inject polymorphic clone functions into hierarchies.
Definition: cloneable.hpp:94
std::unique_ptr< T > clone() const
Definition: cloneable.hpp:194
constexpr bool IsCloneablePtr_v()
Definition: cloneable.hpp:278
Cloneable * do_clone_() const
Definition: cloneable.hpp:145
std::unique_ptr< T > clone() const
Return an exception-safe, memory-safe copy of this object.
Definition: cloneable.hpp:98
Declare that T has unimplemented virtual functions.
Definition: cloneable.hpp:70
Declare Base to be a virtual base.
Definition: cloneable.hpp:55
auto clone_all(std::vector< CloneablePtrT > const &things)
Definition: cloneable.hpp:294
constexpr bool IsCloneable
Definition: cloneable.hpp:254
constexpr bool IsCloneablePtr
Definition: cloneable.hpp:284
std::unique_ptr< T > clone() const
Definition: cloneable.hpp:139
virtual Cloneable * do_clone_() const override
Implement the covariant raw-pointer-based clone operation.
Definition: cloneable.hpp:105
virtual Cloneable * do_clone_() const override
Implement the covariant raw-pointer-based clone operation.
Definition: cloneable.hpp:126
std::unique_ptr< T > clone() const
Return an exception-safe, memory-safe copy of this object.
Definition: cloneable.hpp:116
constexpr bool IsCloneable_v()
Definition: cloneable.hpp:248
Predicate testing for Cloneable interface.
Definition: cloneable.hpp:219