blob: c93651bf67e6cc5c7fae4540907054721cecb306 [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
7#include <base/logging.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -05008#include <base/stl_util.h>
mukesh agrawal7a4e4002011-09-06 11:26:05 -07009
10using std::map;
11using std::string;
12
13namespace shill {
14
15KeyValueStore::KeyValueStore() {}
16
Paul Stewartdef189e2012-08-02 20:12:09 -070017void KeyValueStore::Clear() {
18 bool_properties_.clear();
19 int_properties_.clear();
20 string_properties_.clear();
21 uint_properties_.clear();
22}
23
mukesh agrawal7a4e4002011-09-06 11:26:05 -070024bool KeyValueStore::ContainsBool(const string &name) const {
25 return ContainsKey(bool_properties_, name);
26}
27
Paul Stewart1062d9d2012-04-27 10:42:27 -070028bool KeyValueStore::ContainsInt(const string &name) const {
29 return ContainsKey(int_properties_, name);
30}
31
mukesh agrawal7a4e4002011-09-06 11:26:05 -070032bool KeyValueStore::ContainsString(const string &name) const {
33 return ContainsKey(string_properties_, name);
34}
35
Darin Petkov63138a92012-02-06 14:09:15 +010036bool KeyValueStore::ContainsUint(const string &name) const {
37 return ContainsKey(uint_properties_, name);
38}
39
mukesh agrawal7a4e4002011-09-06 11:26:05 -070040bool KeyValueStore::GetBool(const string &name) const {
41 map<string, bool>::const_iterator it(bool_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -070042 CHECK(it != bool_properties_.end()) << "for bool property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070043 return it->second;
44}
45
Paul Stewart1062d9d2012-04-27 10:42:27 -070046int32 KeyValueStore::GetInt(const string &name) const {
47 map<string, int32>::const_iterator it(int_properties_.find(name));
48 CHECK(it != int_properties_.end()) << "for int property " << name;
49 return it->second;
50}
51
mukesh agrawal7a4e4002011-09-06 11:26:05 -070052const string &KeyValueStore::GetString(const string &name) const {
53 map<string, string>::const_iterator it(string_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -070054 CHECK(it != string_properties_.end()) << "for string property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070055 return it->second;
56}
57
Darin Petkov63138a92012-02-06 14:09:15 +010058uint32 KeyValueStore::GetUint(const string &name) const {
59 map<string, uint32>::const_iterator it(uint_properties_.find(name));
60 CHECK(it != uint_properties_.end()) << "for uint property " << name;
61 return it->second;
62}
63
mukesh agrawal7a4e4002011-09-06 11:26:05 -070064void KeyValueStore::SetBool(const string &name, bool value) {
65 bool_properties_[name] = value;
66}
67
Paul Stewart1062d9d2012-04-27 10:42:27 -070068void KeyValueStore::SetInt(const string &name, int32 value) {
69 int_properties_[name] = value;
70}
71
mukesh agrawal7a4e4002011-09-06 11:26:05 -070072void KeyValueStore::SetString(const string &name, const string &value) {
73 string_properties_[name] = value;
74}
75
Darin Petkov63138a92012-02-06 14:09:15 +010076void KeyValueStore::SetUint(const string &name, uint32 value) {
77 uint_properties_[name] = value;
78}
79
Paul Stewart88125fb2012-03-26 07:13:51 -070080void KeyValueStore::RemoveString(const string &name) {
81 string_properties_.erase(name);
82}
83
Paul Stewart1062d9d2012-04-27 10:42:27 -070084void KeyValueStore::RemoveInt(const string &name) {
85 int_properties_.erase(name);
86}
87
Darin Petkovb536a742012-04-26 11:31:28 +020088bool KeyValueStore::LookupBool(const string &name, bool default_value) const {
89 map<string, bool>::const_iterator it(bool_properties_.find(name));
90 return it == bool_properties_.end() ? default_value : it->second;
91}
92
Darin Petkov7f060332012-03-14 11:46:47 +010093string KeyValueStore::LookupString(const string &name,
94 const string &default_value) const {
Darin Petkovb536a742012-04-26 11:31:28 +020095 map<string, string>::const_iterator it(string_properties_.find(name));
96 return it == string_properties_.end() ? default_value : it->second;
Darin Petkov7f060332012-03-14 11:46:47 +010097}
98
mukesh agrawal7a4e4002011-09-06 11:26:05 -070099} // namespace shill