• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2014 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 #ifndef I18N_ADDRESSINPUT_LOOKUP_KEY_H_
16 #define I18N_ADDRESSINPUT_LOOKUP_KEY_H_
17 
18 #include <libaddressinput/address_field.h>
19 #include <libaddressinput/util/basictypes.h>
20 
21 #include <cstddef>
22 #include <map>
23 #include <string>
24 
25 namespace i18n {
26 namespace addressinput {
27 
28 struct AddressData;
29 
30 // A LookupKey maps between an AddressData struct and the key string used to
31 // request address data from an address data server.
32 class LookupKey {
33  public:
34   // The array length is explicitly specified here, to make it possible to get
35   // the length through arraysize(LookupKey::kHierarchy).
36   static const AddressField kHierarchy[4];
37 
38   LookupKey();
39   ~LookupKey();
40 
41   // Initializes this object by parsing |address|.
42   void FromAddress(const AddressData& address);
43 
44   // Initializes this object to be a copy of |parent| key that's one level
45   // deeper with the next level node being |child_node|.
46   //
47   // For example, if |parent| is "data/US" and |child_node| is "CA", then this
48   // key becomes "data/US/CA".
49   //
50   // The |parent| can be at most LOCALITY level. The |child_node| cannot be
51   // empty.
52   void FromLookupKey(const LookupKey& parent, const std::string& child_node);
53 
54   // Returns the lookup key string (of |max_depth|).
55   std::string ToKeyString(size_t max_depth) const;
56 
57   // Returns the region code. Must not be called on an empty object.
58   const std::string& GetRegionCode() const;
59 
60   // Returns the depth. Must not be called on an empty object.
61   size_t GetDepth() const;
62 
63  private:
64   std::map<AddressField, std::string> nodes_;
65   // The language of the key, obtained from the address (empty for default
66   // language).
67   std::string language_;
68 
69   DISALLOW_COPY_AND_ASSIGN(LookupKey);
70 };
71 
72 }  // namespace addressinput
73 }  // namespace i18n
74 
75 #endif  // I18N_ADDRESSINPUT_LOOKUP_KEY_H_
76