blob: ce0238b7a88322bb72e4780f87b0bd41910d19ae [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
Peter Qiud31fb582015-07-15 14:33:32 -070012#include <chromeos/variant_dictionary.h>
13
mukesh agrawal7a4e4002011-09-06 11:26:05 -070014namespace shill {
15
16class KeyValueStore {
17 // A simple store for key-value pairs, which supports (a limited set of)
18 // heterogenous value types.
19 //
20 // Compare to PropertyStore, which enables a class to (selectively)
21 // expose its instance members as properties accessible via
22 // RPC. (RPC support for ProperyStore is implemented in a
23 // protocol-specific adaptor. e.g. dbus_adpator.)
24 //
25 // Implemented separately from PropertyStore, to avoid complicating
26 // the PropertyStore interface. In particular, objects implementing the
27 // PropertyStore interface always provide the storage themselves. In
28 // contrast, users of KeyValueStore expect KeyValueStore to provide
29 // storage.
30 public:
31 KeyValueStore();
32
Peter Qiud31fb582015-07-15 14:33:32 -070033 // Required for equality comparison when KeyValueStore is wrapped inside a
34 // chromeos::Any object.
35 bool operator==(const KeyValueStore& rhs) const;
36 bool operator!=(const KeyValueStore& rhs) const;
37
Paul Stewartdef189e2012-08-02 20:12:09 -070038 void Clear();
Paul Stewart8ae18742015-06-16 13:13:10 -070039 void CopyFrom(const KeyValueStore& b);
Paul Stewartbaf87072013-10-04 17:03:37 -070040 bool IsEmpty();
Paul Stewartdef189e2012-08-02 20:12:09 -070041
Paul Stewart8ae18742015-06-16 13:13:10 -070042 bool ContainsBool(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070043 bool ContainsByteArrays(const std::string& name) const;
Paul Stewart8ae18742015-06-16 13:13:10 -070044 bool ContainsInt(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070045 bool ContainsInt16(const std::string& name) const;
Paul Stewart8ae18742015-06-16 13:13:10 -070046 bool ContainsKeyValueStore(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070047 bool ContainsRpcIdentifier(const std::string& name) const;
Paul Stewart8ae18742015-06-16 13:13:10 -070048 bool ContainsString(const std::string& name) const;
49 bool ContainsStringmap(const std::string& name) const;
50 bool ContainsStrings(const std::string& name) const;
51 bool ContainsUint(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070052 bool ContainsUint16(const std::string& name) const;
Peter Qiu937fafc2015-07-21 15:34:59 -070053 bool ContainsUint8(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070054 bool ContainsUint8s(const std::string& name) const;
55 bool ContainsUint32s(const std::string& name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070056
Paul Stewart8ae18742015-06-16 13:13:10 -070057 bool GetBool(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070058 const std::vector<std::vector<uint8_t>>& GetByteArrays(
59 const std::string& name) const;
Paul Stewart8ae18742015-06-16 13:13:10 -070060 int32_t GetInt(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070061 int16_t GetInt16(const std::string& name) const;
Paul Stewart8ae18742015-06-16 13:13:10 -070062 const KeyValueStore& GetKeyValueStore(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070063 const std::string& GetRpcIdentifier(const std::string& name) const;
Paul Stewart8ae18742015-06-16 13:13:10 -070064 const std::string& GetString(const std::string& name) const;
65 const std::map<std::string, std::string>& GetStringmap(
66 const std::string& name) const;
67 const std::vector<std::string>& GetStrings(const std::string& name) const;
68 uint32_t GetUint(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070069 uint16_t GetUint16(const std::string& name) const;
Peter Qiu937fafc2015-07-21 15:34:59 -070070 uint8_t GetUint8(const std::string& name) const;
Peter Qiubd7e9fb2015-07-09 10:29:19 -070071 const std::vector<uint8_t>& GetUint8s(const std::string& name) const;
72 const std::vector<uint32_t>& GetUint32s(const std::string& name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070073
Peter Qiu937fafc2015-07-21 15:34:59 -070074 // TODO(zqiu): remove type specific set functions and add a generic set
75 // function instead. This way, we don't need to add new functions every
76 // time we need to support a new type.
Paul Stewart8ae18742015-06-16 13:13:10 -070077 void SetBool(const std::string& name, bool value);
Peter Qiubd7e9fb2015-07-09 10:29:19 -070078 void SetByteArrays(const std::string& name,
79 const std::vector<std::vector<uint8_t>>& value);
Paul Stewart8ae18742015-06-16 13:13:10 -070080 void SetInt(const std::string& name, int32_t value);
Peter Qiubd7e9fb2015-07-09 10:29:19 -070081 void SetInt16(const std::string& name, int16_t value);
Paul Stewart8ae18742015-06-16 13:13:10 -070082 void SetKeyValueStore(const std::string& name, const KeyValueStore& value);
Peter Qiubd7e9fb2015-07-09 10:29:19 -070083 void SetRpcIdentifier(const std::string& name, const std::string& value);
Paul Stewart8ae18742015-06-16 13:13:10 -070084 void SetString(const std::string& name, const std::string& value);
85 void SetStringmap(const std::string& name,
86 const std::map<std::string, std::string>& value);
87 void SetStrings(const std::string& name,
88 const std::vector<std::string>& value);
89 void SetUint(const std::string& name, uint32_t value);
Peter Qiubd7e9fb2015-07-09 10:29:19 -070090 void SetUint16(const std::string& name, uint16_t value);
Peter Qiu937fafc2015-07-21 15:34:59 -070091 void SetUint8(const std::string& name, uint8_t value);
Peter Qiubd7e9fb2015-07-09 10:29:19 -070092 void SetUint8s(const std::string& name, const std::vector<uint8_t>& value);
93 void SetUint32s(const std::string& name, const std::vector<uint32_t>& value);
Darin Petkov63138a92012-02-06 14:09:15 +010094
Peter Qiu937fafc2015-07-21 15:34:59 -070095 // TODO(zqiu): remove type specific remove functions and add a generic remove
96 // function instead.
Paul Stewart8ae18742015-06-16 13:13:10 -070097 void RemoveString(const std::string& name);
98 void RemoveStringmap(const std::string& name);
99 void RemoveStrings(const std::string& name);
100 void RemoveInt(const std::string& name);
101 void RemoveKeyValueStore(const std::string& name);
Peter Qiubd7e9fb2015-07-09 10:29:19 -0700102 void RemoveInt16(const std::string& name);
103 void RemoveRpcIdentifier(const std::string& name);
104 void RemoveByteArrays(const std::string& name);
105 void RemoveUint16(const std::string& name);
Peter Qiu937fafc2015-07-21 15:34:59 -0700106 void RemoveUint8(const std::string& name);
Peter Qiubd7e9fb2015-07-09 10:29:19 -0700107 void RemoveUint8s(const std::string& name);
108 void RemoveUint32s(const std::string& name);
Paul Stewart88125fb2012-03-26 07:13:51 -0700109
Darin Petkov7f060332012-03-14 11:46:47 +0100110 // If |name| is in this store returns its value, otherwise returns
111 // |default_value|.
Paul Stewart8ae18742015-06-16 13:13:10 -0700112 bool LookupBool(const std::string& name, bool default_value) const;
113 int LookupInt(const std::string& name, int default_value) const;
114 std::string LookupString(const std::string& name,
115 const std::string& default_value) const;
Darin Petkov7f060332012-03-14 11:46:47 +0100116
Peter Qiud31fb582015-07-15 14:33:32 -0700117 const chromeos::VariantDictionary& properties() const {
118 return properties_;
Peter Qiubd7e9fb2015-07-09 10:29:19 -0700119 }
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700120
Peter Qiuca813a02015-07-16 11:55:10 -0700121 // Conversion function between KeyValueStore and chromeos::VariantDictionary.
122 // Since we already use chromeos::VariantDictionary for storing key value
123 // pairs, all conversions will be trivial except nested KeyValueStore and
124 // nested chromeos::VariantDictionary.
125 static void ConvertToVariantDictionary(const KeyValueStore& in_store,
126 chromeos::VariantDictionary* out_dict);
127 static void ConvertFromVariantDictionary(
128 const chromeos::VariantDictionary& in_dict, KeyValueStore* out_store);
129
Peter Qiu60163402015-07-28 15:44:31 -0700130 static void ConvertPathsToRpcIdentifiers(
131 const std::vector<dbus::ObjectPath>& paths,
132 std::vector<std::string>* rpc_identifiers);
133
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700134 private:
Peter Qiud31fb582015-07-15 14:33:32 -0700135 chromeos::VariantDictionary properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700136};
137
138} // namespace shill
139
Ben Chanc45688b2014-07-02 23:50:45 -0700140#endif // SHILL_KEY_VALUE_STORE_H_