philip.liard@gmail.com | b905691 | 2011-08-18 11:41:24 +0000 | [diff] [blame] | 1 | // Copyright (C) 2011 The Libphonenumber Authors |
philip.liard@gmail.com | 1ad5e5b | 2011-07-01 08:22:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Author: Fredrik Roubert |
| 16 | |
| 17 | #include "phonenumbers/regexp_cache.h" |
| 18 | |
| 19 | #include <cstddef> |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | |
philip.liard@gmail.com | af4a2ce | 2013-04-30 11:35:55 +0000 | [diff] [blame] | 23 | #include "phonenumbers/base/synchronization/lock.h" |
philip.liard@gmail.com | 1ad5e5b | 2011-07-01 08:22:06 +0000 | [diff] [blame] | 24 | #include "phonenumbers/regexp_adapter.h" |
| 25 | |
| 26 | using std::string; |
| 27 | |
philip.liard@gmail.com | 1ad5e5b | 2011-07-01 08:22:06 +0000 | [diff] [blame] | 28 | namespace i18n { |
| 29 | namespace phonenumbers { |
| 30 | |
philip.liard@gmail.com | 384682a | 2011-07-12 15:41:29 +0000 | [diff] [blame] | 31 | RegExpCache::RegExpCache(const AbstractRegExpFactory& regexp_factory, |
| 32 | size_t min_items) |
| 33 | : regexp_factory_(regexp_factory), |
philip.liard@gmail.com | fa6ddee | 2013-05-03 13:49:35 +0000 | [diff] [blame] | 34 | #ifdef I18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP |
philip.liard@gmail.com | 384682a | 2011-07-12 15:41:29 +0000 | [diff] [blame] | 35 | cache_impl_(new CacheImpl(min_items)) |
philip.liard@gmail.com | e3f2e3e | 2011-07-01 09:23:29 +0000 | [diff] [blame] | 36 | #else |
philip.liard@gmail.com | 384682a | 2011-07-12 15:41:29 +0000 | [diff] [blame] | 37 | cache_impl_(new CacheImpl()) |
philip.liard@gmail.com | e3f2e3e | 2011-07-01 09:23:29 +0000 | [diff] [blame] | 38 | #endif |
| 39 | {} |
philip.liard@gmail.com | 1ad5e5b | 2011-07-01 08:22:06 +0000 | [diff] [blame] | 40 | |
| 41 | RegExpCache::~RegExpCache() { |
| 42 | AutoLock l(lock_); |
| 43 | for (CacheImpl::const_iterator |
| 44 | it = cache_impl_->begin(); it != cache_impl_->end(); ++it) { |
| 45 | delete it->second; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const RegExp& RegExpCache::GetRegExp(const string& pattern) { |
| 50 | AutoLock l(lock_); |
| 51 | CacheImpl::const_iterator it = cache_impl_->find(pattern); |
| 52 | if (it != cache_impl_->end()) return *it->second; |
| 53 | |
philip.liard@gmail.com | 384682a | 2011-07-12 15:41:29 +0000 | [diff] [blame] | 54 | const RegExp* regexp = regexp_factory_.CreateRegExp(pattern); |
philip.liard@gmail.com | 1ad5e5b | 2011-07-01 08:22:06 +0000 | [diff] [blame] | 55 | cache_impl_->insert(make_pair(pattern, regexp)); |
| 56 | return *regexp; |
| 57 | } |
| 58 | |
| 59 | } // namespace phonenumbers |
| 60 | } // namespace i18n |