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 <cstdint>
21 #include <map>
22 #include <string>
23 
24 #include "phonenumbers/base/basictypes.h"
25 #include "phonenumbers/base/memory/scoped_ptr.h"
26 
27 namespace i18n {
28 namespace phonenumbers {
29 
30 using std::map;
31 using std::string;
32 
33 class DefaultMapStorage;
34 class PhoneNumber;
35 class PhoneNumberUtil;
36 struct PrefixDescriptions;
37 
38 // A utility that maps phone number prefixes to a string describing the
39 // geographical area the prefix covers.
40 class AreaCodeMap {
41  public:
42   AreaCodeMap();
43 
44   // This type is neither copyable nor movable.
45   AreaCodeMap(const AreaCodeMap&) = delete;
46   AreaCodeMap& operator=(const AreaCodeMap&) = delete;
47 
48   ~AreaCodeMap();
49 
50   // Returns the description of the geographical area the number corresponds
51   // to. This method distinguishes the case of an invalid prefix and a prefix
52   // for which the name is not available in the current language. If the
53   // description is not available in the current language an empty string is
54   // returned. If no description was found for the provided number, null is
55   // returned.
56   const char* Lookup(const PhoneNumber& number) const;
57 
58   // Creates an AreaCodeMap initialized with area_codes. Note that the
59   // underlying implementation of this method is expensive thus should
60   // not be called by time-critical applications.
61   //
62   // area_codes maps phone number prefixes to geographical area description.
63   void ReadAreaCodeMap(const PrefixDescriptions* descriptions);
64 
65  private:
66   // Does a binary search for value in the provided array from start to end
67   // (inclusive). Returns the position if {@code value} is found; otherwise,
68   // returns the position which has the largest value that is less than value.
69   // This means if value is the smallest, -1 will be returned.
70   int BinarySearch(int start, int end, int64_t value) const;
71 
72   const PhoneNumberUtil& phone_util_;
73   scoped_ptr<const DefaultMapStorage> storage_;
74 };
75 
76 }  // namespace phonenumbers
77 }  // namespace i18n
78 
79 #endif /* I18N_PHONENUMBERS_AREA_CODE_MAP_H_ */
80