Medusa  1.1
Coordinate Free Mehless Method implementation
memutils.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_UTILS_MEMUTILS_HPP_
2 #define MEDUSA_BITS_UTILS_MEMUTILS_HPP_
3 
11 #include <memory>
12 #include <medusa/Config.hpp>
14 
15 namespace mm {
16 
29 template <typename T>
30 class deep_copy_unique_ptr : public std::unique_ptr<T, std::default_delete<T>> {
31  public:
32  using std::unique_ptr<T, std::default_delete<T>>::unique_ptr;
33  using std::unique_ptr<T, std::default_delete<T>>::operator=;
34  using std::unique_ptr<T, std::default_delete<T>>::reset;
35 
37  explicit deep_copy_unique_ptr(const T& v) :
38  std::unique_ptr<T, std::default_delete<T>>(v.clone()) {}
39 
42  std::unique_ptr<T, std::default_delete<T>>() {
43  reset(o ? o->clone() : nullptr);
44  }
45 
48  std::unique_ptr<T, std::default_delete<T>>(std::move(o)) { }
49 
52  reset(v.clone());
53  return *this;
54  }
55 
58  reset(o ? o->clone() : nullptr);
59  return *this;
60  }
61 
64  std::unique_ptr<T, std::default_delete<T>>::operator=(std::move(o));
65  return *this;
66  }
67 };
68 
74 std::string mem2str(std::size_t bytes);
75 
82 template<typename container_t>
83 std::size_t mem_used(const container_t& v) {
84  return sizeof(v[0]) * v.size();
85 }
86 
87 } // namespace mm
88 
89 #endif // MEDUSA_BITS_UTILS_MEMUTILS_HPP_
mm::deep_copy_unique_ptr::deep_copy_unique_ptr
deep_copy_unique_ptr(const T &v)
Construct by polymorphically cloning a given value.
Definition: memutils.hpp:37
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::deep_copy_unique_ptr::operator=
deep_copy_unique_ptr & operator=(const deep_copy_unique_ptr &o)
Copy assign by cloning the value of o.
Definition: memutils.hpp:57
Config.hpp
assert.hpp
mm::deep_copy_unique_ptr::operator=
deep_copy_unique_ptr & operator=(const T &v)
Copy assign by cloning a given value.
Definition: memutils.hpp:51
mm::deep_copy_unique_ptr::deep_copy_unique_ptr
deep_copy_unique_ptr(const deep_copy_unique_ptr &o)
Copy by cloning the value of o.
Definition: memutils.hpp:41
mm::deep_copy_unique_ptr::deep_copy_unique_ptr
deep_copy_unique_ptr(deep_copy_unique_ptr &&o) noexcept
Move construct as unique_ptr.
Definition: memutils.hpp:47
mm::mem_used
std::size_t mem_used(const container_t &v)
Returns number of bytes the container uses in memory.
Definition: memutils.hpp:83
mm::deep_copy_unique_ptr::operator=
deep_copy_unique_ptr & operator=(deep_copy_unique_ptr &&o) noexcept
Move assign as unique_ptr
Definition: memutils.hpp:63
mm::deep_copy_unique_ptr
Unique pointer with polymorphic deep copy semantics.
Definition: DomainShape_fwd.hpp:22
mm::mem2str
std::string mem2str(std::size_t bytes)
Simple function to help format memory amounts for printing.
Definition: memutils.cpp:10