OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2013 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 #include "rule_retriever.h" |
| 16 |
| 17 #include <libaddressinput/callback.h> |
| 18 #include <libaddressinput/util/basictypes.h> |
| 19 #include <libaddressinput/util/scoped_ptr.h> |
| 20 |
| 21 #include <cassert> |
| 22 #include <cstddef> |
| 23 #include <string> |
| 24 |
| 25 #include "retriever.h" |
| 26 #include "rule.h" |
| 27 |
| 28 namespace i18n { |
| 29 namespace addressinput { |
| 30 |
| 31 namespace { |
| 32 |
| 33 class Helper { |
| 34 public: |
| 35 Helper(const std::string& key, |
| 36 const RuleRetriever::Callback& rule_ready, |
| 37 const Retriever& data_retriever) |
| 38 : rule_ready_(rule_ready), |
| 39 data_retrieved_(BuildCallback(this, &Helper::OnDataRetrieved)) { |
| 40 data_retriever.Retrieve(key, *data_retrieved_); |
| 41 } |
| 42 |
| 43 private: |
| 44 ~Helper() {} |
| 45 |
| 46 void OnDataRetrieved(bool success, |
| 47 const std::string& key, |
| 48 const std::string& data) { |
| 49 Rule rule; |
| 50 if (!success) { |
| 51 rule_ready_(false, key, rule); |
| 52 } else { |
| 53 success = rule.ParseSerializedRule(data); |
| 54 rule_ready_(success, key, rule); |
| 55 } |
| 56 delete this; |
| 57 } |
| 58 |
| 59 const RuleRetriever::Callback& rule_ready_; |
| 60 scoped_ptr<Retriever::Callback> data_retrieved_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(Helper); |
| 63 }; |
| 64 |
| 65 } // namespace |
| 66 |
| 67 RuleRetriever::RuleRetriever(const Retriever* retriever) |
| 68 : data_retriever_(retriever) { |
| 69 assert(data_retriever_ != NULL); |
| 70 } |
| 71 |
| 72 RuleRetriever::~RuleRetriever() {} |
| 73 |
| 74 void RuleRetriever::RetrieveRule(const std::string& key, |
| 75 const Callback& rule_ready) const { |
| 76 new Helper(key, rule_ready, *data_retriever_); |
| 77 } |
| 78 |
| 79 } // namespace addressinput |
| 80 } // namespace i18n |
OLD | NEW |