Advertisement
Delfigamer

utils/refobject.cpp

Feb 24th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. // #define COUNT_OBJECTS
  2.  
  3. #include "refobject.hpp"
  4. #include "logger.hpp"
  5. #include "cbase.hpp"
  6.  
  7. namespace utils {
  8. #ifdef COUNT_OBJECTS
  9.     std::atomic< int > rocount( 0 );
  10. #endif
  11.    
  12.     bool RefObject::query( int id, void** target ) {
  13.         return queryobject( this, id, target );
  14.     }
  15.    
  16.     RefObject::RefObject() noexcept {
  17. //      LOG( "((RefObject*) %#10x)->RefObject()", uint32_t( this ) );
  18. #ifdef COUNT_OBJECTS
  19.         LOG( "   new refobject %#10x, utils::rocount = %i", uint32_t( this ), rocount.fetch_add( 1, std::memory_order_relaxed ) + 1 );
  20. #endif
  21.         m_refcount = 1;
  22.     }
  23.    
  24.     RefObject::RefObject( RefObject const& other ) noexcept {
  25. //      LOG( "((RefObject*) %#10x)->RefObject()", uint32_t( this ) );
  26. #ifdef COUNT_OBJECTS
  27.         LOG( "   new refobject %#10x, utils::rocount = %i", uint32_t( this ), rocount.fetch_add( 1, std::memory_order_relaxed ) + 1 );
  28. #endif
  29.         m_refcount = 1;
  30.     }
  31.    
  32.     RefObject::RefObject( RefObject&& other ) noexcept {
  33. //      LOG( "((RefObject*) %#10x)->RefObject()", uint32_t( this ) );
  34. #ifdef COUNT_OBJECTS
  35.         LOG( "   new refobject %#10x, utils::rocount = %i", uint32_t( this ), rocount.fetch_add( 1, std::memory_order_relaxed ) + 1 );
  36. #endif
  37.         m_refcount = 1;
  38.     }
  39.    
  40.     RefObject::~RefObject() noexcept {
  41. //      LOG( "((RefObject*) %#10x)->~RefObject()", uint32_t( this ) );
  42. #ifdef COUNT_OBJECTS
  43.         LOG( "delete refobject %#10x, utils::rocount = %i", uint32_t( this ), rocount.fetch_sub( 1, std::memory_order_relaxed ) - 1 );
  44. #endif
  45.     }
  46.    
  47.     void RefObject::destroy() {
  48.         delete this;
  49.     }
  50.    
  51.     RefObject& RefObject::operator=( RefObject const& other ) noexcept {
  52.         return *this;
  53.     }
  54.    
  55.     RefObject& RefObject::operator=( RefObject&& other ) noexcept {
  56.         return *this;
  57.     }
  58.    
  59. //  template<>
  60. //  bool queryobject< RefObject >( RefObject* source, int id, void** target ) {
  61. //      if( id == RefId< RefObject >::id ) {
  62. //          *target = static_cast< RefObject* >( source );
  63. //          source->addref();
  64. //          return true;
  65. //      } else {
  66. //          *target = 0;
  67. //          return false;
  68. //      }
  69. //  }
  70.    
  71.     template<>
  72.     bool queryobject< void >( void* source, int id, void** target ) {
  73.         *target = 0;
  74.         return false;
  75.     }
  76. }
  77.  
  78. extern "C" {
  79.     int refobject_addref( utils::RefObject* ro ) noexcept {
  80.         CBASE_PROTECT(
  81.             if( ro ) {
  82.                 return ro->addref();
  83.             } else {
  84.                 return -1;
  85.             }
  86.         )
  87.     }
  88.    
  89.     int refobject_release( utils::RefObject* ro ) noexcept {
  90.         CBASE_PROTECT(
  91.             if( ro ) {
  92.                 return ro->release();
  93.             } else {
  94.                 return -1;
  95.             }
  96.         )
  97.     }
  98.    
  99.     utils::RefObject* refobject_refcast( utils::RefObject* ro, int id ) noexcept {
  100.         CBASE_PROTECT(
  101.             if( ro ) {
  102.                 utils::RefObject* ptr;
  103.                 ro->query( id, ( void** )&ptr );
  104.                 return ptr;
  105.             } else {
  106.                 return 0;
  107.             }
  108.         )
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement