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// A fake storage object to use in tests. Stores data in memory instead of 16// writing it to disk. All operations are synchronous. 17 18#ifndef I18N_ADDRESSINPUT_FAKE_STORAGE_H_ 19#define I18N_ADDRESSINPUT_FAKE_STORAGE_H_ 20 21#include <libaddressinput/storage.h> 22#include <libaddressinput/util/basictypes.h> 23 24#include <map> 25#include <string> 26 27namespace i18n { 28namespace addressinput { 29 30// Stores data in memory. Sample usage: 31// class MyClass { 32// public: 33// MyClass() : storage_(), 34// data_ready_(BuildCallback(this, &MyClass::OnDataReady)) {} 35// 36// ~MyClass() {} 37// 38// void Write() { 39// storage_.Put("key", "value"); 40// } 41// 42// void Read() { 43// storage_.Get("key", *data_ready_); 44// } 45// 46// private: 47// void OnDataReady(bool success, 48// const std::string& key, 49// std::string* data) { 50// ... 51// delete data; 52// } 53// 54// FakeStorage storage_; 55// const scoped_ptr<const Storage::Callback> data_ready_; 56// 57// DISALLOW_COPY_AND_ASSIGN(MyClass); 58// }; 59class FakeStorage : public Storage { 60 public: 61 FakeStorage(); 62 virtual ~FakeStorage(); 63 64 // Storage implementation. 65 virtual void Put(const std::string& key, std::string* data); 66 virtual void Get(const std::string& key, const Callback& data_ready) const; 67 68 private: 69 std::map<std::string, std::string*> data_; 70 DISALLOW_COPY_AND_ASSIGN(FakeStorage); 71}; 72 73} // namespace addressinput 74} // namespace i18n 75 76#endif // I18N_ADDRESSINPUT_FAKE_STORAGE_H_ 77