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 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 12 | namespace shill { |
| 13 | |
| 14 | class 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 Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 31 | void Clear(); |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 32 | void CopyFrom(const KeyValueStore& b); |
| 33 | bool Equals(const KeyValueStore& other) const; |
Paul Stewart | baf8707 | 2013-10-04 17:03:37 -0700 | [diff] [blame] | 34 | bool IsEmpty(); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 35 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 36 | 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 agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 43 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 44 | 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 agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 52 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 53 | 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 Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 62 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 63 | 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 Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 68 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 69 | // If |name| is in this store returns its value, otherwise returns |
| 70 | // |default_value|. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 71 | 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 Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 75 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 76 | const std::map<std::string, bool>& bool_properties() const { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 77 | return bool_properties_; |
| 78 | } |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 79 | const std::map<std::string, int32_t>& int_properties() const { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 80 | return int_properties_; |
| 81 | } |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 82 | const std::map<std::string, KeyValueStore> |
| 83 | &key_value_store_properties() const { |
| 84 | return key_value_store_properties_; |
| 85 | } |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 86 | const std::map<std::string, std::string>& string_properties() const { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 87 | return string_properties_; |
| 88 | } |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 89 | const std::map< |
| 90 | std::string, |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 91 | std::map<std::string, std::string>>& stringmap_properties() const { |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 92 | return stringmap_properties_; |
| 93 | } |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 94 | const std::map<std::string, |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 95 | std::vector<std::string>>& strings_properties() const { |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 96 | return strings_properties_; |
| 97 | } |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 98 | const std::map<std::string, uint32_t>& uint_properties() const { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 99 | return uint_properties_; |
| 100 | } |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 101 | |
| 102 | private: |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 103 | // Recursively compare KeyValueStore properties with |other|. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 104 | bool KeyValueStorePropertiesAreEqual(const KeyValueStore& other) const; |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 105 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 106 | std::map<std::string, bool> bool_properties_; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 107 | std::map<std::string, int32_t> int_properties_; |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 108 | std::map<std::string, KeyValueStore> key_value_store_properties_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 109 | std::map<std::string, std::string> string_properties_; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 110 | std::map<std::string, |
| 111 | std::map<std::string, std::string>> stringmap_properties_; |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 112 | std::map<std::string, std::vector<std::string>> strings_properties_; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 113 | std::map<std::string, uint32_t> uint_properties_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace shill |
| 117 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 118 | #endif // SHILL_KEY_VALUE_STORE_H_ |