blob: 162761432da1f9bd75de288eaf6992e3265e31cb [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;
13
14namespace shill {
15
16KeyValueStore::KeyValueStore() {}
17
Paul Stewartdef189e2012-08-02 20:12:09 -070018void KeyValueStore::Clear() {
19 bool_properties_.clear();
20 int_properties_.clear();
21 string_properties_.clear();
22 uint_properties_.clear();
23}
24
mukesh agrawal7a4e4002011-09-06 11:26:05 -070025bool KeyValueStore::ContainsBool(const string &name) const {
26 return ContainsKey(bool_properties_, name);
27}
28
Paul Stewart1062d9d2012-04-27 10:42:27 -070029bool KeyValueStore::ContainsInt(const string &name) const {
30 return ContainsKey(int_properties_, name);
31}
32
mukesh agrawal7a4e4002011-09-06 11:26:05 -070033bool KeyValueStore::ContainsString(const string &name) const {
34 return ContainsKey(string_properties_, name);
35}
36
Darin Petkov63138a92012-02-06 14:09:15 +010037bool KeyValueStore::ContainsUint(const string &name) const {
38 return ContainsKey(uint_properties_, name);
39}
40
mukesh agrawal7a4e4002011-09-06 11:26:05 -070041bool KeyValueStore::GetBool(const string &name) const {
42 map<string, bool>::const_iterator it(bool_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -070043 CHECK(it != bool_properties_.end()) << "for bool property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070044 return it->second;
45}
46
Paul Stewart1062d9d2012-04-27 10:42:27 -070047int32 KeyValueStore::GetInt(const string &name) const {
48 map<string, int32>::const_iterator it(int_properties_.find(name));
49 CHECK(it != int_properties_.end()) << "for int property " << name;
50 return it->second;
51}
52
mukesh agrawal7a4e4002011-09-06 11:26:05 -070053const string &KeyValueStore::GetString(const string &name) const {
54 map<string, string>::const_iterator it(string_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -070055 CHECK(it != string_properties_.end()) << "for string property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070056 return it->second;
57}
58
Darin Petkov63138a92012-02-06 14:09:15 +010059uint32 KeyValueStore::GetUint(const string &name) const {
60 map<string, uint32>::const_iterator it(uint_properties_.find(name));
61 CHECK(it != uint_properties_.end()) << "for uint property " << name;
62 return it->second;
63}
64
mukesh agrawal7a4e4002011-09-06 11:26:05 -070065void KeyValueStore::SetBool(const string &name, bool value) {
66 bool_properties_[name] = value;
67}
68
Paul Stewart1062d9d2012-04-27 10:42:27 -070069void KeyValueStore::SetInt(const string &name, int32 value) {
70 int_properties_[name] = value;
71}
72
mukesh agrawal7a4e4002011-09-06 11:26:05 -070073void KeyValueStore::SetString(const string &name, const string &value) {
74 string_properties_[name] = value;
75}
76
Darin Petkov63138a92012-02-06 14:09:15 +010077void KeyValueStore::SetUint(const string &name, uint32 value) {
78 uint_properties_[name] = value;
79}
80
Paul Stewart88125fb2012-03-26 07:13:51 -070081void KeyValueStore::RemoveString(const string &name) {
82 string_properties_.erase(name);
83}
84
Paul Stewart1062d9d2012-04-27 10:42:27 -070085void KeyValueStore::RemoveInt(const string &name) {
86 int_properties_.erase(name);
87}
88
Darin Petkovb536a742012-04-26 11:31:28 +020089bool KeyValueStore::LookupBool(const string &name, bool default_value) const {
90 map<string, bool>::const_iterator it(bool_properties_.find(name));
91 return it == bool_properties_.end() ? default_value : it->second;
92}
93
Darin Petkov7f060332012-03-14 11:46:47 +010094string KeyValueStore::LookupString(const string &name,
95 const string &default_value) const {
Darin Petkovb536a742012-04-26 11:31:28 +020096 map<string, string>::const_iterator it(string_properties_.find(name));
97 return it == string_properties_.end() ? default_value : it->second;
Darin Petkov7f060332012-03-14 11:46:47 +010098}
99
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700100} // namespace shill