C++ (Qt) struct HackValue { struct Counter { QBasicAtomicInt weakref; QBasicAtomicInt strongref; }; void * m_value; Counter * m_counter; }; //QSharedPointer< Value > value; HackValue * hack = reinterpret_cast< HackValue * >( &value ); // access hack->m_counter->weakref; hack->m_counter->strongref;
C++ (Qt)template < typename _Type >class TShared; template < typename _Type >class TWeak; struct TInfo{ std::map< void *, const char * > m_name_by_address; std::map< void *, int > m_shared_count_by_address; std::map< void *, int > m_weak_count_by_address; void insertShared ( void * address, const char * name ) { m_name_by_address[ address ] = name; ++m_shared_count_by_address[ address ]; } void removeShared ( void * address ) { --m_shared_count_by_address[ address ]; checkEmpty( address ); } void insertWeak ( void * address, const char * name ) { m_name_by_address[ address ] = name; ++m_weak_count_by_address[ address ]; } void removeWeak ( void * address ) { --m_weak_count_by_address[ address ]; checkEmpty( address ); } void checkEmpty ( void * address ) { //TODO если weak и shared обнулились - удалить address из всех коллекций }}; template < typename _Type >class TShared{public: typedef TShared< _Type > ThisType; // using ThisType = TShared< _Type >; private: const void * m_address; std::shared_ptr< _Type > m_pointer; std::shared_ptr< TInfo > m_info; public: TShared ( void * address, _Type * value =std::null_ptr ) : m_address( address ) , m_pointer( value ) , m_info( new TInfo ) // std::make_shared { m_info->insertShared( m_address, typeid( *value ).name() ); } ~TShared () { m_info->removeShared( m_address ); } template < typename _OtherType > TShared ( const TShared< _OtherType > & other ); template < typename _OtherType > TShared ( const TWeak< _OtherType > & other ); template < typename _OtherType > ThisType & operator = ( const TShared< _OtherType > & other ) { m_info->removeShared( m_address ); m_pointer = other.m_pointer; m_info = other.m_info; m_info->insertShared( m_address ); return *this; } template < typename _OtherType > ThisType & operator = ( const TWeak< _OtherType > & other ) { m_info->removeShared( m_address ); m_pointer = other.m_pointer; m_info = other.m_info; m_info->insertShared( m_address ); return *this; }}; template < typename _Type >class TWeak{public: typedef TWeak< _Type > ThisType; // using ThisType = TWeak< _Type >; private: const void * m_address; std::weak_ptr< _Type > m_pointer; std::shared_ptr< TInfo > m_info; public: TWeak ( void * address ) : m_address( address ) , m_pointer( value ) , m_info( new TInfo ) // std::make_shared { m_info->insertWeak( m_address, typeid( *value ).name() ); } ~TWeak () { m_info->removeWeak( m_address ); } template < typename _OtherType > TWeak ( const TWeak< _OtherType > & other ); template < typename _OtherType > TWeak ( const TShared< _OtherType > & other ); template < typename _OtherType > ThisType & operator = ( const TWeak< _OtherType > & other ) { m_info->removeWeak( m_address ); m_pointer = other.m_pointer; m_info = other.m_info; m_info->insertWeak( m_address ); return *this; } template < typename _OtherType > ThisType & operator = ( const TShared< _OtherType > & other ) { m_info->removeWeak( m_address ); m_pointer = other.m_pointer; m_info = other.m_info; m_info->insertWeak( m_address ); return *this; }};
C++ (Qt)template <class T>struct TShared { template <class X> TShared( const X * owner ); ...private: std::shared_ptr<T> mPtr; void * mOwner; const char * mTypeName; static std::set<TShared *> mSet;};