blob: d288c3934e28f4baed9a011ec5d7b6af28f20e58 [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#ifndef SHILL_KEY_VALUE_STORE_
6#define SHILL_KEY_VALUE_STORE_
7
8#include <map>
9#include <string>
10
11#include <base/basictypes.h>
12
13namespace shill {
14
15class KeyValueStore {
16 // A simple store for key-value pairs, which supports (a limited set of)
17 // heterogenous value types.
18 //
19 // Compare to PropertyStore, which enables a class to (selectively)
20 // expose its instance members as properties accessible via
21 // RPC. (RPC support for ProperyStore is implemented in a
22 // protocol-specific adaptor. e.g. dbus_adpator.)
23 //
24 // Implemented separately from PropertyStore, to avoid complicating
25 // the PropertyStore interface. In particular, objects implementing the
26 // PropertyStore interface always provide the storage themselves. In
27 // contrast, users of KeyValueStore expect KeyValueStore to provide
28 // storage.
29 public:
30 KeyValueStore();
31
32 bool ContainsBool(const std::string &name) const;
Paul Stewart1062d9d2012-04-27 10:42:27 -070033 bool ContainsInt(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070034 bool ContainsString(const std::string &name) const;
Darin Petkov63138a92012-02-06 14:09:15 +010035 bool ContainsUint(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070036
37 bool GetBool(const std::string &name) const;
Paul Stewart1062d9d2012-04-27 10:42:27 -070038 int32 GetInt(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070039 const std::string &GetString(const std::string &name) const;
Darin Petkov63138a92012-02-06 14:09:15 +010040 uint32 GetUint(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070041
42 void SetBool(const std::string &name, bool value);
Paul Stewart1062d9d2012-04-27 10:42:27 -070043 void SetInt(const std::string &name, int32 value);
Darin Petkov63138a92012-02-06 14:09:15 +010044 void SetString(const std::string& name, const std::string& value);
45 void SetUint(const std::string &name, uint32 value);
46
Paul Stewart88125fb2012-03-26 07:13:51 -070047 void RemoveString(const std::string &name);
Paul Stewart1062d9d2012-04-27 10:42:27 -070048 void RemoveInt(const std::string &name);
Paul Stewart88125fb2012-03-26 07:13:51 -070049
Darin Petkov7f060332012-03-14 11:46:47 +010050 // If |name| is in this store returns its value, otherwise returns
51 // |default_value|.
Darin Petkovb536a742012-04-26 11:31:28 +020052 bool LookupBool(const std::string &name, bool default_value) const;
Darin Petkov7f060332012-03-14 11:46:47 +010053 std::string LookupString(const std::string &name,
54 const std::string &default_value) const;
55
Darin Petkov63138a92012-02-06 14:09:15 +010056 const std::map<std::string, bool> &bool_properties() const {
57 return bool_properties_;
58 }
Paul Stewart1062d9d2012-04-27 10:42:27 -070059 const std::map<std::string, int32> &int_properties() const {
60 return int_properties_;
61 }
Darin Petkov63138a92012-02-06 14:09:15 +010062 const std::map<std::string, std::string> &string_properties() const {
63 return string_properties_;
64 }
65 const std::map<std::string, uint32> &uint_properties() const {
66 return uint_properties_;
67 }
mukesh agrawal7a4e4002011-09-06 11:26:05 -070068
69 private:
70 std::map<std::string, bool> bool_properties_;
Paul Stewart1062d9d2012-04-27 10:42:27 -070071 std::map<std::string, int32> int_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070072 std::map<std::string, std::string> string_properties_;
Darin Petkov63138a92012-02-06 14:09:15 +010073 std::map<std::string, uint32> uint_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070074};
75
76} // namespace shill
77
78#endif // SHILL_KEY_VALUE_STORE_