Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/key_value_store.h" |
| 6 | |
| 7 | #include <base/logging.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 8 | #include <base/stl_util.h> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 9 | |
| 10 | using std::map; |
| 11 | using std::string; |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | KeyValueStore::KeyValueStore() {} |
| 16 | |
| 17 | bool KeyValueStore::ContainsBool(const string &name) const { |
| 18 | return ContainsKey(bool_properties_, name); |
| 19 | } |
| 20 | |
| 21 | bool KeyValueStore::ContainsString(const string &name) const { |
| 22 | return ContainsKey(string_properties_, name); |
| 23 | } |
| 24 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 25 | bool KeyValueStore::ContainsUint(const string &name) const { |
| 26 | return ContainsKey(uint_properties_, name); |
| 27 | } |
| 28 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 29 | bool KeyValueStore::GetBool(const string &name) const { |
| 30 | map<string, bool>::const_iterator it(bool_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 31 | CHECK(it != bool_properties_.end()) << "for bool property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 32 | return it->second; |
| 33 | } |
| 34 | |
| 35 | const string &KeyValueStore::GetString(const string &name) const { |
| 36 | map<string, string>::const_iterator it(string_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 37 | CHECK(it != string_properties_.end()) << "for string property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 38 | return it->second; |
| 39 | } |
| 40 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 41 | uint32 KeyValueStore::GetUint(const string &name) const { |
| 42 | map<string, uint32>::const_iterator it(uint_properties_.find(name)); |
| 43 | CHECK(it != uint_properties_.end()) << "for uint property " << name; |
| 44 | return it->second; |
| 45 | } |
| 46 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 47 | void KeyValueStore::SetBool(const string &name, bool value) { |
| 48 | bool_properties_[name] = value; |
| 49 | } |
| 50 | |
| 51 | void KeyValueStore::SetString(const string &name, const string &value) { |
| 52 | string_properties_[name] = value; |
| 53 | } |
| 54 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 55 | void KeyValueStore::SetUint(const string &name, uint32 value) { |
| 56 | uint_properties_[name] = value; |
| 57 | } |
| 58 | |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 59 | void KeyValueStore::RemoveString(const string &name) { |
| 60 | string_properties_.erase(name); |
| 61 | } |
| 62 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 63 | string KeyValueStore::LookupString(const string &name, |
| 64 | const string &default_value) const { |
| 65 | return ContainsString(name) ? GetString(name) : default_value; |
| 66 | } |
| 67 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 68 | } // namespace shill |