root/core/unique_ptr_impl.hpp

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. release
  2. reset
  3. swap
  4. swap
  5. swap
  6. get
  7. get
  8. to_string

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/core/unique_ptr_impl.hpp 2055 2017-05-13 19:35:47Z jmmcg $
   3 **
   4 ** Copyright © 2015 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #include <boost/mpl/assert.hpp>
  22 
  23 namespace jmmcg {
  24 
  25         template<class V, class LkT> inline typename unique_ptr<V, LkT>::atomic_ptr_t
  26         unique_ptr<V, LkT>::release() noexcept(true) {
  27                 atomic_ptr_t tmp(nullptr);
  28                 tmp.swap(this->data_);
  29                 return tmp;
  30         }
  31 
  32         template<class V, class LkT> inline void
  33         unique_ptr<V, LkT>::reset() noexcept(true) {
  34                 unique_ptr tmp;
  35                 tmp.swap(*this);
  36         }
  37 
  38         template<class V, class LkT> inline void
  39         unique_ptr<V, LkT>::swap(unique_ptr &s) noexcept(true) {
  40                 data_.swap(s.data_);
  41         }
  42 
  43         template<class V, class LkT>
  44         template<class V1> inline void
  45         unique_ptr<V, LkT>::swap(unique_ptr<V1, LkT> &s) noexcept(true) {
  46                 BOOST_MPL_ASSERT((std::is_base_of<value_type, V1>));
  47                 data_.swap(s.data_);
  48         }
  49 
  50         template<class V, class LkT>
  51         template<class V2, class LkT2> inline void
  52         unique_ptr<V, LkT>::swap(unique_ptr<V2, LkT2> &s) noexcept(true) {
  53                 BOOST_MPL_ASSERT((std::is_base_of<value_type, V2>));
  54                 data_.swap(s.data_);
  55         }
  56 
  57         template<class V, class LkT> inline constexpr
  58         unique_ptr<V, LkT>::unique_ptr(value_type *ptr) noexcept(true)
  59         : data_(ptr) {
  60         }
  61 
  62         template<class V, class LkT>
  63         template<class V1>
  64         inline
  65         unique_ptr<V, LkT>::unique_ptr(V1 *ptr) noexcept(true)
  66         : data_(ptr) {
  67                 BOOST_MPL_ASSERT((std::is_base_of<value_type, V1>));
  68         }
  69 
  70         template<class V, class LkT> inline
  71         unique_ptr<V, LkT>::unique_ptr(atomic_ptr_t &&ptr) noexcept(true)
  72         : data_(nullptr) {
  73                 BOOST_MPL_ASSERT((std::is_base_of<value_type, typename atomic_ptr_t::value_type>));
  74                 data_.swap(ptr);
  75         }
  76 
  77         template<class V, class LkT>
  78         template<class V1, template<class> class At>
  79         inline
  80         unique_ptr<V, LkT>::unique_ptr(At<V1*> &&ptr) noexcept(true)
  81         : data_(nullptr) {
  82                 BOOST_MPL_ASSERT((std::is_base_of<value_type, V1>));
  83                 if (LIKELY(ptr.get())) {
  84                         assert(dynamic_cast<V1 *>(ptr.get()));
  85                         data_.swap(ptr);
  86                         assert(dynamic_cast<value_type *>(data_.get()));
  87                 }
  88         }
  89 
  90         template<class V, class LkT> inline constexpr
  91         unique_ptr<V, LkT>::unique_ptr(std::unique_ptr<value_type, deleter_t> &&d) noexcept(true)
  92         : data_(d.release()) {
  93         }
  94 
  95         template<class V, class LkT>
  96         template<class V1>
  97         inline
  98         unique_ptr<V, LkT>::unique_ptr(std::unique_ptr<V1, typename V1::deleter_t> &&d) noexcept(true)
  99         : data_(d.release()) {
 100                 BOOST_MPL_ASSERT((std::is_base_of<value_type, V1>));
 101                 // I want to test that if the dtor is dynamic alloc, then the V1::element_type is derived from value_type, otherwise if placement, the dtor must be the same, otherwise if noop_dtor, don't care.
 102 // This is too stringent a test, as it ignores virtual dtors:           BOOST_MPL_ASSERT((std::is_same<deleter_t, typename V1::deleter_t>));
 103 //              static_assert(std::is_base_of<deleter_t, typename V1::deleter_t>::value || std::is_base_of<typename V1::deleter_t, deleter_t>::value, "TODO");
 104         }
 105 
 106         template<class V, class LkT> inline constexpr
 107         unique_ptr<V, LkT>::unique_ptr(unique_ptr &&s) noexcept(true)
 108         : data_(s.release()) {
 109         }
 110 
 111         template<class V, class LkT>
 112         template<typename V2, class LkT2> inline
 113         unique_ptr<V, LkT>::unique_ptr(unique_ptr<V2, LkT2> &&s) noexcept(true)
 114         : data_(s.release()) {
 115                 BOOST_MPL_ASSERT((std::is_base_of<value_type, V2>));
 116         }
 117 
 118         template<class V, class LkT> inline
 119         unique_ptr<V, LkT>::~unique_ptr() noexcept(true) {
 120                 atomic_ptr_t tmp(nullptr);
 121                 tmp.swap(this->data_);
 122                 if (tmp) {
 123                         tmp->deleter();
 124                 }
 125         }
 126 
 127         template<class V, class LkT>
 128         template<typename V2, class LkT2> inline void
 129         unique_ptr<V, LkT>::operator=(unique_ptr<V2, LkT2> &&s) noexcept(true) {
 130                 BOOST_MPL_ASSERT((std::is_base_of<value_type, V2>));
 131                 reset();
 132                 data_=s.release();
 133         }
 134 
 135         template<class V, class LkT> inline void
 136         unique_ptr<V, LkT>::operator=(unique_ptr<V, LkT> &&s) noexcept(true) {
 137                 reset();
 138                 data_=s.release();
 139         }
 140 
 141         template<class V, class LkT> constexpr inline bool
 142         unique_ptr<V, LkT>::operator<(const unique_ptr &s) const noexcept(true) {
 143                 return (data_.get() && s.data_.get()) ? (*data_.get()<*s.data_.get()) : false;
 144         }
 145         template<class V, class LkT> constexpr inline bool
 146         unique_ptr<V, LkT>::operator==(const unique_ptr &s) const noexcept(true) {
 147                 return !((data_<s.data_) || (s.data_<data_));
 148         }
 149         template<class V, class LkT> inline constexpr bool
 150         unique_ptr<V, LkT>::operator!=(const unique_ptr &s) const noexcept(true) {
 151                 return !(*this==s);
 152         }
 153         template<class V, class LkT> constexpr inline
 154         unique_ptr<V, LkT>::operator bool() const noexcept(true) {
 155                 return dynamic_cast<value_type const *>(data_.get())!=nullptr;
 156         }
 157         template<class V, class LkT> constexpr inline bool
 158         unique_ptr<V, LkT>::operator>(const unique_ptr &s) const noexcept(true) {
 159                 return (data_!=s.data_) && (s.data_<data_);
 160         }
 161 
 162         template<class V, class LkT> constexpr inline typename unique_ptr<V, LkT>::atomic_ptr_t const &
 163         unique_ptr<V, LkT>::get() const noexcept(true) {
 164                 return data_;
 165         }
 166         template<class V, class LkT> inline typename unique_ptr<V, LkT>::atomic_ptr_t &
 167         unique_ptr<V, LkT>::get() noexcept(true) {
 168                 return data_;
 169         }
 170         template<class V, class LkT> constexpr inline typename unique_ptr<V, LkT>::value_type const &
 171         unique_ptr<V, LkT>::operator*() const noexcept(true) {
 172                 assert(dynamic_cast<value_type const *>(data_.get()));
 173                 return *data_.get();
 174         }
 175         template<class V, class LkT> inline typename unique_ptr<V, LkT>::value_type &
 176         unique_ptr<V, LkT>::operator*() noexcept(true) {
 177                 assert(dynamic_cast<value_type *>(data_.get()));
 178                 return *data_.get();
 179         }
 180         template<class V, class LkT> constexpr inline typename unique_ptr<V, LkT>::value_type const *
 181         unique_ptr<V, LkT>::operator->() const noexcept(true) {
 182                 return data_.get();
 183         }
 184         template<class V, class LkT> inline typename unique_ptr<V, LkT>::value_type *
 185         unique_ptr<V, LkT>::operator->() noexcept(true) {
 186                 return data_.get();
 187         }
 188 
 189         template<class V, class LkT> inline tstring
 190         unique_ptr<V, LkT>::to_string() const noexcept(false) {
 191                 tostringstream os;
 192                 os<<"data_="<<data_.get();
 193                 if (LIKELY(dynamic_cast<value_type *>(data_.get()))) {
 194                         os<<", data: "<<data_->to_string()
 195                                 <<", type: "<<typeid(unique_ptr).name();
 196                 }
 197                 return os.str();
 198         }
 199 
 200 }

/* [<][>][^][v][top][bottom][index][help] */