NSVD Reader  0.0.1
box.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 #ifndef __NODAMUSHI_BOX_HPP__
10 #define __NODAMUSHI_BOX_HPP__
11 
12 # include <type_traits>
13 # include <memory>
14 namespace nodamushi{
15 
20 template<typename T,typename D = std::default_delete<T>>
21 struct box
22 {
23  T* get() noexcept{return ptr.get();}
24  const T* get() const noexcept{return ptr.get();}
25 
26  typename std::add_lvalue_reference<typename std::add_const<T>::type>::type operator*()const{return *ptr;}
27  typename std::add_lvalue_reference<T>::type operator*() {return *ptr;}
28 
29  const T* operator->() const noexcept{return ptr.operator->();}
30  T* operator->() noexcept{return ptr.operator->();}
31 
32  constexpr box()noexcept
33  :ptr(){}
34  explicit box(T* p)noexcept
35  :ptr(p) {}
36  box(T* p, const D& d1)noexcept
37  :ptr(p,d1){}
38  box(T* p, D&& d2)noexcept
39  :ptr(p,std::move(d2)){}
40  box(std::unique_ptr<T,D>&& u)noexcept
41  :ptr(std::move(u)){}
42  box(box<T,D>&& u)noexcept
43  :ptr(std::move(u.ptr)){}
44 
45  constexpr box(std::nullptr_t n)noexcept
46  : ptr(n){}
47  box(const box&) = delete;
48 
49  box& operator=(box&& u) noexcept{
50  ptr=std::move(u.ptr);
51  return *this;
52  }
53  template <class U, class E>
54  box& operator=(box<U, E>&& u) noexcept{
55  ptr=std::move(u.ptr);
56  return *this;
57  }
58  box& operator=(std::nullptr_t p) noexcept{
59  ptr = p;
60  return *this;
61  }
62  box& operator=(const box&) = delete;
63 
64  T* release()noexcept{return ptr.release();}
65  void reset(T* p=nullptr)noexcept{ptr.reset(p);}
66  template<class U> void reset(U)=delete;
67  template<typename T2,typename D2>
68  void swap(std::unique_ptr<T2,D2>& x) noexcept{ptr.swap(x);}
69  template<typename T2,typename D2>
70  void swap(box<T2,D2>& x) noexcept{ptr.swap(x.ptr);}
71 
72 
73  D& get_deleter() noexcept{return ptr.get_deleter();}
74  const D& get_deleter() const noexcept{return ptr.get_deleter();}
75  explicit operator bool() const noexcept{return static_cast<bool>(ptr);}
76 
77  private:
78  std::unique_ptr<T,D> ptr;
79 };
80 
81 template<typename T>
82 const T& unboxing(const T& t){return t;}
83 template<typename T,typename D>
84 const T& unboxing(const box<T,D>& t){return *t;}
85 
86 }// end namespace nodamushi
87 
88 #endif // __NODAMUSHI_BOX_HPP__
const D & get_deleter() const noexcept
Definition: box.hpp:74
const T * get() const noexcept
Definition: box.hpp:24
box(T *p) noexcept
Definition: box.hpp:34
constexpr box() noexcept
Definition: box.hpp:32
const T * operator->() const noexcept
Definition: box.hpp:29
box & operator=(box &&u) noexcept
Definition: box.hpp:49
box(T *p, const D &d1) noexcept
Definition: box.hpp:36
constexpr box(std::nullptr_t n) noexcept
Definition: box.hpp:45
std::add_lvalue_reference< typename std::add_const< T >::type >::type operator *() const
Definition: box.hpp:26
T * get() noexcept
Definition: box.hpp:23
const T & unboxing(const T &t)
Definition: box.hpp:82
box(T *p, D &&d2) noexcept
Definition: box.hpp:38
box(std::unique_ptr< T, D > &&u) noexcept
Definition: box.hpp:40
T * release() noexcept
Definition: box.hpp:64
box(box< T, D > &&u) noexcept
Definition: box.hpp:42
void reset(T *p=nullptr) noexcept
Definition: box.hpp:65
box & operator=(std::nullptr_t p) noexcept
Definition: box.hpp:58
void swap(box< T2, D2 > &x) noexcept
Definition: box.hpp:70
D & get_deleter() noexcept
Definition: box.hpp:73
void swap(std::unique_ptr< T2, D2 > &x) noexcept
Definition: box.hpp:68
T * operator->() noexcept
Definition: box.hpp:30
box & operator=(box< U, E > &&u) noexcept
Definition: box.hpp:54