blob: bde9a188959f54ee37c0dfa1b90ee4d72ced0654 [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
5#ifndef SHILL_KEY_VALUE_STORE_
6#define SHILL_KEY_VALUE_STORE_
7
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);
Paul Stewartdef189e2012-08-02 20:12:09 -070035
mukesh agrawal7a4e4002011-09-06 11:26:05 -070036 bool ContainsBool(const std::string &name) const;
Paul Stewart1062d9d2012-04-27 10:42:27 -070037 bool ContainsInt(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070038 bool ContainsString(const std::string &name) const;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070039 bool ContainsStrings(const std::string &name) const;
Darin Petkov63138a92012-02-06 14:09:15 +010040 bool ContainsUint(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070041
42 bool GetBool(const std::string &name) const;
Paul Stewart1062d9d2012-04-27 10:42:27 -070043 int32 GetInt(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070044 const std::string &GetString(const std::string &name) const;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070045 const std::vector<std::string> &GetStrings(const std::string &name) const;
Darin Petkov63138a92012-02-06 14:09:15 +010046 uint32 GetUint(const std::string &name) const;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070047
48 void SetBool(const std::string &name, bool value);
Paul Stewart1062d9d2012-04-27 10:42:27 -070049 void SetInt(const std::string &name, int32 value);
Paul Stewart3c5e2b32013-06-13 07:49:49 -070050 void SetString(const std::string &name, const std::string &value);
51 void SetStrings(const std::string &name,
52 const std::vector<std::string> &value);
Darin Petkov63138a92012-02-06 14:09:15 +010053 void SetUint(const std::string &name, uint32 value);
54
Paul Stewart88125fb2012-03-26 07:13:51 -070055 void RemoveString(const std::string &name);
Paul Stewart3c5e2b32013-06-13 07:49:49 -070056 void RemoveStrings(const std::string &name);
Paul Stewart1062d9d2012-04-27 10:42:27 -070057 void RemoveInt(const std::string &name);
Paul Stewart88125fb2012-03-26 07:13:51 -070058
Darin Petkov7f060332012-03-14 11:46:47 +010059 // If |name| is in this store returns its value, otherwise returns
60 // |default_value|.
Darin Petkovb536a742012-04-26 11:31:28 +020061 bool LookupBool(const std::string &name, bool default_value) const;
Darin Petkov7f060332012-03-14 11:46:47 +010062 std::string LookupString(const std::string &name,
63 const std::string &default_value) const;
64
Darin Petkov63138a92012-02-06 14:09:15 +010065 const std::map<std::string, bool> &bool_properties() const {
66 return bool_properties_;
67 }
Paul Stewart1062d9d2012-04-27 10:42:27 -070068 const std::map<std::string, int32> &int_properties() const {
69 return int_properties_;
70 }
Darin Petkov63138a92012-02-06 14:09:15 +010071 const std::map<std::string, std::string> &string_properties() const {
72 return string_properties_;
73 }
Paul Stewart3c5e2b32013-06-13 07:49:49 -070074 const std::map<std::string,
75 std::vector<std::string>> &strings_properties() const {
76 return strings_properties_;
77 }
Darin Petkov63138a92012-02-06 14:09:15 +010078 const std::map<std::string, uint32> &uint_properties() const {
79 return uint_properties_;
80 }
mukesh agrawal7a4e4002011-09-06 11:26:05 -070081
82 private:
83 std::map<std::string, bool> bool_properties_;
Paul Stewart1062d9d2012-04-27 10:42:27 -070084 std::map<std::string, int32> int_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070085 std::map<std::string, std::string> string_properties_;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070086 std::map<std::string, std::vector<std::string>> strings_properties_;
Darin Petkov63138a92012-02-06 14:09:15 +010087 std::map<std::string, uint32> uint_properties_;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070088};
89
90} // namespace shill
91
92#endif // SHILL_KEY_VALUE_STORE_