GME  13
SynchronizedStringPool.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: SynchronizedStringPool.cpp 903137 2010-01-26 09:26:28Z borisk $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/util/SynchronizedStringPool.hpp>
00027 
00028 
00029 XERCES_CPP_NAMESPACE_BEGIN
00030 
00031 // ---------------------------------------------------------------------------
00032 //  XMLSynchronizedStringPool: Constructors and Destructor
00033 // ---------------------------------------------------------------------------
00034 XMLSynchronizedStringPool::XMLSynchronizedStringPool(const XMLStringPool *constPool
00035                 , const  unsigned int  modulus
00036                 , MemoryManager* const manager) :
00037 
00038     XMLStringPool(modulus, manager)
00039     , fConstPool(constPool)
00040     , fMutex(manager)
00041 {
00042 }
00043 
00044 XMLSynchronizedStringPool::~XMLSynchronizedStringPool()
00045 {
00046 }
00047 
00048 
00049 // ---------------------------------------------------------------------------
00050 //  XMLSynchronizedStringPool: Pool management methods
00051 // ---------------------------------------------------------------------------
00052 unsigned int XMLSynchronizedStringPool::addOrFind(const XMLCh* const newString)
00053 {
00054     unsigned int id = fConstPool->getId(newString);
00055     if(id)
00056         return id;
00057     // might have to add it to our own table.
00058     // synchronize this bit
00059     unsigned int constCount = fConstPool->getStringCount();
00060     XMLMutexLock lockInit(&fMutex);
00061     id = XMLStringPool::addOrFind(newString);
00062     return id+constCount;
00063 }
00064 
00065 bool XMLSynchronizedStringPool::exists(const XMLCh* const newString) const
00066 {
00067     if(fConstPool->exists(newString))
00068         return true;
00069 
00070     XMLMutexLock lockInit(&const_cast<XMLSynchronizedStringPool*>(this)->fMutex);
00071     return XMLStringPool::exists(newString);
00072 }
00073 
00074 bool XMLSynchronizedStringPool::exists(const unsigned int id) const
00075 {
00076     if (!id)
00077         return false;
00078 
00079     // First see if this id belongs to the const pool.
00080     //
00081     unsigned int constCount = fConstPool->getStringCount();
00082 
00083     if (id <= constCount)
00084       return true;
00085 
00086     // The rest needs to be synchronized.
00087     //
00088     XMLMutexLock lockInit(&const_cast<XMLSynchronizedStringPool*>(this)->fMutex);
00089     return id < fCurId + constCount;
00090 }
00091 
00092 void XMLSynchronizedStringPool::flushAll()
00093 {
00094     // don't touch const pool!
00095     XMLStringPool::flushAll();
00096 }
00097 
00098 
00099 unsigned int XMLSynchronizedStringPool::getId(const XMLCh* const toFind) const
00100 {
00101     unsigned int retVal = fConstPool->getId(toFind);
00102     if(retVal)
00103         return retVal;
00104 
00105     // make sure we return a truly unique id
00106     unsigned int constCount = fConstPool->getStringCount();
00107     XMLMutexLock lockInit(&const_cast<XMLSynchronizedStringPool*>(this)->fMutex);
00108     return XMLStringPool::getId(toFind)+constCount;
00109 }
00110 
00111 
00112 const XMLCh* XMLSynchronizedStringPool::getValueForId(const unsigned int id) const
00113 {
00114     if (id <= fConstPool->getStringCount())
00115         return fConstPool->getValueForId(id);
00116 
00117     unsigned int constCount = fConstPool->getStringCount();
00118     XMLMutexLock lockInit(&const_cast<XMLSynchronizedStringPool*>(this)->fMutex);
00119     return XMLStringPool::getValueForId(id-constCount);
00120 }
00121 
00122 unsigned int XMLSynchronizedStringPool::getStringCount() const
00123 {
00124     unsigned int constCount = fConstPool->getStringCount();
00125     XMLMutexLock lockInit(&const_cast<XMLSynchronizedStringPool*>(this)->fMutex);
00126     return fCurId+constCount-1;
00127 }
00128 
00129 XERCES_CPP_NAMESPACE_END