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