24df22d1f05c969b6c23e04442b6f002f4b6b2f0
[platform/upstream/libphonenumber.git] / cpp / src / phonenumbers / geocoding / area_code_map.h
1 // Copyright (C) 2012 The Libphonenumber Authors
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: Patrick Mezard
16
17 #ifndef I18N_PHONENUMBERS_AREA_CODE_MAP_H_
18 #define I18N_PHONENUMBERS_AREA_CODE_MAP_H_
19
20 #include <map>
21 #include <string>
22
23 #include "base/basictypes.h"
24 #include "base/memory/scoped_ptr.h"
25
26 namespace i18n {
27 namespace phonenumbers {
28
29 using std::map;
30 using std::string;
31
32 class AreaCodeMapStorageStrategy;
33 class PhoneNumber;
34 class PhoneNumberUtil;
35
36 // A utility that maps phone number prefixes to a string describing the
37 // geographical area the prefix covers.
38 class AreaCodeMap {
39  public:
40   AreaCodeMap();
41   ~AreaCodeMap();
42
43   // Returns the description of the geographical area the number corresponds
44   // to. This method distinguishes the case of an invalid prefix and a prefix
45   // for which the name is not available in the current language. If the
46   // description is not available in the current language an empty string is
47   // returned. If no description was found for the provided number, null is
48   // returned.
49   const string* Lookup(const PhoneNumber& number) const;
50
51   // Creates an AreaCodeMap initialized with area_codes. Note that the
52   // underlying implementation of this method is expensive thus should
53   // not be called by time-critical applications.
54   //
55   // area_codes maps phone number prefixes to geographical area description.
56   void ReadAreaCodeMap(const map<int, string>& area_codes);
57
58  private:
59   AreaCodeMapStorageStrategy* CreateDefaultMapStorage() const;
60
61   // Does a binary search for value in the provided array from start to end
62   // (inclusive). Returns the position if {@code value} is found; otherwise,
63   // returns the position which has the largest value that is less than value.
64   // This means if value is the smallest, -1 will be returned.
65   int BinarySearch(int start, int end, int64 value) const;
66
67   const PhoneNumberUtil& phone_util_;
68   scoped_ptr<const AreaCodeMapStorageStrategy> storage_;
69
70   DISALLOW_COPY_AND_ASSIGN(AreaCodeMap);
71 };
72
73 }  // namespace phonenumbers
74 }  // namespace i18n
75
76 #endif /* I18N_PHONENUMBERS_AREA_CODE_MAP_H_ */