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 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 21 | bool KeyValueStore::ContainsInt(const string &name) const { |
| 22 | return ContainsKey(int_properties_, name); |
| 23 | } |
| 24 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 25 | bool KeyValueStore::ContainsString(const string &name) const { |
| 26 | return ContainsKey(string_properties_, name); |
| 27 | } |
| 28 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 29 | bool KeyValueStore::ContainsUint(const string &name) const { |
| 30 | return ContainsKey(uint_properties_, name); |
| 31 | } |
| 32 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 33 | bool KeyValueStore::GetBool(const string &name) const { |
| 34 | map<string, bool>::const_iterator it(bool_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 35 | CHECK(it != bool_properties_.end()) << "for bool property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 36 | return it->second; |
| 37 | } |
| 38 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 39 | int32 KeyValueStore::GetInt(const string &name) const { |
| 40 | map<string, int32>::const_iterator it(int_properties_.find(name)); |
| 41 | CHECK(it != int_properties_.end()) << "for int property " << name; |
| 42 | return it->second; |
| 43 | } |
| 44 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 45 | const string &KeyValueStore::GetString(const string &name) const { |
| 46 | map<string, string>::const_iterator it(string_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 47 | CHECK(it != string_properties_.end()) << "for string property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 48 | return it->second; |
| 49 | } |
| 50 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 51 | uint32 KeyValueStore::GetUint(const string &name) const { |
| 52 | map<string, uint32>::const_iterator it(uint_properties_.find(name)); |
| 53 | CHECK(it != uint_properties_.end()) << "for uint property " << name; |
| 54 | return it->second; |
| 55 | } |
| 56 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 57 | void KeyValueStore::SetBool(const string &name, bool value) { |
| 58 | bool_properties_[name] = value; |
| 59 | } |
| 60 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 61 | void KeyValueStore::SetInt(const string &name, int32 value) { |
| 62 | int_properties_[name] = value; |
| 63 | } |
| 64 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 65 | void KeyValueStore::SetString(const string &name, const string &value) { |
| 66 | string_properties_[name] = value; |
| 67 | } |
| 68 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 69 | void KeyValueStore::SetUint(const string &name, uint32 value) { |
| 70 | uint_properties_[name] = value; |
| 71 | } |
| 72 | |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 73 | void KeyValueStore::RemoveString(const string &name) { |
| 74 | string_properties_.erase(name); |
| 75 | } |
| 76 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 77 | void KeyValueStore::RemoveInt(const string &name) { |
| 78 | int_properties_.erase(name); |
| 79 | } |
| 80 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 81 | bool KeyValueStore::LookupBool(const string &name, bool default_value) const { |
| 82 | map<string, bool>::const_iterator it(bool_properties_.find(name)); |
| 83 | return it == bool_properties_.end() ? default_value : it->second; |
| 84 | } |
| 85 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 86 | string KeyValueStore::LookupString(const string &name, |
| 87 | const string &default_value) const { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 88 | map<string, string>::const_iterator it(string_properties_.find(name)); |
| 89 | return it == string_properties_.end() ? default_value : it->second; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 90 | } |
| 91 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 92 | } // namespace shill |