Advertisement
Delfigamer

utils/ref.hpp

Feb 24th, 2015
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #ifndef UTILS_REF_HPP__
  2. #define UTILS_REF_HPP__ 1
  3.  
  4. #include "refobject.hpp"
  5. #include <atomic>
  6. #include <stdexcept>
  7. #include <exception>
  8. #include <utility>
  9.  
  10. namespace utils {
  11.     class RefBase {
  12.     private:
  13.         std::atomic< RefObject* > m_ref;
  14.         RefObject* possess_silent( RefObject* ref ) noexcept;
  15.         RefObject* assign_silent( RefObject* ref ) noexcept;
  16.         RefObject* assign_silent( RefObject* ref, int id ) noexcept;
  17.        
  18.     public:
  19.         RefBase() noexcept;
  20.         RefBase( RefObject* ref, int ) noexcept;
  21.         RefBase( RefObject* ref ) noexcept;
  22.         RefBase( RefObject* ref, int, int id ) noexcept;
  23.         RefBase( RefBase&& other ) noexcept;
  24.         RefBase( RefBase const& other ) noexcept;
  25.         RefBase( RefBase const& other, int id ) noexcept;
  26.         ~RefBase() noexcept;
  27.         RefObject* possess( RefObject* ref ) noexcept;
  28.         RefObject* assign( RefObject* ref ) noexcept;
  29.         RefObject* assign( RefObject* ref, int id ) noexcept;
  30.         RefBase& assign( RefBase&& other ) noexcept;
  31.         RefBase& assign( RefBase const& other ) noexcept;
  32.         RefBase& assign( RefBase const& other, int id ) noexcept;
  33.         RefObject* get() const noexcept;
  34.         RefObject& deref() const;
  35.         bool operator==( RefBase const& other );
  36.         bool operator!=( RefBase const& other );
  37.     };
  38.    
  39.     template< typename T >
  40.     class Ref: public RefBase {
  41.     public:
  42.         Ref() noexcept;
  43.         Ref( nullptr_t ) noexcept;
  44.         Ref( T* ref ) noexcept;
  45.         Ref( RefObject* ref ) noexcept;
  46.         Ref( T* ref, int ) noexcept;
  47.         Ref( Ref const& other ) noexcept;
  48.         Ref( RefBase const& other ) noexcept;
  49.         Ref( Ref&& other ) noexcept;
  50.         ~Ref() noexcept;
  51.         Ref& operator=( nullptr_t ) noexcept;
  52.         Ref& operator=( T* ref ) noexcept;
  53.         Ref& operator=( RefObject* ref ) noexcept;
  54.         Ref& operator=( Ref const& other ) noexcept;
  55.         Ref& operator=( RefBase const& other ) noexcept;
  56.         Ref& operator=( Ref&& other ) noexcept;
  57.         T& operator*() const;
  58.         T* operator->() const;
  59.         operator T*() const noexcept;
  60.    
  61.         template< typename ...Ts >
  62.         static Ref create( Ts&& ...args );
  63.     };
  64.    
  65.     template<>
  66.     class Ref< RefObject >: public RefBase {
  67.     public:
  68.         Ref() noexcept;
  69.         Ref( RefObject* ref ) noexcept;
  70.         Ref( RefObject* ref, int ) noexcept;
  71.         Ref( Ref const& other ) noexcept;
  72.         Ref( RefBase const& other ) noexcept;
  73.         Ref( Ref&& other ) noexcept;
  74.         Ref( RefBase&& other ) noexcept;
  75.         ~Ref() noexcept;
  76.         Ref& operator=( RefObject* ref ) noexcept;
  77.         Ref& operator=( Ref const& other ) noexcept;
  78.         Ref& operator=( RefBase const& other ) noexcept;
  79.         Ref& operator=( Ref&& other ) noexcept;
  80.         Ref& operator=( RefBase&& other ) noexcept;
  81.         RefObject& operator*() const;
  82.         RefObject* operator->() const;
  83.         operator RefObject*() const noexcept;
  84.     };
  85.    
  86.     template< typename T >
  87.     Ref< T >::Ref() noexcept :
  88.         RefBase() {
  89.     }
  90.    
  91.     template< typename T >
  92.     Ref< T >::Ref( nullptr_t ) noexcept :
  93.         RefBase( ( RefObject* )0 ) {
  94.     }
  95.    
  96.     template< typename T >
  97.     Ref< T >::Ref( T* ref ) noexcept :
  98.         RefBase( ( RefObject* )ref ) {
  99.     }
  100.    
  101.     template< typename T >
  102.     Ref< T >::Ref( RefObject* ref ) noexcept :
  103.         RefBase( ref, 0, RefId< T >::id ) {
  104.     }
  105.    
  106.     template< typename T >
  107.     Ref< T >::Ref( T* ref, int ) noexcept :
  108.         RefBase( ( RefObject* )ref, 0 ) {
  109.     }
  110.    
  111.     template< typename T >
  112.     Ref< T >::Ref( Ref const& other ) noexcept :
  113.         RefBase( other ) {
  114.     }
  115.    
  116.     template< typename T >
  117.     Ref< T >::Ref( RefBase const& other ) noexcept :
  118.         RefBase( other, RefId< T >::id ) {
  119.     }
  120.    
  121.     template< typename T >
  122.     Ref< T >::Ref( Ref&& other ) noexcept :
  123.         RefBase( std::move( ( RefBase&& )other ) ) {
  124.     }
  125.    
  126.     template< typename T >
  127.     Ref< T >::~Ref() noexcept {
  128.     }
  129.    
  130.     template< typename T >
  131.     Ref< T >& Ref< T >::operator=( nullptr_t ) noexcept {
  132.         RefBase::assign( ( RefObject* )0 );
  133.         return *this;
  134.     }
  135.    
  136.     template< typename T >
  137.     Ref< T >& Ref< T >::operator=( T* ref ) noexcept {
  138.         RefBase::assign( ( RefObject* )ref );
  139.         return *this;
  140.     }
  141.    
  142.     template< typename T >
  143.     Ref< T >& Ref< T >::operator=( RefObject* ref ) noexcept {
  144.         RefBase::assign( ref, RefId< T >::id );
  145.         return *this;
  146.     }
  147.    
  148.     template< typename T >
  149.     Ref< T >& Ref< T >::operator=( Ref const& other ) noexcept {
  150.         RefBase::assign( other );
  151.         return *this;
  152.     }
  153.    
  154.     template< typename T >
  155.     Ref< T >& Ref< T >::operator=( RefBase const& other ) noexcept {
  156.         RefBase::assign( other, RefId< T >::id );
  157.         return *this;
  158.     }
  159.    
  160.     template< typename T >
  161.     Ref< T >& Ref< T >::operator=( Ref&& other ) noexcept {
  162.         RefBase::assign( std::move( ( RefBase&& )other ) );
  163.         return *this;
  164.     }
  165.    
  166.     template< typename T >
  167.     T& Ref< T >::operator*() const {
  168.         return ( T& )RefBase::deref();
  169.     }
  170.    
  171.     template< typename T >
  172.     T* Ref< T >::operator->() const {
  173.         return ( T* )&RefBase::deref();
  174.     }
  175.    
  176.     template< typename T >
  177.     Ref< T >::operator T*() const noexcept {
  178.         return ( T* )RefBase::get();
  179.     }
  180.    
  181.     template< typename T >
  182.     template< typename ...Ts >
  183.     Ref< T > Ref< T >::create( Ts&& ...args ) {
  184.         return Ref( new T( std::forward< Ts >( args )... ), 0 );
  185.     }
  186. }
  187.  
  188. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement