OLD | NEW |
1 // Copyright (C) 2014 Google Inc. | 1 // Copyright (C) 2014 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "ruleset.h" | 15 #include "ruleset.h" |
16 | 16 |
| 17 #include <libaddressinput/address_field.h> |
17 #include <libaddressinput/util/scoped_ptr.h> | 18 #include <libaddressinput/util/scoped_ptr.h> |
18 | 19 |
19 #include <cassert> | 20 #include <cassert> |
20 #include <cstddef> | 21 #include <cstddef> |
21 #include <map> | 22 #include <map> |
22 #include <string> | 23 #include <string> |
23 #include <utility> | |
24 | 24 |
25 #include "rule.h" | 25 #include "rule.h" |
26 #include "util/stl_util.h" | 26 #include "util/stl_util.h" |
27 | 27 |
28 namespace i18n { | 28 namespace i18n { |
29 namespace addressinput { | 29 namespace addressinput { |
30 | 30 |
31 Ruleset::Ruleset(scoped_ptr<Rule> rule) | 31 Ruleset::Ruleset(AddressField field, scoped_ptr<Rule> rule) |
32 : rule_(rule.Pass()), | 32 : field_(field), |
| 33 rule_(rule.Pass()), |
33 sub_regions_(), | 34 sub_regions_(), |
34 language_codes_() { | 35 language_codes_() { |
| 36 assert(field_ >= COUNTRY); |
| 37 assert(field_ <= DEPENDENT_LOCALITY); |
35 assert(rule_ != NULL); | 38 assert(rule_ != NULL); |
36 } | 39 } |
37 | 40 |
38 Ruleset::~Ruleset() { | 41 Ruleset::~Ruleset() { |
39 STLDeleteValues(&sub_regions_); | 42 STLDeleteValues(&sub_regions_); |
40 STLDeleteValues(&language_codes_); | 43 STLDeleteValues(&language_codes_); |
41 } | 44 } |
42 | 45 |
43 void Ruleset::AddSubRegionRuleset(const std::string& sub_region, | 46 void Ruleset::AddSubRegionRuleset(const std::string& sub_region, |
44 scoped_ptr<Ruleset> ruleset) { | 47 scoped_ptr<Ruleset> ruleset) { |
45 assert(sub_regions_.find(sub_region) == sub_regions_.end()); | 48 assert(sub_regions_.find(sub_region) == sub_regions_.end()); |
| 49 assert(ruleset != NULL); |
| 50 assert(ruleset->field() == static_cast<AddressField>(field() + 1)); |
46 sub_regions_[sub_region] = ruleset.release(); | 51 sub_regions_[sub_region] = ruleset.release(); |
47 } | 52 } |
48 | 53 |
49 void Ruleset::AddLanguageCodeRule(const std::string& language_code, | 54 void Ruleset::AddLanguageCodeRule(const std::string& language_code, |
50 scoped_ptr<Rule> rule) { | 55 scoped_ptr<Rule> rule) { |
51 assert(language_codes_.find(language_code) == language_codes_.end()); | 56 assert(language_codes_.find(language_code) == language_codes_.end()); |
| 57 assert(rule != NULL); |
52 language_codes_[language_code] = rule.release(); | 58 language_codes_[language_code] = rule.release(); |
53 } | 59 } |
54 | 60 |
| 61 Ruleset* Ruleset::GetSubRegionRuleset(const std::string& sub_region) const { |
| 62 std::map<std::string, Ruleset*>::const_iterator it = |
| 63 sub_regions_.find(sub_region); |
| 64 return it != sub_regions_.end() ? it->second : NULL; |
| 65 } |
| 66 |
| 67 const Rule& Ruleset::GetLanguageCodeRule( |
| 68 const std::string& language_code) const { |
| 69 std::map<std::string, const Rule*>::const_iterator it = |
| 70 language_codes_.find(language_code); |
| 71 return it != language_codes_.end() ? *it->second : *rule_; |
| 72 } |
| 73 |
55 } // namespace addressinput | 74 } // namespace addressinput |
56 } // namespace i18n | 75 } // namespace i18n |
OLD | NEW |