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 | |
| 12 | #include <base/basictypes.h> |
| 13 | |
| 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 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 33 | void Clear(); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 34 | void CopyFrom(const KeyValueStore &b); |
Prathmesh Prabhu | 3903c70 | 2013-11-22 17:31:17 -0800 | [diff] [blame] | 35 | bool Equals(const KeyValueStore &other) const; |
Paul Stewart | baf8707 | 2013-10-04 17:03:37 -0700 | [diff] [blame] | 36 | bool IsEmpty(); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 37 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 38 | bool ContainsBool(const std::string &name) const; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 39 | bool ContainsInt(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 40 | bool ContainsString(const std::string &name) const; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 41 | bool ContainsStringmap(const std::string &name) const; |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 42 | bool ContainsStrings(const std::string &name) const; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 43 | bool ContainsUint(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 44 | |
| 45 | bool GetBool(const std::string &name) const; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 46 | int32_t GetInt(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 47 | const std::string &GetString(const std::string &name) const; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 48 | const std::map<std::string, std::string> &GetStringmap( |
| 49 | const std::string &name) const; |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 50 | const std::vector<std::string> &GetStrings(const std::string &name) const; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 51 | uint32_t GetUint(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 52 | |
| 53 | void SetBool(const std::string &name, bool value); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 54 | void SetInt(const std::string &name, int32_t value); |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 55 | void SetString(const std::string &name, const std::string &value); |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 56 | void SetStringmap(const std::string &name, |
| 57 | const std::map<std::string, std::string> &value); |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 58 | void SetStrings(const std::string &name, |
| 59 | const std::vector<std::string> &value); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 60 | void SetUint(const std::string &name, uint32_t value); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 61 | |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 62 | void RemoveString(const std::string &name); |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 63 | void RemoveStringmap(const std::string &name); |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 64 | void RemoveStrings(const std::string &name); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 65 | void RemoveInt(const std::string &name); |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 66 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 67 | // If |name| is in this store returns its value, otherwise returns |
| 68 | // |default_value|. |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 69 | bool LookupBool(const std::string &name, bool default_value) const; |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 70 | int LookupInt(const std::string &name, int default_value) const; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 71 | std::string LookupString(const std::string &name, |
| 72 | const std::string &default_value) const; |
| 73 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 74 | const std::map<std::string, bool> &bool_properties() const { |
| 75 | return bool_properties_; |
| 76 | } |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 77 | const std::map<std::string, int32_t> &int_properties() const { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 78 | return int_properties_; |
| 79 | } |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 80 | const std::map<std::string, std::string> &string_properties() const { |
| 81 | return string_properties_; |
| 82 | } |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 83 | const std::map< |
| 84 | std::string, |
| 85 | std::map<std::string, std::string>> &stringmap_properties() const { |
| 86 | return stringmap_properties_; |
| 87 | } |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 88 | const std::map<std::string, |
| 89 | std::vector<std::string>> &strings_properties() const { |
| 90 | return strings_properties_; |
| 91 | } |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 92 | const std::map<std::string, uint32_t> &uint_properties() const { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 93 | return uint_properties_; |
| 94 | } |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 95 | |
| 96 | private: |
| 97 | std::map<std::string, bool> bool_properties_; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 98 | std::map<std::string, int32_t> int_properties_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 99 | std::map<std::string, std::string> string_properties_; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 100 | std::map<std::string, |
| 101 | std::map<std::string, std::string>> stringmap_properties_; |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 102 | std::map<std::string, std::vector<std::string>> strings_properties_; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 103 | std::map<std::string, uint32_t> uint_properties_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace shill |
| 107 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 108 | #endif // SHILL_KEY_VALUE_STORE_H_ |