Home | History | Annotate | Download | only in libaddressinput
      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_REGION_DATA_H_
     16 #define I18N_ADDRESSINPUT_REGION_DATA_H_
     17 
     18 #include <libaddressinput/util/basictypes.h>
     19 
     20 #include <cassert>
     21 #include <cstddef>
     22 #include <string>
     23 #include <vector>
     24 
     25 namespace i18n {
     26 namespace addressinput {
     27 
     28 // The key and name of a region that can be used as one of the items in a
     29 // dropdown UI element.
     30 class RegionData {
     31  public:
     32   // Creates a top-level RegionData object. Use AddSubRegion() to add data below
     33   // it. Does not make a copy of data in |region_code|.
     34   explicit RegionData(const std::string& region_code);
     35 
     36   ~RegionData();
     37 
     38   // Creates a sub-level RegionData object, with this object as its parent and
     39   // owner. Does not make copies of the data in |key| or |name|.
     40   RegionData* AddSubRegion(const std::string& key, const std::string& name);
     41 
     42   const std::string& key() const { return key_; }
     43 
     44   const std::string& name() const { return name_; }
     45 
     46   bool has_parent() const { return parent_ != NULL; }
     47 
     48   // Should be called only if has_parent() returns true.
     49   const RegionData& parent() const {
     50     assert(parent_ != NULL);
     51     return *parent_;
     52   }
     53 
     54   // The caller does not own the results. The results are not NULL and have a
     55   // parent.
     56   const std::vector<const RegionData*>& sub_regions() const {
     57     return sub_regions_;
     58   }
     59 
     60  private:
     61   // Private constructor used by AddSubRegion().
     62   RegionData(const std::string& key,
     63              const std::string& name,
     64              RegionData* parent);
     65 
     66   const std::string& key_;
     67   const std::string& name_;
     68   const RegionData* const parent_;  // Not owned.
     69   std::vector<const RegionData*> sub_regions_;  // Owned.
     70 
     71   DISALLOW_COPY_AND_ASSIGN(RegionData);
     72 };
     73 
     74 }  // namespace addressinput
     75 }  // namespace i18n
     76 
     77 #endif  // I18N_ADDRESSINPUT_REGION_DATA_H_
     78