copyable_unique_ptr.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 /* Portions copyright (c) 2015 Stanford University and the Authors.
4  Authors: Michael Sherman
5 
6 Licensed under the Apache License, Version 2.0 (the "License"); you may
7 not use this file except in compliance with the License. You may obtain a
8 copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
9 
10 (Adapted from Simbody's ClonePtr class.)
11 (Adapted from Drake copyable_unique_ptr class.)
12  */
13 
14 #include <cstddef>
15 #include <iostream>
16 #include <memory>
17 #include <utility>
18 
19 namespace dai {
20 
21 // TODO(SeanCurtis-TRI): Consider extending this to add the Deleter as well.
103 template <typename T>
104 class copyable_unique_ptr : public std::unique_ptr<T> {
105  public:
112  copyable_unique_ptr() noexcept : std::unique_ptr<T>() {}
113 
116  explicit copyable_unique_ptr(T* raw) noexcept : std::unique_ptr<T>(raw) {}
117 
120  explicit copyable_unique_ptr(const T& value) : std::unique_ptr<T>(CopyOrNull(&value)) {}
121 
126  copyable_unique_ptr(const copyable_unique_ptr& cu_ptr) : std::unique_ptr<T>(CopyOrNull(cu_ptr.get())) {}
127 
133  template <typename U>
134  explicit copyable_unique_ptr(const std::unique_ptr<U>& u_ptr) : std::unique_ptr<T>(CopyOrNull(u_ptr.get())) {}
135 
139  copyable_unique_ptr(copyable_unique_ptr&& cu_ptr) noexcept : std::unique_ptr<T>(cu_ptr.release()) {}
140 
145  explicit copyable_unique_ptr(std::unique_ptr<T>&& u_ptr) noexcept : std::unique_ptr<T>(u_ptr.release()) {}
146 
151  template <typename U>
152  explicit copyable_unique_ptr(std::unique_ptr<U>&& u_ptr) noexcept : std::unique_ptr<T>(u_ptr.release()) {}
153 
162  copyable_unique_ptr& operator=(T* raw) noexcept {
164  return *this;
165  }
166 
173  return *this;
174  }
175 
183  return operator=(static_cast<const std::unique_ptr<T>&>(cu_ptr));
184  }
185 
192  template <typename U>
194  return operator=(static_cast<const std::unique_ptr<U>&>(cu_ptr));
195  }
196 
203  copyable_unique_ptr& operator=(const std::unique_ptr<T>& src) {
204  if(&src != this) {
205  // can't be same ptr unless null
206  DRAKE_DEMAND((get() != src.get()) || !get());
208  }
209  return *this;
210  }
211 
218  template <typename U>
219  copyable_unique_ptr& operator=(const std::unique_ptr<U>& u_ptr) {
220  // can't be same ptr unless null
221  DRAKE_DEMAND((get() != u_ptr.get()) || !get());
223  return *this;
224  }
225 
231  std::unique_ptr<T>::reset(cu_ptr.release());
232  return *this;
233  }
234 
239  template <typename U>
241  std::unique_ptr<T>::reset(cu_ptr.release());
242  return *this;
243  }
244 
249  copyable_unique_ptr& operator=(std::unique_ptr<T>&& u_ptr) noexcept {
250  std::unique_ptr<T>::reset(u_ptr.release());
251  return *this;
252  }
253 
258  template <typename U>
259  copyable_unique_ptr& operator=(std::unique_ptr<U>&& u_ptr) noexcept {
260  std::unique_ptr<T>::reset(u_ptr.release());
261  return *this;
262  }
263 
272  bool empty() const noexcept {
273  return !(*this);
274  }
275 
280  const T* get() const noexcept {
281  return std::unique_ptr<T>::get();
282  }
283 
284  // TODO(SeanCurtis-TRI): Consider adding some debug assertions about whether
285  // T is const or not. If so, it would be nice to give feedback that calling
286  // the mutable version makes no sense.
294  T* get_mutable() noexcept {
295  return std::unique_ptr<T>::get();
296  }
297 
298  // TODO(15344) We need to shore up this const correctness hole. Rather than an
299  // is-a relationship, we need some alternative relationship that will provide
300  // the same functionality but not be upcastable. One possibility is to own
301  // an unique_ptr and forward various APIs. Another is to implement from
302  // scratch. The current "is-A" relationship was intended so that the
303  // copyable_unique_ptr could be used where unique_ptrs are used. What would
304  // the impact of such a change in the relationship be to Drake and Drake
305  // users?
306 
320  const T& operator*() const {
321  return *get();
322  }
323 
340  T& operator*() {
341  return *get_mutable();
342  }
343 
345  private:
346  template <class U>
347  static T* CopyOrClone(const T* raw, std::true_type) {
348  return raw->clone().release();
349  }
350 
351  template <class U>
352  static T* CopyOrClone(const T* raw, std::false_type) {
353  return new T(*raw);
354  }
355 
356  // If src is non-null, clone it; otherwise return nullptr.
357  // If both can_copy() and can_clone() return true, we will prefer the Clone()
358  // function over the copy constructor.
359  // The caller has ownership over the return value.
360  static T* CopyOrNull(const T* raw) {
361  if(raw == nullptr) {
362  return nullptr;
363  }
364 
365  return CopyOrClone<T>(
366  raw, std::integral_constant<bool, std::is_same<decltype(std::declval<const T>().clone()), std::unique_ptr<std::remove_const_t<T>>>::value>{});
367  }
368 };
369 
373 template <class charT, class traits, class T>
374 std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const copyable_unique_ptr<T>& cu_ptr) {
375  os << cu_ptr.get();
376  return os;
377 }
378 
379 } // namespace dai
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(const copyable_unique_ptr< U > &cu_ptr)
Definition: copyable_unique_ptr.hpp:193
operator<<
std::ostream & operator<<(std::ostream &out, const dai::CameraBoardSocket &socket)
Definition: include/depthai/common/CameraBoardSocket.hpp:8
dai::copyable_unique_ptr::get_mutable
T * get_mutable() noexcept
Definition: copyable_unique_ptr.hpp:294
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr() noexcept
Definition: copyable_unique_ptr.hpp:112
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr(const std::unique_ptr< U > &u_ptr)
Definition: copyable_unique_ptr.hpp:134
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr(std::unique_ptr< T > &&u_ptr) noexcept
Definition: copyable_unique_ptr.hpp:145
DAI_SPAN_NAMESPACE_NAME::get
constexpr auto get(span< E, S > s) -> decltype(s[N])
Definition: span.hpp:491
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr(const copyable_unique_ptr &cu_ptr)
Definition: copyable_unique_ptr.hpp:126
dai::copyable_unique_ptr::operator*
const T & operator*() const
Definition: copyable_unique_ptr.hpp:320
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(std::unique_ptr< U > &&u_ptr) noexcept
Definition: copyable_unique_ptr.hpp:259
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(const std::unique_ptr< U > &u_ptr)
Definition: copyable_unique_ptr.hpp:219
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(copyable_unique_ptr< U > &&cu_ptr) noexcept
Definition: copyable_unique_ptr.hpp:240
dai::copyable_unique_ptr::CopyOrNull
static T * CopyOrNull(const T *raw)
Definition: copyable_unique_ptr.hpp:360
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr(const T &value)
Definition: copyable_unique_ptr.hpp:120
dai::Properties::clone
virtual std::unique_ptr< Properties > clone() const =0
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(T *raw) noexcept
Definition: copyable_unique_ptr.hpp:162
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(const copyable_unique_ptr &cu_ptr)
Definition: copyable_unique_ptr.hpp:182
dai::copyable_unique_ptr::CopyOrClone
static T * CopyOrClone(const T *raw, std::true_type)
Definition: copyable_unique_ptr.hpp:347
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(const T &ref)
Definition: copyable_unique_ptr.hpp:171
dai::copyable_unique_ptr::operator*
T & operator*()
Definition: copyable_unique_ptr.hpp:340
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(std::unique_ptr< T > &&u_ptr) noexcept
Definition: copyable_unique_ptr.hpp:249
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr(std::unique_ptr< U > &&u_ptr) noexcept
Definition: copyable_unique_ptr.hpp:152
dai::copyable_unique_ptr::empty
bool empty() const noexcept
Definition: copyable_unique_ptr.hpp:272
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(const std::unique_ptr< T > &src)
Definition: copyable_unique_ptr.hpp:203
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr(copyable_unique_ptr &&cu_ptr) noexcept
Definition: copyable_unique_ptr.hpp:139
std
Definition: Node.hpp:366
dai::copyable_unique_ptr::CopyOrClone
static T * CopyOrClone(const T *raw, std::false_type)
Definition: copyable_unique_ptr.hpp:352
reset
static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
Definition: sha1.hpp:55
dai::copyable_unique_ptr::copyable_unique_ptr
copyable_unique_ptr(T *raw) noexcept
Definition: copyable_unique_ptr.hpp:116
dai::copyable_unique_ptr::operator=
copyable_unique_ptr & operator=(copyable_unique_ptr &&cu_ptr) noexcept
Definition: copyable_unique_ptr.hpp:230
dai
Definition: CameraExposureOffset.hpp:6
dai::copyable_unique_ptr::get
const T * get() const noexcept
Definition: copyable_unique_ptr.hpp:280
dai::copyable_unique_ptr
Definition: copyable_unique_ptr.hpp:104


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19