/* $Id: attachableobject.cpp,v 1.14 2005/12/04 20:40:39 pohlt Exp $ */ #include "attachableobject.hpp" #include "serialize.hpp" #include "constants.hpp" #include /******************************************************************************/ void AttachableObject::printAttachInfo( const bool withOwnID ) const { INFO( "AttachableObject::printAttachInfo: info for %s (%p)\n", withOwnID ? ObjectIDString[getID()] : "[n/a]", this ); INFO( "\tnAttachedObjects: %i\n", getNAttachedObjects() ); for( int i = 0; i < getNAttachedObjects(); i++ ) { INFO( "\tobject %2i is a %s (%p)\n", i, ObjectIDString[getAttachedObject( i )->getID()], getAttachedObject( i ) ); } } /******************************************************************************/ void AttachableObject::serialize( Uint8*& bufferPointer ) const { AttachableRegistrar* registrar = AttachableRegistrar::getInstance(); // expands to a check of the buffer movement START_OBJECT_SERIALIZED_SIZE_CHECK( bufferPointer ); Object::serialize( bufferPointer ); const Sint32 nAttachedObjects = m_attachedTo.size(); Serialize::serialize( nAttachedObjects, bufferPointer ); if( nAttachedObjects > 0 ) { Serialize::serialize( registrar->convertPtrToID( this ), bufferPointer ); for( int i = 0; i < nAttachedObjects; i++ ) { Serialize::serialize( registrar->convertPtrToID( m_attachedTo[i] ), bufferPointer ); } } // expands to tag serialization SERIALIZE_OBJECT_TAG( bufferPointer ); // expands to a check of the buffer movement END_OBJECT_SERIALIZED_SIZE_CHECK( bufferPointer, AttachableObject ); } /******************************************************************************/ void AttachableObject::deserialize( Uint8*& bufferPointer ) { Object::deserialize( bufferPointer ); Sint32 nAttachedObjects; Serialize::deserialize( bufferPointer, nAttachedObjects ); if( nAttachedObjects > 0 ) { Uint32 id; Serialize::deserialize( bufferPointer, id ); AttachableRegistrar::getInstance()->registerID( id, this ); for( int i = 0; i < nAttachedObjects; i++ ) { Serialize::deserialize( bufferPointer, id ); m_attachedTo.push_back( reinterpret_cast( id )); } } // expands to tag deserialization DESERIALIZE_OBJECT_TAG( bufferPointer ); } /******************************************************************************/ Uint32 AttachableObject::getSerializeBufferSize() const { if( m_attachedTo.size() == 0 ) { // no attached objects => just send a zero return Object::getSerializeBufferSize() + Serialize::sizeOf() PLUS_TAG_SIZE( 1 ); } else { return Object::getSerializeBufferSize() + Serialize::sizeOf() + (1 + m_attachedTo.size()) * Serialize::sizeOf() // adds additional size for debugging (see serialize.hpp), // but only, if the tags are used PLUS_TAG_SIZE( 1 ); } } /******************************************************************************/ /******************************************************************************/ /******************************************************************************/ AttachableRegistrar::AttachableRegistrar(): m_nextID( 0 ) {} /******************************************************************************/ AttachableRegistrar* AttachableRegistrar::m_instance = 0x0; AttachableRegistrar* AttachableRegistrar::getInstance() { if( ! m_instance ) { m_instance = new AttachableRegistrar; } return m_instance; } /******************************************************************************/ void AttachableRegistrar::deleteInstance() { delete m_instance; m_instance = 0x0; } /******************************************************************************/ Uint32 AttachableRegistrar::convertPtrToID( const AttachableObject* ptr ) { Uint32 id; std::map::iterator it = m_mapPtrToID.find( ptr ); if( it == m_mapPtrToID.end() ) { id = m_nextID; m_mapPtrToID.insert( std::make_pair( ptr, id )); m_nextID++; } else { id = it->second; } return id; } /******************************************************************************/ bool AttachableRegistrar::registerID( const Uint32 id, AttachableObject* ptr ) { LOG( 3 ) INFO( "AttachableRegistrar::registerMe: registering AO\n" ); return m_mapIDToPtr.insert( std::make_pair( id, ptr ) ).second; } /******************************************************************************/ bool AttachableRegistrar::startIDToPtrConversion() { LOG( 2 ) INFO( "AttachableRegistrar::startIDToPtrConversion: converting %i " "AOs\n", m_mapIDToPtr.size() ); std::map::iterator it; for( it = m_mapIDToPtr.begin(); it != m_mapIDToPtr.end(); it++ ) { LOG( 2 ) INFO( "AttachableRegistrar::startPointerConversion: converting\n" ); std::vector& ao = it->second->getAttachedObjects(); for( unsigned int o = 0; o < ao.size(); o++ ) { const Uint32 id = reinterpret_cast( ao[o] ); std::map::iterator foundPtr = m_mapIDToPtr.find( id ); ASSERT( foundPtr != m_mapIDToPtr.end(), "AttachableRegistrar::startPointerConversion: could not find object\n" ); ao[o] = foundPtr->second; } LOG( 2 ) INFO( "AttachableRegistrar::startPointerConversion: converting done\n" ); } return true; } /******************************************************************************/ void AttachableRegistrar::reset() { m_mapIDToPtr.clear(); m_mapPtrToID.clear(); m_nextID = 0; LOG( 2 ) INFO( "AttachableRegistrar::reset: done\n" ); }