Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

clientdatamap.cpp

00001 /***************************************************************************
00002  * The contents of this file are subject to the Mozilla Public             *
00003  * License Version 1.1 (the "License"); you may not use this file          *
00004  * except in compliance with the License. You may obtain a copy of         *
00005  * the License at http://www.mozilla.org/MPL/                              *
00006  *                                                                         *
00007  * Software distributed under the License is distributed on an "AS         *
00008  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
00009  * implied. See the License for the specific language governing            *
00010  * rights and limitations under the License.                               *
00011  *                                                                         *
00012  * The Original Code is Game Network Framework (GaNeF).                    *
00013  *                                                                         *
00014  * The Initial Developers of the Original Code are                         *
00015  * Lars Langer and Emanuel Greisen                                         *
00016  * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
00017  * All Rights Reserved.                                                    *
00018  *                                                                         *
00019  * Contributor(s):                                                         *
00020  *   none yet....                                                          *
00021  *                                                                         *
00022  ***************************************************************************/
00023 #include "clientdatamap.h"
00024 #include "clientdata.h"
00025 #include <map.h>
00026 
00027 ClientDataMap::ClientDataMap()
00028 {
00029    pointing_map.clear();
00030    pointee_map.clear();
00031 }
00032 
00033 
00034 ClientDataMap::~ClientDataMap()
00035 {
00036 }
00037 
00038 void ClientDataMap::unregisterData( ClientData * data )
00039 {
00040    //update the references in the reference map to NULL
00041    if(pointee_map.find(data->getFrameworkId()) != pointee_map.end())
00042    {
00043       PPMap & ppmap = pointee_map[data->getFrameworkId()];
00044       unsigned int i ;
00045       for (PPMap::iterator it = ppmap.begin() ; it != ppmap.end(); it++)
00046       {
00047          PtrPtrVec & ptrptrvec = it->second;
00048          for(PtrPtrVec::iterator vit = ptrptrvec.begin(); vit != ptrptrvec.end(); vit++)
00049          {
00050             ClientData ** ptrptr = *vit;
00051             if(*ptrptr == data)
00052                *ptrptr = NULL;
00053          }
00054       }
00055    }
00056 
00057    // remove from pointee maps via pointing map - if this object no longer exists noone needs to wait for it
00058    // and set its pointer when it arrives
00059    if(pointing_map.find(data->getFrameworkId()) != pointing_map.end())
00060    {
00061       PPMap & ppmap = pointing_map[data->getFrameworkId()].ppmap;
00062       unsigned int i ;
00063       for (PPMap::iterator it = ppmap.begin() ; it != ppmap.end(); it++)
00064       {
00065 
00066          unsigned int pointee_entry = it->first;
00067          pointee_map[pointee_entry].erase(data->getFrameworkId());
00068          if(pointee_map[pointee_entry].empty());
00069          {
00070             pointee_map.erase(pointee_entry);
00071          }
00072 
00073       }
00074          //and last, but not least, remove the entry from the pointing map
00075       pointing_map.erase(data->getFrameworkId());
00076    }
00077    //std::cout << "unregisterData( FrameworkData * data : " << data->getFrameworkId() << ")" << std::endl;
00078 }
00079 
00080 void ClientDataMap::registerPointer( unsigned int receiver, unsigned int missing, ClientData ** ptrptr )
00081 {
00082    pointing_map[receiver].ppmap[missing].push_back(ptrptr);
00083    pointing_map[receiver].ptr2pointee[ptrptr] = missing;
00084 
00085       // Register that someone is missing 'missing' (its 'receiver')
00086    pointee_map[missing][receiver].push_back(ptrptr);
00087 
00088    //std::cout << "registerPointer( resv: "<<receiver<<", miss: "<<missing<<", ptrptr: " <<ptrptr<<" )" << std::endl;
00089 }
00090 
00091 
00092 bool ClientDataMap::unregisterPointer(unsigned int receiver, unsigned int new_objid, ClientData **ptrptr)
00093 {
00094    // Either the pointing object is unknown
00095    if(pointing_map.find(receiver) == pointing_map.end())
00096       return true;
00097 
00098    // Or this pointer has not been set before
00099    if(pointing_map[receiver].ptr2pointee.find(ptrptr) == pointing_map[receiver].ptr2pointee.end())
00100       return true;
00101 
00102    // Ok, it is set, lets see who it's pointing at
00103    unsigned int pointee_entry = pointing_map[receiver].ptr2pointee[ptrptr];
00104 
00105    // We want the ptrptr to point at the same object - no need to change
00106    if(pointee_entry == new_objid)
00107       return false;
00108 
00109    // If we are about the read a null-pointer we might as well clean up the pointing_map
00110    //if(new_objid == 0)
00111    {
00112       pointing_map[receiver].ptr2pointee.erase(ptrptr);
00113 
00114       PtrPtrVec & ptrptrvec = pointing_map[receiver].ppmap[pointee_entry];
00115       for(PtrPtrVec::iterator vit = ptrptrvec.begin(); vit != ptrptrvec.end(); vit++)
00116       {
00117          if(*vit == ptrptr)
00118          {
00119             ptrptrvec.erase(vit);
00120             if(ptrptrvec.empty())
00121                pointing_map[receiver].ppmap.erase(pointee_entry);
00122 
00123             if(pointing_map[receiver].isEmpty())
00124                pointing_map.erase(receiver);
00125 
00126             break;
00127          }
00128       }
00129    }
00130 
00131    // Sanity check: make sure pointee is also in the pointee-map
00132    if(pointee_map.find(pointee_entry) != pointee_map.end())
00133    {
00134       PPMap & ppmap = pointee_map[pointee_entry];
00135       if(ppmap.find(receiver) == ppmap.end())
00136          throw new FrameworkError("Pointing object did not exist in pointee map");
00137 
00138       PtrPtrVec & ptrptrvec = ppmap[receiver];
00139       for(PtrPtrVec::iterator vit = ptrptrvec.begin(); vit != ptrptrvec.end(); vit++)
00140       {
00141          if(*vit == ptrptr)
00142          {
00143             ptrptrvec.erase(vit);
00144 
00145             if(ptrptrvec.empty())
00146                pointee_map[pointee_entry].erase(receiver);
00147             if(pointee_map[pointee_entry].empty())
00148                pointee_map.erase(pointee_entry);
00149 
00150             break;
00151          }
00152       }
00153    }
00154    else
00155    {
00156       std::cerr << "Pointee ["<<pointee_entry<<"] did not exist in pointee map"<<std::endl;
00157       throw new FrameworkError("Pointee did not exist in pointee map");
00158    }
00159    return true;
00160 }
00161 
00162 void ClientDataMap::printDebug( )
00163 {
00164    std::cout << "----------: Pointer pointer table thingy :----------"<<std::endl;
00165 
00166    // The pointing objects
00167    for(PointingMap::const_iterator it = pointing_map.begin(); it != pointing_map.end(); ++it)
00168    {
00169       std::cout << "Pointing Obj["<<it->first<<"]:\n";
00170       std::cout << " -PPMAP:";
00171       for(PPMap::const_iterator ppit = it->second.ppmap.begin();ppit != it->second.ppmap.end(); ++ppit)
00172       {
00173          std::cout << "["<<ppit->first<<":<";
00174          for(PtrPtrVec::const_iterator ptrit = ppit->second.begin(); ptrit != ppit->second.end(); ++ptrit)
00175          {
00176             std::cout << (*ptrit) << ",";
00177          }
00178          std::cout << ">]";
00179       }
00180       std::cout << std::endl;
00181       std::cout << " -PtrPtrMap:";
00182       std::map<ClientData **, unsigned int>::const_iterator ptrit;
00183       for(ptrit = it->second.ptr2pointee.begin(); ptrit != it->second.ptr2pointee.end(); ++ptrit)
00184       {
00185          std::cout << "["<<ptrit->first<<":"<<ptrit->second<<"],";
00186       }
00187       std::cout << std::endl;
00188    }
00189 
00190 
00191    // The pointees
00192    for(PointeeMap::const_iterator it = pointee_map.begin(); it != pointee_map.end(); ++it)
00193    {
00194       std::cout << "Pointee Obj["<<it->first<<"]:\n";
00195       std::cout << " -PPMAP:";
00196       for(PPMap::const_iterator ppit = it->second.begin();ppit != it->second.end(); ++ppit)
00197       {
00198          std::cout << "["<<ppit->first<<":<";
00199          for(PtrPtrVec::const_iterator ptrit = ppit->second.begin(); ptrit != ppit->second.end(); ++ptrit)
00200          {
00201             std::cout << (*ptrit) << ",";
00202          }
00203          std::cout << ">]";
00204       }
00205       std::cout << std::endl;
00206    }
00207    std::cout << "----------: Pointer pointer table thingy :----------"<<std::endl;
00208 }
00209 
00210 
00211 
00212 
00213 

Generated on Mon Feb 6 12:24:50 2006 for Ganef by  doxygen 1.4.4