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 | |
| 5 | #ifndef SHILL_KEY_VALUE_STORE_ |
| 6 | #define SHILL_KEY_VALUE_STORE_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <base/basictypes.h> |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | class KeyValueStore { |
| 16 | // A simple store for key-value pairs, which supports (a limited set of) |
| 17 | // heterogenous value types. |
| 18 | // |
| 19 | // Compare to PropertyStore, which enables a class to (selectively) |
| 20 | // expose its instance members as properties accessible via |
| 21 | // RPC. (RPC support for ProperyStore is implemented in a |
| 22 | // protocol-specific adaptor. e.g. dbus_adpator.) |
| 23 | // |
| 24 | // Implemented separately from PropertyStore, to avoid complicating |
| 25 | // the PropertyStore interface. In particular, objects implementing the |
| 26 | // PropertyStore interface always provide the storage themselves. In |
| 27 | // contrast, users of KeyValueStore expect KeyValueStore to provide |
| 28 | // storage. |
| 29 | public: |
| 30 | KeyValueStore(); |
| 31 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 32 | void Clear(); |
| 33 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 34 | bool ContainsBool(const std::string &name) const; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 35 | bool ContainsInt(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 36 | bool ContainsString(const std::string &name) const; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 37 | bool ContainsUint(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 38 | |
| 39 | bool GetBool(const std::string &name) const; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 40 | int32 GetInt(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 41 | const std::string &GetString(const std::string &name) const; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 42 | uint32 GetUint(const std::string &name) const; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 43 | |
| 44 | void SetBool(const std::string &name, bool value); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 45 | void SetInt(const std::string &name, int32 value); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 46 | void SetString(const std::string& name, const std::string& value); |
| 47 | void SetUint(const std::string &name, uint32 value); |
| 48 | |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 49 | void RemoveString(const std::string &name); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 50 | void RemoveInt(const std::string &name); |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 51 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 52 | // If |name| is in this store returns its value, otherwise returns |
| 53 | // |default_value|. |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 54 | bool LookupBool(const std::string &name, bool default_value) const; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 55 | std::string LookupString(const std::string &name, |
| 56 | const std::string &default_value) const; |
| 57 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 58 | const std::map<std::string, bool> &bool_properties() const { |
| 59 | return bool_properties_; |
| 60 | } |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 61 | const std::map<std::string, int32> &int_properties() const { |
| 62 | return int_properties_; |
| 63 | } |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 64 | const std::map<std::string, std::string> &string_properties() const { |
| 65 | return string_properties_; |
| 66 | } |
| 67 | const std::map<std::string, uint32> &uint_properties() const { |
| 68 | return uint_properties_; |
| 69 | } |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | std::map<std::string, bool> bool_properties_; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 73 | std::map<std::string, int32> int_properties_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 74 | std::map<std::string, std::string> string_properties_; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 75 | std::map<std::string, uint32> uint_properties_; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace shill |
| 79 | |
| 80 | #endif // SHILL_KEY_VALUE_STORE_ |