Home | History | Annotate | Download | only in test
      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 "fake_storage.h"
     16 
     17 #include <cassert>
     18 #include <cstddef>
     19 #include <map>
     20 #include <string>
     21 #include <utility>
     22 
     23 namespace i18n {
     24 namespace addressinput {
     25 
     26 FakeStorage::FakeStorage() {}
     27 
     28 FakeStorage::~FakeStorage() {
     29   for (std::map<std::string, std::string*>::const_iterator
     30        it = data_.begin(); it != data_.end(); ++it) {
     31     delete it->second;
     32   }
     33 }
     34 
     35 void FakeStorage::Put(const std::string& key, std::string* data) {
     36   assert(data != NULL);
     37   std::pair<std::map<std::string, std::string*>::iterator, bool> result =
     38       data_.insert(std::make_pair(key, data));
     39   if (!result.second) {
     40     // Replace data in existing entry for this key.
     41     delete result.first->second;
     42     result.first->second = data;
     43   }
     44 }
     45 
     46 void FakeStorage::Get(const std::string& key,
     47                       const Callback& data_ready) const {
     48   std::map<std::string, std::string*>::const_iterator data_it = data_.find(key);
     49   bool success = data_it != data_.end();
     50   data_ready(success, key, success ? new std::string(*data_it->second) : NULL);
     51 }
     52 
     53 }  // namespace addressinput
     54 }  // namespace i18n
     55