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 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/stl_util.h> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 8 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 9 | #include "shill/logging.h" |
| 10 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 11 | using std::map; |
| 12 | using std::string; |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 13 | using std::vector; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | KeyValueStore::KeyValueStore() {} |
| 18 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 19 | void KeyValueStore::Clear() { |
| 20 | bool_properties_.clear(); |
| 21 | int_properties_.clear(); |
| 22 | string_properties_.clear(); |
| 23 | uint_properties_.clear(); |
| 24 | } |
| 25 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 26 | void KeyValueStore::CopyFrom(const KeyValueStore &b) { |
| 27 | bool_properties_ = b.bool_properties_; |
| 28 | int_properties_ = b.int_properties_; |
| 29 | string_properties_ = b.string_properties_; |
| 30 | uint_properties_ = b.uint_properties_; |
| 31 | } |
| 32 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 33 | bool KeyValueStore::ContainsBool(const string &name) const { |
| 34 | return ContainsKey(bool_properties_, name); |
| 35 | } |
| 36 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 37 | bool KeyValueStore::ContainsInt(const string &name) const { |
| 38 | return ContainsKey(int_properties_, name); |
| 39 | } |
| 40 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 41 | bool KeyValueStore::ContainsString(const string &name) const { |
| 42 | return ContainsKey(string_properties_, name); |
| 43 | } |
| 44 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 45 | bool KeyValueStore::ContainsStrings(const string &name) const { |
| 46 | return ContainsKey(strings_properties_, name); |
| 47 | } |
| 48 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 49 | bool KeyValueStore::ContainsUint(const string &name) const { |
| 50 | return ContainsKey(uint_properties_, name); |
| 51 | } |
| 52 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 53 | bool KeyValueStore::GetBool(const string &name) const { |
| 54 | map<string, bool>::const_iterator it(bool_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 55 | CHECK(it != bool_properties_.end()) << "for bool property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 56 | return it->second; |
| 57 | } |
| 58 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 59 | int32 KeyValueStore::GetInt(const string &name) const { |
| 60 | map<string, int32>::const_iterator it(int_properties_.find(name)); |
| 61 | CHECK(it != int_properties_.end()) << "for int property " << name; |
| 62 | return it->second; |
| 63 | } |
| 64 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 65 | const string &KeyValueStore::GetString(const string &name) const { |
| 66 | map<string, string>::const_iterator it(string_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 67 | CHECK(it != string_properties_.end()) << "for string property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 68 | return it->second; |
| 69 | } |
| 70 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 71 | const vector<string> &KeyValueStore::GetStrings(const string &name) const { |
| 72 | const auto it(strings_properties_.find(name)); |
| 73 | CHECK(it != strings_properties_.end()) << "for strings property " << name; |
| 74 | return it->second; |
| 75 | } |
| 76 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 77 | uint32 KeyValueStore::GetUint(const string &name) const { |
| 78 | map<string, uint32>::const_iterator it(uint_properties_.find(name)); |
| 79 | CHECK(it != uint_properties_.end()) << "for uint property " << name; |
| 80 | return it->second; |
| 81 | } |
| 82 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 83 | void KeyValueStore::SetBool(const string &name, bool value) { |
| 84 | bool_properties_[name] = value; |
| 85 | } |
| 86 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 87 | void KeyValueStore::SetInt(const string &name, int32 value) { |
| 88 | int_properties_[name] = value; |
| 89 | } |
| 90 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 91 | void KeyValueStore::SetString(const string &name, const string &value) { |
| 92 | string_properties_[name] = value; |
| 93 | } |
| 94 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 95 | void KeyValueStore::SetStrings(const string &name, |
| 96 | const vector<string> &value) { |
| 97 | strings_properties_[name] = value; |
| 98 | } |
| 99 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 100 | void KeyValueStore::SetUint(const string &name, uint32 value) { |
| 101 | uint_properties_[name] = value; |
| 102 | } |
| 103 | |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 104 | void KeyValueStore::RemoveString(const string &name) { |
| 105 | string_properties_.erase(name); |
| 106 | } |
| 107 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 108 | void KeyValueStore::RemoveStrings(const string &name) { |
| 109 | strings_properties_.erase(name); |
| 110 | } |
| 111 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 112 | void KeyValueStore::RemoveInt(const string &name) { |
| 113 | int_properties_.erase(name); |
| 114 | } |
| 115 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 116 | bool KeyValueStore::LookupBool(const string &name, bool default_value) const { |
| 117 | map<string, bool>::const_iterator it(bool_properties_.find(name)); |
| 118 | return it == bool_properties_.end() ? default_value : it->second; |
| 119 | } |
| 120 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 121 | string KeyValueStore::LookupString(const string &name, |
| 122 | const string &default_value) const { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 123 | map<string, string>::const_iterator it(string_properties_.find(name)); |
| 124 | return it == string_properties_.end() ? default_value : it->second; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 125 | } |
| 126 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 127 | } // namespace shill |