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 | |
16 | |
17 // RegExpCache is a simple wrapper around hash_map<> to store RegExp objects. | |
18 // | |
19 // To get a cached RegExp object for a regexp pattern string, call the | |
20 // GetRegExp() method of the class RegExpCache providing the pattern string. If | |
21 // a RegExp object corresponding to the pattern string doesn't already exist, it | |
22 // will be created by the GetRegExp() method. | |
23 // | |
24 // RegExpCache cache; | |
25 // const RegExp& regexp = cache.GetRegExp("\d"); | |
26 | |
27 #ifndef I18N_PHONENUMBERS_REGEXP_CACHE_H_ | |
28 #define I18N_PHONENUMBERS_REGEXP_CACHE_H_ | |
29 | |
30 #include <cstddef> | |
31 #include <string> | |
32 | |
33 #include "base/basictypes.h" | |
34 #include "base/memory/scoped_ptr.h" | |
35 #include "base/synchronization/lock.h" | |
36 | |
37 #ifdef USE_TR1_UNORDERED_MAP | |
38 # include <tr1/unordered_map> | |
39 #elif defined(USE_HASH_MAP) | |
40 # include "base/hash_tables.h" | |
41 #else | |
42 # error STL map type unsupported on this platform! | |
43 #endif | |
44 | |
45 namespace i18n { | |
46 namespace phonenumbers { | |
47 | |
48 using std::string; | |
49 | |
50 class RegExp; | |
51 | |
52 class RegExpCache { | |
53 private: | |
54 #ifdef USE_TR1_UNORDERED_MAP | |
55 typedef std::tr1::unordered_map<string, const RegExp*> CacheImpl; | |
56 #elif defined(USE_HASH_MAP) | |
57 typedef base::hash_map<string, const RegExp*> CacheImpl; | |
58 #endif | |
59 | |
60 public: | |
61 explicit RegExpCache(size_t min_items); | |
62 ~RegExpCache(); | |
63 | |
64 const RegExp& GetRegExp(const string& pattern); | |
65 | |
66 private: | |
67 base::Lock lock_; // protects cache_impl_ | |
68 scoped_ptr<CacheImpl> cache_impl_; // protected by lock_ | |
69 friend class RegExpCacheTest_CacheConstructor_Test; | |
70 DISALLOW_COPY_AND_ASSIGN(RegExpCache); | |
71 }; | |
72 | |
73 } // namespace phonenumbers | |
74 } // namespace i18n | |
75 | |
76 #endif // I18N_PHONENUMBERS_REGEXP_CACHE_H_ | |
OLD | NEW |