blob: 4f8775ab25bae0c8160e88cadd035a0d76886e26 [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
12#include <base/basictypes.h>
13
14namespace 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
Paul Stewartdef189e2012-08-02 20:12:09 -070033 void Clear();
Paul Stewartd2e1c362013-03-03 19:06:07 -080034 void CopyFrom(const KeyValueStore &b);
Prathmesh Prabhu3903c702013-11-22 17:31:17 -080035 bool Equals(const KeyValueStore &other) const;
Paul Stewartbaf87072013-10-04 17:03:37 -070036 bool IsEmpty();
Paul Stewartdef189e2012-08-02 20:12:09 -070037
mukesh agrawal7a4e4002011-09-06 11:26:05 -070038 bool ContainsBool(const std::string &name) const;
Paul Stewart1062d9d2012-04-27 10:42:27 -070039 bool ContainsInt(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070040 bool ContainsString(const std::string &name) const;
Arman Uguray631c7e42013-07-30 16:41:12 -070041 bool ContainsStringmap(const std::string &name) const;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070042 bool ContainsStrings(const std::string &name) const;
Darin Petkov63138a92012-02-06 14:09:15 +010043 bool ContainsUint(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070044
45 bool GetBool(const std::string &name) const;
Ben Chan7fab8972014-08-10 17:14:46 -070046 int32_t GetInt(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070047 const std::string &GetString(const std::string &name) const;
Arman Uguray631c7e42013-07-30 16:41:12 -070048 const std::map<std::string, std::string> &GetStringmap(
49 const std::string &name) const;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070050 const std::vector<std::string> &GetStrings(const std::string &name) const;
Ben Chan7fab8972014-08-10 17:14:46 -070051 uint32_t GetUint(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070052
53 void SetBool(const std::string &name, bool value);
Ben Chan7fab8972014-08-10 17:14:46 -070054 void SetInt(const std::string &name, int32_t value);
Paul Stewart3c5e2b32013-06-13 07:49:49 -070055 void SetString(const std::string &name, const std::string &value);
Arman Uguray631c7e42013-07-30 16:41:12 -070056 void SetStringmap(const std::string &name,
57 const std::map<std::string, std::string> &value);
Paul Stewart3c5e2b32013-06-13 07:49:49 -070058 void SetStrings(const std::string &name,
59 const std::vector<std::string> &value);
Ben Chan7fab8972014-08-10 17:14:46 -070060 void SetUint(const std::string &name, uint32_t value);
Darin Petkov63138a92012-02-06 14:09:15 +010061
Paul Stewart88125fb2012-03-26 07:13:51 -070062 void RemoveString(const std::string &name);
Arman Uguray631c7e42013-07-30 16:41:12 -070063 void RemoveStringmap(const std::string &name);
Paul Stewart3c5e2b32013-06-13 07:49:49 -070064 void RemoveStrings(const std::string &name);
Paul Stewart1062d9d2012-04-27 10:42:27 -070065 void RemoveInt(const std::string &name);
Paul Stewart88125fb2012-03-26 07:13:51 -070066
Darin Petkov7f060332012-03-14 11:46:47 +010067 // If |name| is in this store returns its value, otherwise returns
68 // |default_value|.
Darin Petkovb536a742012-04-26 11:31:28 +020069 bool LookupBool(const std::string &name, bool default_value) const;
Paul Stewart68e59562013-12-12 08:15:05 -080070 int LookupInt(const std::string &name, int default_value) const;
Darin Petkov7f060332012-03-14 11:46:47 +010071 std::string LookupString(const std::string &name,
72 const std::string &default_value) const;
73
Darin Petkov63138a92012-02-06 14:09:15 +010074 const std::map<std::string, bool> &bool_properties() const {
75 return bool_properties_;
76 }
Ben Chan7fab8972014-08-10 17:14:46 -070077 const std::map<std::string, int32_t> &int_properties() const {
Paul Stewart1062d9d2012-04-27 10:42:27 -070078 return int_properties_;
79 }
Darin Petkov63138a92012-02-06 14:09:15 +010080 const std::map<std::string, std::string> &string_properties() const {
81 return string_properties_;
82 }
Arman Uguray631c7e42013-07-30 16:41:12 -070083 const std::map<
84 std::string,
85 std::map<std::string, std::string>> &stringmap_properties() const {
86 return stringmap_properties_;
87 }
Paul Stewart3c5e2b32013-06-13 07:49:49 -070088 const std::map<std::string,
89 std::vector<std::string>> &strings_properties() const {
90 return strings_properties_;
91 }
Ben Chan7fab8972014-08-10 17:14:46 -070092 const std::map<std::string, uint32_t> &uint_properties() const {
Darin Petkov63138a92012-02-06 14:09:15 +010093 return uint_properties_;
94 }
mukesh agrawal7a4e4002011-09-06 11:26:05 -070095
96 private:
97 std::map<std::string, bool> bool_properties_;
Ben Chan7fab8972014-08-10 17:14:46 -070098 std::map<std::string, int32_t> int_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070099 std::map<std::string, std::string> string_properties_;
Arman Uguray631c7e42013-07-30 16:41:12 -0700100 std::map<std::string,
101 std::map<std::string, std::string>> stringmap_properties_;
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700102 std::map<std::string, std::vector<std::string>> strings_properties_;
Ben Chan7fab8972014-08-10 17:14:46 -0700103 std::map<std::string, uint32_t> uint_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700104};
105
106} // namespace shill
107
Ben Chanc45688b2014-07-02 23:50:45 -0700108#endif // SHILL_KEY_VALUE_STORE_H_