Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

PsNoRefCountingNameServer.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of openMask © INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software
00003  * 
00004  * The Software has been developped within the Siames Project. 
00005  * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights
00006  * 
00007  * The Software has been registered with the Agence pour la Protection des
00008  * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200
00009  *
00010  * This file may be distributed under the terms of the Q Public License
00011  * version 1.0 as defined by Trolltech AS of Norway and appearing in the file
00012  * LICENSE.QPL included in the packaging of this file.
00013  *
00014  * Licensees holding valid specific licenses issued by INRIA, CNRS or Université de Rennes 1 
00015  * for the software may use this file in accordance with that specific license 
00016  *
00017  */
00018 
00019 #ifndef PsNoRefCountingNameServerHEADER
00020 #define PsNoRefCountingNameServerHEADER
00021 
00022 #include "PsNameServer.h"
00023 
00024 using namespace std ;
00025 
00033 #ifdef _OLDALLOCATORS
00034 template <class STLallocator >
00035 #else
00036 template <template <typename T> class STLallocator >
00037 #endif
00038 class PsNoRefCountingNameServer : public PsNameServer
00039 {
00040 public:
00042    virtual ~PsNoRefCountingNameServer() ;
00043 
00045    PsNoRefCountingNameServer(const PsNameServer::CanonicalRepresentationType * canonicalRepresentation) ;
00046 
00049    virtual long getIdentifier(const PsString &);
00050 
00051 
00054    virtual long getSystemIdentifier(const PsString &);
00055 
00056 
00059    virtual long getIdentifierAsFrom(const PsString &, long * nextId);
00060 
00061 
00064    virtual const PsString & getStringAssociatedTo (PsName::idType id) const;
00065 
00066 
00068    void * operator new(size_t);
00069 
00070 
00072    void operator delete(void * ptr, size_t n);
00073 
00074 
00076    virtual void created(PsName::idType id, PsName * );
00077 
00078 
00080    virtual void deleted(PsName::idType id, PsName * );
00081 
00082 
00086    CanonicalRepresentationType * getCanonicalRepresentation () const ;
00087 
00089    virtual const list<PsName *> & getNamesOfId (PsName::idType) const ;
00090 
00091 protected:
00093    virtual void lock() const ;
00094 
00096    virtual void unlock() const ;
00097 
00099    mutable PsnMutexLock _ourDataLock ;
00100 
00103    long _nextIdentifier ;
00104 
00107    long _nextSystemIdentifier ;
00108 #ifdef _OLDALLOCATORS
00109 
00110    typedef hash_map<PsString, long, PsString_hash > correspondenceContainerType ;
00111 
00113    typedef map<long,correspondenceContainerType::iterator> fastStringAccessTableType;
00114 #else
00115 
00116    typedef hash_map<PsString,long, PsString_hash ,equal_to<PsString>,STLallocator<pair <PsString,long> > > correspondenceContainerType;
00117 
00119    typedef map<long, typename correspondenceContainerType::iterator,less<long>,STLallocator<pair<const long, typename correspondenceContainerType::iterator> > > fastStringAccessTableType;
00120 #endif
00121 
00122    fastStringAccessTableType _fastStringAccessTable ;
00123 
00125    correspondenceContainerType _stringToIdCorrespondenceContainer ;
00126 
00127 };
00128 
00129 
00130 #ifdef _OLDALLOCATORS
00131 template <class STLallocator >
00132 #else
00133 template <template <typename T> class STLallocator >
00134 #endif
00135 PsNoRefCountingNameServer<STLallocator>::PsNoRefCountingNameServer(const PsNameServer::CanonicalRepresentationType * canonicalRepresentation)  
00136 {
00137    //cerr<<"PsNoRefCountingNameServer<STLallocator>:"<<this<<":PsNoRefCountingNameServer(const PsNameServer::CanonicalRepresentationType * canonicalRepresentation)"<<endl;
00138     for ( CanonicalRepresentationType::const_iterator i = canonicalRepresentation->begin() ;
00139          i != canonicalRepresentation->end() ;
00140          i++ )
00141       {
00142          //(*i) is < <PsName::idType,PsString> , numberOfInstances> therefore
00143          //(*i).first.first is the id,
00144          //(*i).first.second is the associated string
00145          //(*i).second is the number of instances of PsNames with id as id
00146 
00147          //first create an entry in the hash_table for the associated string
00148 #ifdef _TYPENAMENOTIMPLICIT
00149          typename correspondenceContainerType::iterator ii=(_stringToIdCorrespondenceContainer.insert(typename correspondenceContainerType::value_type((*i).first.second,(*i).first.first))).first;
00150          // then reference that entry in the iterator table 
00151          _fastStringAccessTable.insert(pair<const long, typename correspondenceContainerType::iterator>((*i).first.first,ii));   
00152 #else
00153          correspondenceContainerType::iterator ii=(_stringToIdCorrespondenceContainer.insert(correspondenceContainerType::value_type((*i).first.second,(*i).first.first))).first;
00154          // then reference that entry in the iterator table 
00155          _fastStringAccessTable.insert(pair<const long, correspondenceContainerType::iterator>((*i).first.first,ii));    
00156 #endif
00157 
00158          //try and find a reasonnable next id
00159          _nextIdentifier = (*i).first.first + 1 ;
00160       }
00161 }
00162 
00163 
00164 #ifdef _OLDALLOCATORS
00165 template <class STLallocator >
00166 #else
00167 template <template <typename T> class STLallocator >
00168 #endif
00169 PsNoRefCountingNameServer<STLallocator>::~PsNoRefCountingNameServer() 
00170 {
00171    //nothing to do   
00172 }
00173 
00174 
00175 #ifdef _OLDALLOCATORS
00176 template <class STLallocator >
00177 #else
00178 template <template <typename T> class STLallocator >
00179 #endif
00180 PsName::idType 
00181 PsNoRefCountingNameServer<STLallocator>::getIdentifier(const PsString & name) 
00182 {
00183    PsName::idType res;
00184    //cerr<<this<<" "<<_nextIdentifier<<" ";
00185    res = getIdentifierAsFrom(name, &_nextIdentifier) ;
00186    //cerr<<"("<<res<<","<<name<<") added to name server "<<_nextIdentifier<<endl;
00187    return res;
00188 }
00189 
00190 
00191 #ifdef _OLDALLOCATORS
00192 template <class STLallocator >
00193 #else
00194 template <template <typename T> class STLallocator >
00195 #endif
00196 PsName::idType 
00197 PsNoRefCountingNameServer<STLallocator>::getSystemIdentifier(const PsString & name) 
00198 {
00199 
00200    PsName::idType result = getIdentifierAsFrom(name, &_nextSystemIdentifier) ;
00201    // this assertion could fail if a PsName corresponding to a reserved id was staticaly created
00202    assert ( result <= PsName::_maxReservedId ) ;
00203    //this would fail if more reserved Id's are created than originaly planned for
00204    assert ( _nextSystemIdentifier <= PsName::_maxReservedId ) ;
00205 
00206    return result ;
00207 }
00208 
00209 
00210 #ifdef _OLDALLOCATORS
00211 template <class STLallocator >
00212 #else
00213 template <template <typename T> class STLallocator >
00214 #endif
00215 PsName::idType 
00216 PsNoRefCountingNameServer<STLallocator>::getIdentifierAsFrom (const PsString & name, long * nextId ) {
00217 
00218 #ifdef _TYPENAMENOTIMPLICIT
00219    typename
00220 #endif
00221    correspondenceContainerType::iterator i = _stringToIdCorrespondenceContainer.find( name );
00222 
00223    if (i == _stringToIdCorrespondenceContainer.end()) 
00224       {
00225          //we are going to change the values contained in the PsNameServer
00226          lock() ; 
00227 
00228          i = _stringToIdCorrespondenceContainer.find( name ) ; //name might have been added by an other process
00229 
00230          if (i == _stringToIdCorrespondenceContainer.end() ) //we have to add name
00231             { 
00232 
00233 #ifdef _TYPENAMENOTIMPLICIT
00234                pair<typename correspondenceContainerType::iterator,bool> res = _stringToIdCorrespondenceContainer.insert(typename correspondenceContainerType::value_type(name,*nextId)) ;
00235 #else
00236                pair<correspondenceContainerType::iterator,bool> res = _stringToIdCorrespondenceContainer.insert(correspondenceContainerType::value_type(name,*nextId)) ;
00237 #endif
00238  
00239                _fastStringAccessTable[*nextId] = res.first ;
00240 
00241                *nextId = *nextId + 1 ;
00242                
00243                i = res.first ;
00244          }
00245 
00246          unlock() ;
00247       }
00248    return (*i).second;
00249 }
00250 
00251 #ifdef _OLDALLOCATORS
00252 template <class STLallocator >
00253 #else
00254 template <template <typename T> class STLallocator >
00255 #endif
00256 const PsString & 
00257 PsNoRefCountingNameServer<STLallocator>::getStringAssociatedTo (PsName::idType id) const 
00258 {
00259    assert ( id<_nextIdentifier) ;
00260    //assert ( _fastStringAccessTable.find(id) != _fastStringAccessTable.end() ) ;
00261    return  (*(*_fastStringAccessTable.find(id)).second).first ;
00262 }
00263 
00264 #ifdef _OLDALLOCATORS
00265 template <class STLallocator >
00266 #else
00267 template <template <typename T> class STLallocator >
00268 #endif
00269 PsNameServer::CanonicalRepresentationType * 
00270 PsNoRefCountingNameServer<STLallocator>::getCanonicalRepresentation() const 
00271 {
00272    PsNameServer::CanonicalRepresentationType * canonicalRepresentation = new PsNameServer::CanonicalRepresentationType () ;
00273 #ifdef _TYPENAMENOTIMPLICIT
00274    typename
00275 #endif
00276    PsNoRefCountingNameServer<STLallocator>::correspondenceContainerType::const_iterator i;
00277    for (i=_stringToIdCorrespondenceContainer.begin(); i!=_stringToIdCorrespondenceContainer.end();i++) 
00278       {
00279          canonicalRepresentation->push_back(PsPair<PsPair<PsName::idType,PsString>, int>(PsPair<PsName::idType,PsString>((*i).second,(*i).first),1)) ;
00280       }
00281    canonicalRepresentation->sort() ;
00282    return canonicalRepresentation ;
00283 }
00284 
00285 
00286 
00287 #ifdef _OLDALLOCATORS
00288 template <class STLallocator >
00289 void *
00290 PsNoRefCountingNameServer<STLallocator>::operator new(size_t size) 
00291 {
00292    return STLallocator::allocate(size);
00293 }
00294 #else
00295 template <template <typename T> class STLallocator >
00296 void *
00297 PsNoRefCountingNameServer<STLallocator>::operator new(size_t size) 
00298 {
00299    STLallocator<PsNoRefCountingNameServer<STLallocator> > anAllocator;
00300    return anAllocator.allocate(size);
00301 }
00302 #endif
00303 
00304 
00305 
00306 #ifdef _OLDALLOCATORS
00307 template <class STLallocator >
00308 void PsNoRefCountingNameServer<STLallocator>::operator delete(void * ptr, size_t n) 
00309 {
00310    STLallocator::deallocate(ptr,n);
00311 }
00312 #else
00313 template <template <typename T> class STLallocator >
00314 void PsNoRefCountingNameServer<STLallocator>::operator delete(void * ptr, size_t n) 
00315 {
00316    STLallocator<PsNoRefCountingNameServer<STLallocator> > anAllocator;
00317    anAllocator.deallocate((PsNoRefCountingNameServer<STLallocator> *)ptr,n);
00318 }
00319 #endif
00320 
00321 #ifdef _OLDALLOCATORS
00322 template <class STLallocator >
00323 #else
00324 template <template <typename T> class STLallocator >
00325 #endif
00326 void PsNoRefCountingNameServer<STLallocator>::created(PsName::idType id, PsName * name) 
00327 {
00328 }
00329 
00330 #ifdef _OLDALLOCATORS
00331 template <class STLallocator >
00332 #else
00333 template <template <typename T> class STLallocator >
00334 #endif
00335 void PsNoRefCountingNameServer<STLallocator>::deleted(PsName::idType id, PsName * name) 
00336 {
00337 }
00338 
00339 
00340 #ifdef _OLDALLOCATORS
00341 template <class STLallocator >
00342 #else
00343 template <template <typename T> class STLallocator >
00344 #endif
00345 void 
00346 PsNoRefCountingNameServer<STLallocator>::lock () const 
00347 {
00348    _ourDataLock.protect() ;
00349 }
00350 
00351 
00352 #ifdef _OLDALLOCATORS
00353 template <class STLallocator >
00354 #else
00355 template <template <typename T> class STLallocator >
00356 #endif
00357 void PsNoRefCountingNameServer<STLallocator>::unlock () const 
00358 {
00359    _ourDataLock.unprotect() ;
00360 }
00361 #ifdef _OLDALLOCATORS
00362 template <class STLallocator >
00363 #else
00364 template <template <typename T> class STLallocator >
00365 #endif
00366 const list<PsName *> & PsNoRefCountingNameServer<STLallocator>::getNamesOfId (PsName::idType id) const 
00367 {
00368    static list<PsName *> emptyResult ;
00369    return emptyResult ;
00370 }
00371 
00372 #endif

logo OpenMask

Documentation generated on Mon Nov 25 15:25:01 2002

Generated with doxygen 1.2.12 by Dimitri van Heesch ,   1997-2001