blob: 44c90a8d7a43a22ae25d4be0c5ba6cba89b1f141 [file] [log] [blame]
Darin Petkov63138a92012-02-06 14:09:15 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07002// 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 Shienbrood3e20a232012-02-16 11:35:56 -05007#include <base/stl_util.h>
mukesh agrawal7a4e4002011-09-06 11:26:05 -07008
Christopher Wileyb691efd2012-08-09 13:51:51 -07009#include "shill/logging.h"
10
mukesh agrawal7a4e4002011-09-06 11:26:05 -070011using std::map;
12using std::string;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070013using std::vector;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070014
15namespace shill {
16
17KeyValueStore::KeyValueStore() {}
18
Paul Stewartdef189e2012-08-02 20:12:09 -070019void KeyValueStore::Clear() {
20 bool_properties_.clear();
21 int_properties_.clear();
22 string_properties_.clear();
23 uint_properties_.clear();
24}
25
Paul Stewartd2e1c362013-03-03 19:06:07 -080026void 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 agrawal7a4e4002011-09-06 11:26:05 -070033bool KeyValueStore::ContainsBool(const string &name) const {
34 return ContainsKey(bool_properties_, name);
35}
36
Paul Stewart1062d9d2012-04-27 10:42:27 -070037bool KeyValueStore::ContainsInt(const string &name) const {
38 return ContainsKey(int_properties_, name);
39}
40
mukesh agrawal7a4e4002011-09-06 11:26:05 -070041bool KeyValueStore::ContainsString(const string &name) const {
42 return ContainsKey(string_properties_, name);
43}
44
Paul Stewart3c5e2b32013-06-13 07:49:49 -070045bool KeyValueStore::ContainsStrings(const string &name) const {
46 return ContainsKey(strings_properties_, name);
47}
48
Darin Petkov63138a92012-02-06 14:09:15 +010049bool KeyValueStore::ContainsUint(const string &name) const {
50 return ContainsKey(uint_properties_, name);
51}
52
mukesh agrawal7a4e4002011-09-06 11:26:05 -070053bool KeyValueStore::GetBool(const string &name) const {
54 map<string, bool>::const_iterator it(bool_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -070055 CHECK(it != bool_properties_.end()) << "for bool property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070056 return it->second;
57}
58
Paul Stewart1062d9d2012-04-27 10:42:27 -070059int32 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 agrawal7a4e4002011-09-06 11:26:05 -070065const string &KeyValueStore::GetString(const string &name) const {
66 map<string, string>::const_iterator it(string_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -070067 CHECK(it != string_properties_.end()) << "for string property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070068 return it->second;
69}
70
Paul Stewart3c5e2b32013-06-13 07:49:49 -070071const 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 Petkov63138a92012-02-06 14:09:15 +010077uint32 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 agrawal7a4e4002011-09-06 11:26:05 -070083void KeyValueStore::SetBool(const string &name, bool value) {
84 bool_properties_[name] = value;
85}
86
Paul Stewart1062d9d2012-04-27 10:42:27 -070087void KeyValueStore::SetInt(const string &name, int32 value) {
88 int_properties_[name] = value;
89}
90
mukesh agrawal7a4e4002011-09-06 11:26:05 -070091void KeyValueStore::SetString(const string &name, const string &value) {
92 string_properties_[name] = value;
93}
94
Paul Stewart3c5e2b32013-06-13 07:49:49 -070095void KeyValueStore::SetStrings(const string &name,
96 const vector<string> &value) {
97 strings_properties_[name] = value;
98}
99
Darin Petkov63138a92012-02-06 14:09:15 +0100100void KeyValueStore::SetUint(const string &name, uint32 value) {
101 uint_properties_[name] = value;
102}
103
Paul Stewart88125fb2012-03-26 07:13:51 -0700104void KeyValueStore::RemoveString(const string &name) {
105 string_properties_.erase(name);
106}
107
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700108void KeyValueStore::RemoveStrings(const string &name) {
109 strings_properties_.erase(name);
110}
111
Paul Stewart1062d9d2012-04-27 10:42:27 -0700112void KeyValueStore::RemoveInt(const string &name) {
113 int_properties_.erase(name);
114}
115
Darin Petkovb536a742012-04-26 11:31:28 +0200116bool 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 Petkov7f060332012-03-14 11:46:47 +0100121string KeyValueStore::LookupString(const string &name,
122 const string &default_value) const {
Darin Petkovb536a742012-04-26 11:31:28 +0200123 map<string, string>::const_iterator it(string_properties_.find(name));
124 return it == string_properties_.end() ? default_value : it->second;
Darin Petkov7f060332012-03-14 11:46:47 +0100125}
126
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700127} // namespace shill