11767c5feSopenharmony_ci// Copyright (C) 2011 The Libphonenumber Authors 21767c5feSopenharmony_ci// 31767c5feSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 41767c5feSopenharmony_ci// you may not use this file except in compliance with the License. 51767c5feSopenharmony_ci// You may obtain a copy of the License at 61767c5feSopenharmony_ci// 71767c5feSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 81767c5feSopenharmony_ci// 91767c5feSopenharmony_ci// Unless required by applicable law or agreed to in writing, software 101767c5feSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 111767c5feSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121767c5feSopenharmony_ci// See the License for the specific language governing permissions and 131767c5feSopenharmony_ci// limitations under the License. 141767c5feSopenharmony_ci 151767c5feSopenharmony_ci// Author: Fredrik Roubert 161767c5feSopenharmony_ci 171767c5feSopenharmony_ci// RegExpCache is a simple wrapper around hash_map<> to store RegExp objects. 181767c5feSopenharmony_ci// 191767c5feSopenharmony_ci// To get a cached RegExp object for a regexp pattern string, call the 201767c5feSopenharmony_ci// GetRegExp() method of the class RegExpCache providing the pattern string. If 211767c5feSopenharmony_ci// a RegExp object corresponding to the pattern string doesn't already exist, it 221767c5feSopenharmony_ci// will be created by the GetRegExp() method. 231767c5feSopenharmony_ci// 241767c5feSopenharmony_ci// RegExpCache cache; 251767c5feSopenharmony_ci// const RegExp& regexp = cache.GetRegExp("\d"); 261767c5feSopenharmony_ci 271767c5feSopenharmony_ci#ifndef I18N_PHONENUMBERS_REGEXP_CACHE_H_ 281767c5feSopenharmony_ci#define I18N_PHONENUMBERS_REGEXP_CACHE_H_ 291767c5feSopenharmony_ci 301767c5feSopenharmony_ci#include <cstddef> 311767c5feSopenharmony_ci#include <string> 321767c5feSopenharmony_ci 331767c5feSopenharmony_ci#include "phonenumbers/base/basictypes.h" 341767c5feSopenharmony_ci#include "phonenumbers/base/memory/scoped_ptr.h" 351767c5feSopenharmony_ci#include "phonenumbers/base/synchronization/lock.h" 361767c5feSopenharmony_ci 371767c5feSopenharmony_ci#ifdef I18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP 381767c5feSopenharmony_ci# include <tr1/unordered_map> 391767c5feSopenharmony_ci#else 401767c5feSopenharmony_ci# include <map> 411767c5feSopenharmony_ci#endif 421767c5feSopenharmony_ci 431767c5feSopenharmony_cinamespace i18n { 441767c5feSopenharmony_cinamespace phonenumbers { 451767c5feSopenharmony_ci 461767c5feSopenharmony_ciusing std::string; 471767c5feSopenharmony_ci 481767c5feSopenharmony_ciclass AbstractRegExpFactory; 491767c5feSopenharmony_ciclass RegExp; 501767c5feSopenharmony_ci 511767c5feSopenharmony_ciclass RegExpCache { 521767c5feSopenharmony_ci private: 531767c5feSopenharmony_ci#ifdef I18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP 541767c5feSopenharmony_ci typedef std::tr1::unordered_map<string, const RegExp*> CacheImpl; 551767c5feSopenharmony_ci#else 561767c5feSopenharmony_ci typedef std::map<string, const RegExp*> CacheImpl; 571767c5feSopenharmony_ci#endif 581767c5feSopenharmony_ci 591767c5feSopenharmony_ci public: 601767c5feSopenharmony_ci explicit RegExpCache(const AbstractRegExpFactory& regexp_factory, 611767c5feSopenharmony_ci size_t min_items); 621767c5feSopenharmony_ci // This type is neither copyable nor movable. 631767c5feSopenharmony_ci RegExpCache(const RegExpCache&) = delete; 641767c5feSopenharmony_ci RegExpCache& operator=(const RegExpCache&) = delete; 651767c5feSopenharmony_ci 661767c5feSopenharmony_ci ~RegExpCache(); 671767c5feSopenharmony_ci 681767c5feSopenharmony_ci const RegExp& GetRegExp(const string& pattern); 691767c5feSopenharmony_ci 701767c5feSopenharmony_ci private: 711767c5feSopenharmony_ci const AbstractRegExpFactory& regexp_factory_; 721767c5feSopenharmony_ci Lock lock_; // protects cache_impl_ 731767c5feSopenharmony_ci scoped_ptr<CacheImpl> cache_impl_; // protected by lock_ 741767c5feSopenharmony_ci friend class RegExpCacheTest_CacheConstructor_Test; 751767c5feSopenharmony_ci}; 761767c5feSopenharmony_ci 771767c5feSopenharmony_ci} // namespace phonenumbers 781767c5feSopenharmony_ci} // namespace i18n 791767c5feSopenharmony_ci 801767c5feSopenharmony_ci#endif // I18N_PHONENUMBERS_REGEXP_CACHE_H_ 81