tokenfactory.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 550 行 · 第 1/2 页
CPP
550 行
/* * Copyright 2001,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Log: TokenFactory.cpp,v $ * Revision 1.13 2004/09/08 13:56:47 peiyongz * Apache License Version 2.0 * * Revision 1.12 2004/01/29 11:51:21 cargilld * Code cleanup changes to get rid of various compiler diagnostic messages. * * Revision 1.11 2004/01/09 22:41:58 knoaman * Use a global static mutex for locking when creating local static mutexes instead of compareAndSwap * * Revision 1.10 2003/12/17 00:18:37 cargilld * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data. * * Revision 1.9 2003/10/17 16:44:34 knoaman * Fix multithreading problem. * * Revision 1.8 2003/05/18 14:02:06 knoaman * Memory manager implementation: pass per instance manager. * * Revision 1.7 2003/05/16 21:37:00 knoaman * Memory manager implementation: Modify constructors to pass in the memory manager. * * Revision 1.6 2003/05/16 00:03:10 knoaman * Partial implementation of the configurable memory manager. * * Revision 1.5 2003/03/04 21:11:12 knoaman * [Bug 17516] Thread safety problems in ../util/ and ../util/regx. * * Revision 1.4 2002/12/24 17:59:07 tng * Build with ICU 2.4 * * Revision 1.3 2002/11/04 15:17:00 tng * C++ Namespace Support. * * Revision 1.2 2002/03/18 19:29:53 knoaman * Change constant names to eliminate possible conflict with user defined ones. * * Revision 1.1.1.1 2002/02/01 22:22:31 peiyongz * sane_include * * Revision 1.5 2001/06/07 20:55:39 tng * Fix no newline at the end warning. By Pei Yong Zhang. * * Revision 1.4 2001/05/11 13:26:51 tng * Copyright update. * * Revision 1.3 2001/05/03 18:17:52 knoaman * Some design changes: * o Changed the TokenFactory from a single static instance, to a * normal class. Each RegularExpression object will have its own * instance of TokenFactory, and that instance will be passed to * other classes that need to use a TokenFactory to create Token * objects (with the exception of RangeTokenMap). * o Added a new class RangeTokenMap to map a the different ranges * in a given category to a specific RangeFactory object. In the old * design RangeFactory had dual functionality (act as a Map, and as * a factory for creating RangeToken(s)). The RangeTokenMap will * have its own copy of the TokenFactory. There will be only one * instance of the RangeTokenMap class, and that instance will be * lazily deleted when XPlatformUtils::Terminate is called. * * Revision 1.2 2001/03/22 13:23:34 knoaman * Minor modifications to eliminate compiler warnings. * * Revision 1.1 2001/03/02 19:23:00 knoaman * Schema: Regular expression handling part I * */// ---------------------------------------------------------------------------// Includes// ---------------------------------------------------------------------------#include <xercesc/util/regx/TokenFactory.hpp>#include <xercesc/util/regx/TokenInc.hpp>#include <xercesc/util/regx/XMLRangeFactory.hpp>#include <xercesc/util/regx/ASCIIRangeFactory.hpp>#include <xercesc/util/regx/UnicodeRangeFactory.hpp>#include <xercesc/util/regx/BlockRangeFactory.hpp>#include <xercesc/util/regx/RangeTokenMap.hpp>#include <xercesc/util/regx/RegxDefs.hpp>#include <xercesc/util/XMLRegisterCleanup.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------// Static member data initialization// ---------------------------------------------------------------------------bool TokenFactory::fRangeInitialized = false;// ---------------------------------------------------------------------------// Local static data// ---------------------------------------------------------------------------static bool sTokFactoryMutexRegistered = false;static XMLMutex* sTokFactoryMutex = 0;static XMLRegisterCleanup tokenFactoryMutexCleanup;// ---------------------------------------------------------------------------// Local, static functions// ---------------------------------------------------------------------------// Cleanup for the TokenFactory mutexvoid TokenFactory::reinitTokenFactoryMutex(){ delete sTokFactoryMutex; sTokFactoryMutex = 0; sTokFactoryMutexRegistered = false;}// We need to fault in this mutex. But, since its used for synchronization// itself, we have to do this the low level way using a compare and swap.static XMLMutex& gTokenFactoryMutex(){ if (!sTokFactoryMutexRegistered) { XMLMutexLock lock(XMLPlatformUtils::fgAtomicMutex); if (!sTokFactoryMutexRegistered) { sTokFactoryMutex = new XMLMutex; tokenFactoryMutexCleanup.registerCleanup(TokenFactory::reinitTokenFactoryMutex); sTokFactoryMutexRegistered = true; } } return *sTokFactoryMutex;}// ---------------------------------------------------------------------------// TokenFactory: Constructors and Destructor// ---------------------------------------------------------------------------TokenFactory::TokenFactory(MemoryManager* const manager) : fTokens(new (manager) RefVectorOf<Token> (16, true, manager)) , fEmpty(0) , fLineBegin(0) , fLineBegin2(0) , fLineEnd(0) , fStringBegin(0) , fStringEnd(0) , fStringEnd2(0) , fWordEdge(0) , fNotWordEdge(0) , fWordEnd(0) , fWordBegin(0) , fDot(0) , fCombiningChar(0) , fGrapheme(0) , fMemoryManager(manager){}TokenFactory::~TokenFactory() { delete fTokens; fTokens = 0;}// ---------------------------------------------------------------------------// TokenFactory - Factory methods// ---------------------------------------------------------------------------Token* TokenFactory::createToken(const unsigned short tokType) { if (tokType == Token::T_EMPTY && fEmpty != 0) return fEmpty; Token* tmpTok = new (fMemoryManager) Token(tokType, fMemoryManager); if (tokType == Token::T_EMPTY) { fEmpty = tmpTok; } fTokens->addElement(tmpTok); return tmpTok;}ParenToken* TokenFactory::createLook(const unsigned short tokType, Token* const token) { ParenToken* tmpTok = new (fMemoryManager) ParenToken(tokType, token, 0, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}ParenToken* TokenFactory::createParenthesis(Token* const token, const int noGroups) { ParenToken* tmpTok = new (fMemoryManager) ParenToken(Token::T_PAREN, token, noGroups, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}ClosureToken* TokenFactory::createClosure(Token* const token, bool isNonGreedy) { ClosureToken* tmpTok = isNonGreedy ? new (fMemoryManager) ClosureToken(Token::T_NONGREEDYCLOSURE, token, fMemoryManager) : new (fMemoryManager) ClosureToken(Token::T_CLOSURE, token, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}ConcatToken* TokenFactory::createConcat(Token* const token1, Token* const token2) { ConcatToken* tmpTok = new (fMemoryManager) ConcatToken(token1, token2, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}UnionToken* TokenFactory::createUnion(const bool isConcat) { UnionToken* tmpTok = isConcat ? new (fMemoryManager) UnionToken(Token::T_CONCAT, fMemoryManager) : new (fMemoryManager) UnionToken(Token::T_UNION, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}RangeToken* TokenFactory::createRange(const bool isNegRange){ RangeToken* tmpTok = isNegRange ? new (fMemoryManager) RangeToken(Token::T_NRANGE, fMemoryManager) : new (fMemoryManager) RangeToken(Token::T_RANGE, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}CharToken* TokenFactory::createChar(const XMLUInt32 ch, const bool isAnchor) { CharToken* tmpTok = isAnchor ? new (fMemoryManager) CharToken(Token::T_ANCHOR, ch, fMemoryManager) : new (fMemoryManager) CharToken(Token::T_CHAR, ch, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}StringToken* TokenFactory::createBackReference(const int noRefs) { StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_BACKREFERENCE, 0, noRefs, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}StringToken* TokenFactory::createString(const XMLCh* const literal) { StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_STRING, literal, 0, fMemoryManager); fTokens->addElement(tmpTok); return tmpTok;}ModifierToken* TokenFactory::createModifierGroup(Token* const child, const int add,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?