Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UTILS_REF_HPP__
- #define UTILS_REF_HPP__ 1
- #include "refobject.hpp"
- #include <atomic>
- #include <stdexcept>
- #include <exception>
- #include <utility>
- namespace utils {
- class RefBase {
- private:
- std::atomic< RefObject* > m_ref;
- RefObject* possess_silent( RefObject* ref ) noexcept;
- RefObject* assign_silent( RefObject* ref ) noexcept;
- RefObject* assign_silent( RefObject* ref, int id ) noexcept;
- public:
- RefBase() noexcept;
- RefBase( RefObject* ref, int ) noexcept;
- RefBase( RefObject* ref ) noexcept;
- RefBase( RefObject* ref, int, int id ) noexcept;
- RefBase( RefBase&& other ) noexcept;
- RefBase( RefBase const& other ) noexcept;
- RefBase( RefBase const& other, int id ) noexcept;
- ~RefBase() noexcept;
- RefObject* possess( RefObject* ref ) noexcept;
- RefObject* assign( RefObject* ref ) noexcept;
- RefObject* assign( RefObject* ref, int id ) noexcept;
- RefBase& assign( RefBase&& other ) noexcept;
- RefBase& assign( RefBase const& other ) noexcept;
- RefBase& assign( RefBase const& other, int id ) noexcept;
- RefObject* get() const noexcept;
- RefObject& deref() const;
- bool operator==( RefBase const& other );
- bool operator!=( RefBase const& other );
- };
- template< typename T >
- class Ref: public RefBase {
- public:
- Ref() noexcept;
- Ref( nullptr_t ) noexcept;
- Ref( T* ref ) noexcept;
- Ref( RefObject* ref ) noexcept;
- Ref( T* ref, int ) noexcept;
- Ref( Ref const& other ) noexcept;
- Ref( RefBase const& other ) noexcept;
- Ref( Ref&& other ) noexcept;
- ~Ref() noexcept;
- Ref& operator=( nullptr_t ) noexcept;
- Ref& operator=( T* ref ) noexcept;
- Ref& operator=( RefObject* ref ) noexcept;
- Ref& operator=( Ref const& other ) noexcept;
- Ref& operator=( RefBase const& other ) noexcept;
- Ref& operator=( Ref&& other ) noexcept;
- T& operator*() const;
- T* operator->() const;
- operator T*() const noexcept;
- template< typename ...Ts >
- static Ref create( Ts&& ...args );
- };
- template<>
- class Ref< RefObject >: public RefBase {
- public:
- Ref() noexcept;
- Ref( RefObject* ref ) noexcept;
- Ref( RefObject* ref, int ) noexcept;
- Ref( Ref const& other ) noexcept;
- Ref( RefBase const& other ) noexcept;
- Ref( Ref&& other ) noexcept;
- Ref( RefBase&& other ) noexcept;
- ~Ref() noexcept;
- Ref& operator=( RefObject* ref ) noexcept;
- Ref& operator=( Ref const& other ) noexcept;
- Ref& operator=( RefBase const& other ) noexcept;
- Ref& operator=( Ref&& other ) noexcept;
- Ref& operator=( RefBase&& other ) noexcept;
- RefObject& operator*() const;
- RefObject* operator->() const;
- operator RefObject*() const noexcept;
- };
- template< typename T >
- Ref< T >::Ref() noexcept :
- RefBase() {
- }
- template< typename T >
- Ref< T >::Ref( nullptr_t ) noexcept :
- RefBase( ( RefObject* )0 ) {
- }
- template< typename T >
- Ref< T >::Ref( T* ref ) noexcept :
- RefBase( ( RefObject* )ref ) {
- }
- template< typename T >
- Ref< T >::Ref( RefObject* ref ) noexcept :
- RefBase( ref, 0, RefId< T >::id ) {
- }
- template< typename T >
- Ref< T >::Ref( T* ref, int ) noexcept :
- RefBase( ( RefObject* )ref, 0 ) {
- }
- template< typename T >
- Ref< T >::Ref( Ref const& other ) noexcept :
- RefBase( other ) {
- }
- template< typename T >
- Ref< T >::Ref( RefBase const& other ) noexcept :
- RefBase( other, RefId< T >::id ) {
- }
- template< typename T >
- Ref< T >::Ref( Ref&& other ) noexcept :
- RefBase( std::move( ( RefBase&& )other ) ) {
- }
- template< typename T >
- Ref< T >::~Ref() noexcept {
- }
- template< typename T >
- Ref< T >& Ref< T >::operator=( nullptr_t ) noexcept {
- RefBase::assign( ( RefObject* )0 );
- return *this;
- }
- template< typename T >
- Ref< T >& Ref< T >::operator=( T* ref ) noexcept {
- RefBase::assign( ( RefObject* )ref );
- return *this;
- }
- template< typename T >
- Ref< T >& Ref< T >::operator=( RefObject* ref ) noexcept {
- RefBase::assign( ref, RefId< T >::id );
- return *this;
- }
- template< typename T >
- Ref< T >& Ref< T >::operator=( Ref const& other ) noexcept {
- RefBase::assign( other );
- return *this;
- }
- template< typename T >
- Ref< T >& Ref< T >::operator=( RefBase const& other ) noexcept {
- RefBase::assign( other, RefId< T >::id );
- return *this;
- }
- template< typename T >
- Ref< T >& Ref< T >::operator=( Ref&& other ) noexcept {
- RefBase::assign( std::move( ( RefBase&& )other ) );
- return *this;
- }
- template< typename T >
- T& Ref< T >::operator*() const {
- return ( T& )RefBase::deref();
- }
- template< typename T >
- T* Ref< T >::operator->() const {
- return ( T* )&RefBase::deref();
- }
- template< typename T >
- Ref< T >::operator T*() const noexcept {
- return ( T* )RefBase::get();
- }
- template< typename T >
- template< typename ...Ts >
- Ref< T > Ref< T >::create( Ts&& ...args ) {
- return Ref( new T( std::forward< Ts >( args )... ), 0 );
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement