blob: 53c97c23f8ce7f68d7aaf188294e96907826b5d3 [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
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_KEY_VALUE_STORE_H_
6#define SHILL_KEY_VALUE_STORE_H_
mukesh agrawal7a4e4002011-09-06 11:26:05 -07007
8#include <map>
9#include <string>
Paul Stewart3c5e2b32013-06-13 07:49:49 -070010#include <vector>
mukesh agrawal7a4e4002011-09-06 11:26:05 -070011
mukesh agrawal7a4e4002011-09-06 11:26:05 -070012namespace shill {
13
14class KeyValueStore {
15 // A simple store for key-value pairs, which supports (a limited set of)
16 // heterogenous value types.
17 //
18 // Compare to PropertyStore, which enables a class to (selectively)
19 // expose its instance members as properties accessible via
20 // RPC. (RPC support for ProperyStore is implemented in a
21 // protocol-specific adaptor. e.g. dbus_adpator.)
22 //
23 // Implemented separately from PropertyStore, to avoid complicating
24 // the PropertyStore interface. In particular, objects implementing the
25 // PropertyStore interface always provide the storage themselves. In
26 // contrast, users of KeyValueStore expect KeyValueStore to provide
27 // storage.
28 public:
29 KeyValueStore();
30
Paul Stewartdef189e2012-08-02 20:12:09 -070031 void Clear();
Paul Stewart8ae18742015-06-16 13:13:10 -070032 void CopyFrom(const KeyValueStore& b);
33 bool Equals(const KeyValueStore& other) const;
Paul Stewartbaf87072013-10-04 17:03:37 -070034 bool IsEmpty();
Paul Stewartdef189e2012-08-02 20:12:09 -070035
Paul Stewart8ae18742015-06-16 13:13:10 -070036 bool ContainsBool(const std::string& name) const;
37 bool ContainsInt(const std::string& name) const;
38 bool ContainsKeyValueStore(const std::string& name) const;
39 bool ContainsString(const std::string& name) const;
40 bool ContainsStringmap(const std::string& name) const;
41 bool ContainsStrings(const std::string& name) const;
42 bool ContainsUint(const std::string& name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070043
Paul Stewart8ae18742015-06-16 13:13:10 -070044 bool GetBool(const std::string& name) const;
45 int32_t GetInt(const std::string& name) const;
46 const KeyValueStore& GetKeyValueStore(const std::string& name) const;
47 const std::string& GetString(const std::string& name) const;
48 const std::map<std::string, std::string>& GetStringmap(
49 const std::string& name) const;
50 const std::vector<std::string>& GetStrings(const std::string& name) const;
51 uint32_t GetUint(const std::string& name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070052
Paul Stewart8ae18742015-06-16 13:13:10 -070053 void SetBool(const std::string& name, bool value);
54 void SetInt(const std::string& name, int32_t value);
55 void SetKeyValueStore(const std::string& name, const KeyValueStore& value);
56 void SetString(const std::string& name, const std::string& value);
57 void SetStringmap(const std::string& name,
58 const std::map<std::string, std::string>& value);
59 void SetStrings(const std::string& name,
60 const std::vector<std::string>& value);
61 void SetUint(const std::string& name, uint32_t value);
Darin Petkov63138a92012-02-06 14:09:15 +010062
Paul Stewart8ae18742015-06-16 13:13:10 -070063 void RemoveString(const std::string& name);
64 void RemoveStringmap(const std::string& name);
65 void RemoveStrings(const std::string& name);
66 void RemoveInt(const std::string& name);
67 void RemoveKeyValueStore(const std::string& name);
Paul Stewart88125fb2012-03-26 07:13:51 -070068
Darin Petkov7f060332012-03-14 11:46:47 +010069 // If |name| is in this store returns its value, otherwise returns
70 // |default_value|.
Paul Stewart8ae18742015-06-16 13:13:10 -070071 bool LookupBool(const std::string& name, bool default_value) const;
72 int LookupInt(const std::string& name, int default_value) const;
73 std::string LookupString(const std::string& name,
74 const std::string& default_value) const;
Darin Petkov7f060332012-03-14 11:46:47 +010075
Paul Stewart8ae18742015-06-16 13:13:10 -070076 const std::map<std::string, bool>& bool_properties() const {
Darin Petkov63138a92012-02-06 14:09:15 +010077 return bool_properties_;
78 }
Paul Stewart8ae18742015-06-16 13:13:10 -070079 const std::map<std::string, int32_t>& int_properties() const {
Paul Stewart1062d9d2012-04-27 10:42:27 -070080 return int_properties_;
81 }
Paul Stewartbb833562015-01-21 23:30:46 -080082 const std::map<std::string, KeyValueStore>
83 &key_value_store_properties() const {
84 return key_value_store_properties_;
85 }
Paul Stewart8ae18742015-06-16 13:13:10 -070086 const std::map<std::string, std::string>& string_properties() const {
Darin Petkov63138a92012-02-06 14:09:15 +010087 return string_properties_;
88 }
Arman Uguray631c7e42013-07-30 16:41:12 -070089 const std::map<
90 std::string,
Paul Stewart8ae18742015-06-16 13:13:10 -070091 std::map<std::string, std::string>>& stringmap_properties() const {
Arman Uguray631c7e42013-07-30 16:41:12 -070092 return stringmap_properties_;
93 }
Paul Stewart3c5e2b32013-06-13 07:49:49 -070094 const std::map<std::string,
Paul Stewart8ae18742015-06-16 13:13:10 -070095 std::vector<std::string>>& strings_properties() const {
Paul Stewart3c5e2b32013-06-13 07:49:49 -070096 return strings_properties_;
97 }
Paul Stewart8ae18742015-06-16 13:13:10 -070098 const std::map<std::string, uint32_t>& uint_properties() const {
Darin Petkov63138a92012-02-06 14:09:15 +010099 return uint_properties_;
100 }
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700101
102 private:
Paul Stewartbb833562015-01-21 23:30:46 -0800103 // Recursively compare KeyValueStore properties with |other|.
Paul Stewart8ae18742015-06-16 13:13:10 -0700104 bool KeyValueStorePropertiesAreEqual(const KeyValueStore& other) const;
Paul Stewartbb833562015-01-21 23:30:46 -0800105
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700106 std::map<std::string, bool> bool_properties_;
Ben Chan7fab8972014-08-10 17:14:46 -0700107 std::map<std::string, int32_t> int_properties_;
Paul Stewartbb833562015-01-21 23:30:46 -0800108 std::map<std::string, KeyValueStore> key_value_store_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700109 std::map<std::string, std::string> string_properties_;
Arman Uguray631c7e42013-07-30 16:41:12 -0700110 std::map<std::string,
111 std::map<std::string, std::string>> stringmap_properties_;
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700112 std::map<std::string, std::vector<std::string>> strings_properties_;
Ben Chan7fab8972014-08-10 17:14:46 -0700113 std::map<std::string, uint32_t> uint_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700114};
115
116} // namespace shill
117
Ben Chanc45688b2014-07-02 23:50:45 -0700118#endif // SHILL_KEY_VALUE_STORE_H_