Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 5 | #ifndef SHILL_KEY_VALUE_STORE_H_ |
| 6 | #define SHILL_KEY_VALUE_STORE_H_ |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 10 | #include <vector> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 11 | |
Peter Qiu | d31fb58 | 2015-07-15 14:33:32 -0700 | [diff] [blame] | 12 | #include <chromeos/variant_dictionary.h> |
| 13 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 14 | namespace shill { |
| 15 | |
| 16 | class 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 Qiu | d31fb58 | 2015-07-15 14:33:32 -0700 | [diff] [blame] | 33 | // 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 Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 38 | void Clear(); |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 39 | void CopyFrom(const KeyValueStore& b); |
Paul Stewart | baf8707 | 2013-10-04 17:03:37 -0700 | [diff] [blame] | 40 | bool IsEmpty(); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 41 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 42 | bool ContainsBool(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 43 | bool ContainsByteArrays(const std::string& name) const; |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 44 | bool ContainsInt(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 45 | bool ContainsInt16(const std::string& name) const; |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 46 | bool ContainsKeyValueStore(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 47 | bool ContainsRpcIdentifier(const std::string& name) const; |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 48 | 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 Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 52 | bool ContainsUint16(const std::string& name) const; |
Peter Qiu | 937fafc | 2015-07-21 15:34:59 -0700 | [diff] [blame] | 53 | bool ContainsUint8(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 54 | bool ContainsUint8s(const std::string& name) const; |
| 55 | bool ContainsUint32s(const std::string& name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 56 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 57 | bool GetBool(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 58 | const std::vector<std::vector<uint8_t>>& GetByteArrays( |
| 59 | const std::string& name) const; |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 60 | int32_t GetInt(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 61 | int16_t GetInt16(const std::string& name) const; |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 62 | const KeyValueStore& GetKeyValueStore(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 63 | const std::string& GetRpcIdentifier(const std::string& name) const; |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 64 | 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 Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 69 | uint16_t GetUint16(const std::string& name) const; |
Peter Qiu | 937fafc | 2015-07-21 15:34:59 -0700 | [diff] [blame] | 70 | uint8_t GetUint8(const std::string& name) const; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 71 | const std::vector<uint8_t>& GetUint8s(const std::string& name) const; |
| 72 | const std::vector<uint32_t>& GetUint32s(const std::string& name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 73 | |
Peter Qiu | 937fafc | 2015-07-21 15:34:59 -0700 | [diff] [blame] | 74 | // 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 Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 77 | void SetBool(const std::string& name, bool value); |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 78 | void SetByteArrays(const std::string& name, |
| 79 | const std::vector<std::vector<uint8_t>>& value); |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 80 | void SetInt(const std::string& name, int32_t value); |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 81 | void SetInt16(const std::string& name, int16_t value); |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 82 | void SetKeyValueStore(const std::string& name, const KeyValueStore& value); |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 83 | void SetRpcIdentifier(const std::string& name, const std::string& value); |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 84 | 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 Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 90 | void SetUint16(const std::string& name, uint16_t value); |
Peter Qiu | 937fafc | 2015-07-21 15:34:59 -0700 | [diff] [blame] | 91 | void SetUint8(const std::string& name, uint8_t value); |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 92 | 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 Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 94 | |
Peter Qiu | 937fafc | 2015-07-21 15:34:59 -0700 | [diff] [blame] | 95 | // TODO(zqiu): remove type specific remove functions and add a generic remove |
| 96 | // function instead. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 97 | 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 Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 102 | 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 Qiu | 937fafc | 2015-07-21 15:34:59 -0700 | [diff] [blame] | 106 | void RemoveUint8(const std::string& name); |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 107 | void RemoveUint8s(const std::string& name); |
| 108 | void RemoveUint32s(const std::string& name); |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 109 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 110 | // If |name| is in this store returns its value, otherwise returns |
| 111 | // |default_value|. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 112 | 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 Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 116 | |
Peter Qiu | d31fb58 | 2015-07-15 14:33:32 -0700 | [diff] [blame] | 117 | const chromeos::VariantDictionary& properties() const { |
| 118 | return properties_; |
Peter Qiu | bd7e9fb | 2015-07-09 10:29:19 -0700 | [diff] [blame] | 119 | } |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 120 | |
Peter Qiu | ca813a0 | 2015-07-16 11:55:10 -0700 | [diff] [blame] | 121 | // 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 Qiu | 6016340 | 2015-07-28 15:44:31 -0700 | [diff] [blame] | 130 | static void ConvertPathsToRpcIdentifiers( |
| 131 | const std::vector<dbus::ObjectPath>& paths, |
| 132 | std::vector<std::string>* rpc_identifiers); |
| 133 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 134 | private: |
Peter Qiu | d31fb58 | 2015-07-15 14:33:32 -0700 | [diff] [blame] | 135 | chromeos::VariantDictionary properties_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | } // namespace shill |
| 139 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 140 | #endif // SHILL_KEY_VALUE_STORE_H_ |