Advertisement
Guest User

Untitled

a guest
Jul 21st, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #pragma once
  2. // #include <progrock/cpp/pointers/NullCheckingPtr.h>
  3. //
  4. // Formerly known as "ZPtr".
  5. // Copyright (c) 200x - 2012 Alf P. Steinbach
  6.  
  7.  
  8. //----------------------------------------------- Dependencies:
  9.  
  10. #include <progrock/cpp/classkinds/SelfDestroyingObject.h>   // cpp::DestructionEvent, cpp::SelfDestroyingObject
  11. #include <progrock/cpp/throwx.h>                            // cpp::hopefully, cpp::throwX
  12. #include <progrock/cpp/c++11_emulation.h>                   // CPP_NOEXCEPT
  13.  
  14. #include <algorithm>            // std::swap
  15. #include <stddef.h>             // std::nullptr_t
  16.  
  17.  
  18. //----------------------------------------------- Interface:
  19.  
  20. namespace progrock{ namespace cpp{
  21.     using std::swap;
  22.     using std::nullptr_t;
  23.  
  24.     template< class Pointee >
  25.     class NullCheckingPtr
  26.         : private DestructionEvent::Listener
  27.     {
  28.     private:
  29.         Pointee*    p_;
  30.  
  31.         virtual void on( DestructionEvent const& ) CPP_NOEXCEPT CPP_IS_OVERRIDE
  32.         {
  33.             p_ = nullptr;
  34.         }
  35.  
  36.         Pointee* goodPointer() const
  37.         {
  38.             hopefully( p_ != 0 )
  39.                 || throwX( "cpp::NullCheckingPtr: attempted access of non-existent pointee" );
  40.             return p_;
  41.         }
  42.  
  43.     public:
  44.         bool operator!() const CPP_NOEXCEPT { return (p_ != 0); }
  45.  
  46.         Pointee* operator->() const { return goodPointer(); }
  47.         Pointee& operator*() const { return *goodPointer(); }
  48.  
  49.         void swapWith( NullCheckingPtr& other )
  50.         {
  51.             swap( p_, other.p_ );
  52.         }
  53.  
  54.         void operator=( NullCheckingPtr other ) CPP_NOEXCEPT
  55.         {
  56.             swapWith( other );
  57.         }
  58.  
  59.         ~NullCheckingPtr()
  60.         {
  61.             if( p_ != 0 )
  62.             {
  63.                 p_->removeDestructionListener( *this );
  64.             }
  65.         }
  66.  
  67.         explicit NullCheckingPtr( Pointee* p = nullptr )
  68.             : p_( p )
  69.         {
  70.             if( p_ != 0 )
  71.             {
  72.                 p_->addDestructionListener( *this );
  73.             }
  74.         }
  75.  
  76.         NullCheckingPtr( NullCheckingPtr const& other )
  77.             : p_( other.p_ )
  78.         {
  79.             if( p_ != 0 )
  80.             {
  81.                 p_->addDestructionListener( *this );
  82.             }
  83.         }
  84.     };
  85.  
  86.     template< class Pointee >
  87.     inline bool operator==( NullCheckingPtr< Pointee > const& p, std::nullptr_t )
  88.         CPP_NOEXCEPT
  89.     {
  90.         return !p;
  91.     }
  92.  
  93.     template< class Pointee >
  94.     inline bool operator==( std::nullptr_t, NullCheckingPtr< Pointee > const& p )
  95.         CPP_NOEXCEPT
  96.     {
  97.         return !p;
  98.     }
  99.  
  100.     template< class Pointee >
  101.     inline bool operator!=( NullCheckingPtr< Pointee > const& p, std::nullptr_t )
  102.         CPP_NOEXCEPT
  103.     {
  104.         return !!p;
  105.     }
  106.  
  107.     template< class Pointee >
  108.     inline bool operator!=( std::nullptr_t, NullCheckingPtr< Pointee > const& p )
  109.         CPP_NOEXCEPT
  110.     {
  111.         return !!p;
  112.     }
  113.  
  114.     template< class Pointee >
  115.     inline void swap( NullCheckingPtr< Pointee >& a, NullCheckingPtr< Pointee >& b )
  116.         CPP_NOEXCEPT
  117.     {
  118.         a.swapWith( b );
  119.     }
  120. } }  // namespace progrock::cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement