OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2011 Google Inc. |
| 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 <roubert@google.com> |
| 16 |
| 17 // RE2Cache is a simple wrapper around hash_map<> to store RE2 objects. |
| 18 // |
| 19 // To get a cached RE2 object for a regexp pattern string, create a ScopedAccess |
| 20 // object with a pointer to the cache object and the pattern string itself as |
| 21 // constructor parameters. If an RE2 object corresponding to the pattern string |
| 22 // doesn't already exist, it will be created by the access object constructor. |
| 23 // The access object implements operator const RE& and can therefore be passed |
| 24 // as an argument to any function that expects an RE2 object. |
| 25 // |
| 26 // RE2Cache cache; |
| 27 // RE2Cache::ScopedAccess foo(&cache, "foo"); |
| 28 // bool match = RE2::FullMatch("foobar", foo); |
| 29 |
| 30 #ifndef I18N_PHONENUMBERS_RE2_CACHE_H_ |
| 31 #define I18N_PHONENUMBERS_RE2_CACHE_H_ |
| 32 |
| 33 #ifdef __DEPRECATED |
| 34 #undef __DEPRECATED // Don't warn for using <hash_map>. |
| 35 #endif |
| 36 |
| 37 #include <cstddef> |
| 38 #include <hash_map> |
| 39 #include <string> |
| 40 |
| 41 #include "base/scoped_ptr.h" |
| 42 #include "base/synchronization/lock.h" |
| 43 |
| 44 namespace re2 { |
| 45 class RE2; |
| 46 } // namespace re2 |
| 47 |
| 48 namespace i18n { |
| 49 namespace phonenumbers { |
| 50 |
| 51 using re2::RE2; |
| 52 using std::string; |
| 53 using __gnu_cxx::hash_map; |
| 54 |
| 55 class RE2Cache { |
| 56 private: |
| 57 typedef hash_map<string, const RE2*> CacheImpl; |
| 58 |
| 59 public: |
| 60 explicit RE2Cache(size_t min_items); |
| 61 ~RE2Cache(); |
| 62 |
| 63 class ScopedAccess { |
| 64 public: |
| 65 ScopedAccess(RE2Cache* cache, const string& pattern); |
| 66 operator const RE2&() const { return *regexp_; } |
| 67 |
| 68 private: |
| 69 const RE2* regexp_; |
| 70 friend class RE2CacheTest_AccessConstructor_Test; |
| 71 }; |
| 72 |
| 73 private: |
| 74 base::Lock lock_; // protects cache_impl_ |
| 75 scoped_ptr<CacheImpl> cache_impl_; // protected by lock_ |
| 76 friend class RE2CacheTest_CacheConstructor_Test; |
| 77 friend class RE2CacheTest_AccessConstructor_Test; |
| 78 }; |
| 79 |
| 80 } // namespace phonenumbers |
| 81 } // namespace i18n |
| 82 |
| 83 #endif // I18N_PHONENUMBERS_RE2_CACHE_H_ |
OLD | NEW |