/* Copyright 2019 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_SMART_PTR_ALLOCATION_PTR_HPP #define BOOST_SMART_PTR_ALLOCATION_PTR_HPP #include #include #include #if !defined(BOOST_NO_CXX11_ALLOCATOR) #include #endif namespace boost { namespace detail { template struct sp_allocation_ptr { #if !defined(BOOST_NO_CXX11_ALLOCATOR) typedef typename std::allocator_traits::template rebind_traits::pointer type; #else typedef typename A::template rebind::other::pointer type; #endif }; } /* detail */ template class allocation_ptr { public: typedef typename detail::sp_allocation_ptr::type pointer; typedef T element_type; allocation_ptr() BOOST_SP_NOEXCEPT : p_() { } #if !defined(BOOST_NO_CXX11_NULLPTR) allocation_ptr(detail::sp_nullptr_t) BOOST_SP_NOEXCEPT : p_() { } #endif explicit allocation_ptr(pointer p) BOOST_SP_NOEXCEPT : p_(p) { } T& operator*() const { return *p_; } pointer operator->() const BOOST_SP_NOEXCEPT { return p_; } pointer get() const BOOST_SP_NOEXCEPT { return p_; } #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) explicit operator bool() const BOOST_SP_NOEXCEPT { return static_cast(p_); } #endif bool operator!() const BOOST_SP_NOEXCEPT { return !p_; } private: pointer p_; }; template class allocation_ptr { public: typedef typename detail::sp_allocation_ptr::type pointer; typedef T element_type; allocation_ptr() BOOST_SP_NOEXCEPT : p_() , n_() { } #if !defined(BOOST_NO_CXX11_NULLPTR) allocation_ptr(detail::sp_nullptr_t) BOOST_SP_NOEXCEPT : p_() , n_() { } #endif allocation_ptr(pointer p, std::size_t n) BOOST_SP_NOEXCEPT : p_(p) , n_(n) { } T& operator[](std::size_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT { BOOST_ASSERT(i < n_); return p_[i]; } pointer get() const BOOST_SP_NOEXCEPT { return p_; } std::size_t size() const BOOST_SP_NOEXCEPT { return n_; } #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) explicit operator bool() const BOOST_SP_NOEXCEPT { return static_cast(p_); } #endif bool operator!() const BOOST_SP_NOEXCEPT { return !p_; } private: pointer p_; std::size_t n_; }; template class allocation_ptr { public: typedef typename detail::sp_allocation_ptr::type pointer; typedef T element_type; allocation_ptr() BOOST_SP_NOEXCEPT : p_() { } #if !defined(BOOST_NO_CXX11_NULLPTR) allocation_ptr(detail::sp_nullptr_t) BOOST_SP_NOEXCEPT : p_() { } #endif explicit allocation_ptr(pointer p) BOOST_SP_NOEXCEPT : p_(p) { } T& operator[](std::size_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT { BOOST_ASSERT(i < N); return p_[i]; } pointer get() const BOOST_SP_NOEXCEPT { return p_; } static std::size_t size() BOOST_SP_NOEXCEPT { return N; } #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) explicit operator bool() const BOOST_SP_NOEXCEPT { return static_cast(p_); } #endif bool operator!() const BOOST_SP_NOEXCEPT { return !p_; } private: pointer p_; }; template inline bool operator==(const allocation_ptr& lhs, const allocation_ptr& rhs) { return lhs.get() == rhs.get(); } template inline bool operator!=(const allocation_ptr& lhs, const allocation_ptr& rhs) { return !(lhs == rhs); } #if !defined(BOOST_NO_CXX11_NULLPTR) template inline bool operator==(const allocation_ptr& lhs, detail::sp_nullptr_t) BOOST_SP_NOEXCEPT { return !lhs.get(); } template inline bool operator==(detail::sp_nullptr_t, const allocation_ptr& rhs) BOOST_SP_NOEXCEPT { return !rhs.get(); } template inline bool operator!=(const allocation_ptr& lhs, detail::sp_nullptr_t) BOOST_SP_NOEXCEPT { return static_cast(lhs.get()); } template inline bool operator!=(detail::sp_nullptr_t, const allocation_ptr& rhs) BOOST_SP_NOEXCEPT { return static_cast(rhs.get()); } #endif } /* boost */ #endif