Blame view

3rdparty/boost_1_81_0/boost/locale/hold_ptr.hpp 2.48 KB
63e88f80   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  //
  // Copyright (c) 2010 Artyom Beilis (Tonkikh)
  //
  // Distributed under the Boost Software License, Version 1.0.
  // https://www.boost.org/LICENSE_1_0.txt
  
  #ifndef BOOST_LOCALE_HOLD_PTR_H
  #define BOOST_LOCALE_HOLD_PTR_H
  
  #include <boost/locale/config.hpp>
  
  namespace boost { namespace locale {
      /// \brief a smart pointer similar to std::unique_ptr but the
      /// underlying object has the same constness as the pointer itself (unlike an ordinary pointer).
      template<typename T>
      class hold_ptr {
      public:
          /// Create new empty pointer
          hold_ptr() : ptr_(nullptr) {}
  
          /// Create a pointer that holds \a v, ownership is transferred to smart pointer
          explicit hold_ptr(T* v) : ptr_(v) {}
  
          /// Destroy smart pointer and the object it owns.
          ~hold_ptr() { delete ptr_; }
  
          // Non-copyable
          hold_ptr(const hold_ptr&) = delete;
          hold_ptr& operator=(const hold_ptr&) = delete;
          // Movable
          hold_ptr(hold_ptr&& other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; }
          hold_ptr& operator=(hold_ptr&& other) noexcept
          {
              swap(other);
              return *this;
          }
  
          /// Get a const pointer to the object
          T const* get() const { return ptr_; }
          /// Get a mutable pointer to the object
          T* get() { return ptr_; }
  
          /// Explicitly convertible to bool. Returns: get() != nullptr
          explicit operator bool() const { return ptr_ != nullptr; }
  
          /// Get a const reference to the object
          T const& operator*() const { return *ptr_; }
          /// Get a mutable reference to the object
          T& operator*() { return *ptr_; }
  
          /// Get a const pointer to the object
          T const* operator->() const { return ptr_; }
          /// Get a mutable pointer to the object
          T* operator->() { return ptr_; }
  
          /// Transfer an ownership on the pointer to user
          T* release()
          {
              T* tmp = ptr_;
              ptr_ = nullptr;
              return tmp;
          }
  
          /// Set new value to pointer, previous object is destroyed, ownership on new object is transferred
          void reset(T* p = nullptr)
          {
              if(ptr_)
                  delete ptr_;
              ptr_ = p;
          }
  
          /// Swap two pointers
          void swap(hold_ptr& other)
          {
              T* tmp = other.ptr_;
              other.ptr_ = ptr_;
              ptr_ = tmp;
          }
  
      private:
          T* ptr_;
      };
  
  }} // namespace boost::locale
  
  #endif