Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/locale/hold_ptr.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // Copyright (c) 2010 Artyom Beilis (Tonkikh)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // https://www.boost.org/LICENSE_1_0.txt
0006 
0007 #ifndef BOOST_LOCALE_HOLD_PTR_H
0008 #define BOOST_LOCALE_HOLD_PTR_H
0009 
0010 #include <boost/locale/config.hpp>
0011 #include <boost/core/exchange.hpp>
0012 
0013 namespace boost { namespace locale {
0014     /// \brief a smart pointer similar to std::unique_ptr but the
0015     /// underlying object has the same constness as the pointer itself (unlike an ordinary pointer).
0016     template<typename T>
0017     class hold_ptr {
0018     public:
0019         /// Create new empty pointer
0020         hold_ptr() : ptr_(nullptr) {}
0021 
0022         /// Create a pointer that holds \a v, ownership is transferred to smart pointer
0023         explicit hold_ptr(T* v) : ptr_(v) {}
0024 
0025         /// Destroy smart pointer and the object it owns.
0026         ~hold_ptr() { delete ptr_; }
0027 
0028         // Non-copyable
0029         hold_ptr(const hold_ptr&) = delete;
0030         hold_ptr& operator=(const hold_ptr&) = delete;
0031         // Movable
0032         hold_ptr(hold_ptr&& other) noexcept : ptr_(exchange(other.ptr_, nullptr)) {}
0033         hold_ptr& operator=(hold_ptr&& other) noexcept
0034         {
0035             swap(other);
0036             return *this;
0037         }
0038 
0039         /// Get a const pointer to the object
0040         T const* get() const { return ptr_; }
0041         /// Get a mutable pointer to the object
0042         T* get() { return ptr_; }
0043 
0044         /// Explicitly convertible to bool. Returns: get() != nullptr
0045         explicit operator bool() const { return ptr_ != nullptr; }
0046 
0047         /// Get a const reference to the object
0048         T const& operator*() const { return *ptr_; }
0049         /// Get a mutable reference to the object
0050         T& operator*() { return *ptr_; }
0051 
0052         /// Get a const pointer to the object
0053         T const* operator->() const { return ptr_; }
0054         /// Get a mutable pointer to the object
0055         T* operator->() { return ptr_; }
0056 
0057         /// Transfer ownership of the pointer to user
0058         T* release() { return exchange(ptr_, nullptr); }
0059 
0060         /// Set new value to pointer, previous object is destroyed, ownership of new object is transferred
0061         void reset(T* p = nullptr)
0062         {
0063             if(ptr_)
0064                 delete ptr_;
0065             ptr_ = p;
0066         }
0067 
0068         /// Swap two pointers
0069         void swap(hold_ptr& other) noexcept { ptr_ = exchange(other.ptr_, ptr_); }
0070 
0071     private:
0072         T* ptr_;
0073     };
0074 
0075 }} // namespace boost::locale
0076 
0077 #endif