blob: 56ade72e77afa41487409fe052ca9fbbd7ea56df [file] [log] [blame]
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoneb925cc82011-06-22 15:39:57 -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_PROPERTY_STORE_
6#define SHILL_PROPERTY_STORE_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include <base/basictypes.h>
13
14#include "shill/accessor_interface.h"
Chris Masonea8a2c252011-06-27 22:16:30 -070015#include "shill/property_iterator.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070016
17namespace shill {
18
19class Error;
20
21class PropertyStore {
22 public:
Chris Masone27c4aa52011-07-02 13:10:14 -070023 PropertyStore();
Chris Masoneb925cc82011-06-22 15:39:57 -070024 virtual ~PropertyStore();
25
mukesh agrawalde29fa82011-09-16 16:16:36 -070026 virtual bool Contains(const std::string& property) const;
Chris Masoneb925cc82011-06-22 15:39:57 -070027
Paul Stewarte6e8e492013-01-17 11:00:50 -080028 // Methods to allow the getting of properties stored in the referenced
29 // |store_| by name. Upon success, these methods return true and return the
30 // property value in |value|. Upon failure, they return false and
31 // leave |value| untouched.
32 bool GetBoolProperty(const std::string &name, bool *value,
33 Error *error) const;
34 bool GetInt16Property(const std::string &name, int16 *value,
35 Error *error) const;
36 bool GetInt32Property(const std::string &name, int32 *value,
37 Error *error) const;
38 bool GetKeyValueStoreProperty(const std::string &name, KeyValueStore *value,
39 Error *error) const;
40 bool GetStringProperty(const std::string &name, std::string *value,
41 Error *error) const;
42 bool GetStringmapProperty(const std::string &name, Stringmap *values,
43 Error *error) const;
44 bool GetStringmapsProperty(const std::string &name, Stringmaps *values,
45 Error *error) const;
46 bool GetStringsProperty(const std::string &name, Strings *values,
47 Error *error) const;
48 bool GetUint8Property(const std::string &name, uint8 *value,
49 Error *error) const;
50 bool GetUint16Property(const std::string &name, uint16 *value,
51 Error *error) const;
52 bool GetUint32Property(const std::string &name, uint32 *value,
53 Error *error) const;
54 bool GetUint64Property(const std::string &name, uint64 *value,
55 Error *error) const;
56 bool GetRpcIdentifierProperty(const std::string &name, RpcIdentifier *value,
57 Error *error) const;
58
Chris Masoneb925cc82011-06-22 15:39:57 -070059 // Methods to allow the setting, by name, of properties stored in this object.
60 // The property names are declared in chromeos/dbus/service_constants.h,
61 // so that they may be shared with libcros.
62 // Upon success, these methods return true and leave |error| untouched.
63 // Upon failure, they return false and set |error| appropriately, if it
64 // is non-NULL.
mukesh agrawal66b0aca2012-01-30 15:28:28 -080065 virtual bool SetBoolProperty(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070066 bool value,
67 Error *error);
68
mukesh agrawal66b0aca2012-01-30 15:28:28 -080069 virtual bool SetInt16Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070070 int16 value,
71 Error *error);
72
mukesh agrawal66b0aca2012-01-30 15:28:28 -080073 virtual bool SetInt32Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070074 int32 value,
75 Error *error);
76
mukesh agrawal66b0aca2012-01-30 15:28:28 -080077 virtual bool SetStringProperty(const std::string &name,
78 const std::string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -070079 Error *error);
80
81 virtual bool SetStringmapProperty(
mukesh agrawal66b0aca2012-01-30 15:28:28 -080082 const std::string &name,
83 const std::map<std::string, std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -070084 Error *error);
85
mukesh agrawal66b0aca2012-01-30 15:28:28 -080086 virtual bool SetStringsProperty(const std::string &name,
87 const std::vector<std::string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -070088 Error *error);
89
mukesh agrawal66b0aca2012-01-30 15:28:28 -080090 virtual bool SetUint8Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070091 uint8 value,
92 Error *error);
93
mukesh agrawal66b0aca2012-01-30 15:28:28 -080094 virtual bool SetUint16Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070095 uint16 value,
96 Error *error);
97
mukesh agrawal66b0aca2012-01-30 15:28:28 -080098 virtual bool SetUint32Property(const std::string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -070099 uint32 value,
100 Error *error);
101
Paul Stewarte18c33b2012-07-10 20:48:44 -0700102 virtual bool SetUint64Property(const std::string &name,
103 uint64 value,
104 Error *error);
105
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400106 virtual bool SetRpcIdentifierProperty(const std::string &name,
107 const RpcIdentifier &value,
108 Error *error);
109
mukesh agrawal4d260da2012-01-30 11:53:52 -0800110 // Clearing a property resets it to its "factory" value. This value
111 // is generally the value that it (the property) had when it was
112 // registered with PropertyStore.
113 //
114 // The exception to this rule is write-only derived properties. For
115 // such properties, the property owner explicitly provides a
116 // "factory" value at registration time. This is necessary because
117 // PropertyStore can't read the current value at registration time.
118 //
119 // |name| is the key used to access the property. If the property
120 // cannot be cleared, |error| is set, and the method returns false.
121 // Otherwrise, |error| is unchanged, and the method returns true.
122 virtual bool ClearProperty(const std::string &name, Error *error);
123
mukesh agrawalffa3d042011-10-06 15:26:10 -0700124 // Accessors for iterators over property maps. Useful for dumping all
125 // properties.
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800126 ReadablePropertyConstIterator<bool> GetBoolPropertiesIter() const;
127 ReadablePropertyConstIterator<int16> GetInt16PropertiesIter() const;
128 ReadablePropertyConstIterator<int32> GetInt32PropertiesIter() const;
Darin Petkov63138a92012-02-06 14:09:15 +0100129 ReadablePropertyConstIterator<KeyValueStore> GetKeyValueStorePropertiesIter(
130 ) const;
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400131 ReadablePropertyConstIterator<RpcIdentifier> GetRpcIdentifierPropertiesIter(
132 ) const;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700133 ReadablePropertyConstIterator<RpcIdentifiers> GetRpcIdentifiersPropertiesIter(
134 ) const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800135 ReadablePropertyConstIterator<std::string> GetStringPropertiesIter() const;
136 ReadablePropertyConstIterator<Stringmap> GetStringmapPropertiesIter() const;
137 ReadablePropertyConstIterator<Stringmaps> GetStringmapsPropertiesIter() const;
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800138 ReadablePropertyConstIterator<Strings> GetStringsPropertiesIter() const;
139 ReadablePropertyConstIterator<uint8> GetUint8PropertiesIter() const;
140 ReadablePropertyConstIterator<uint16> GetUint16PropertiesIter() const;
141 ReadablePropertyConstIterator<uint32> GetUint32PropertiesIter() const;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700142 ReadablePropertyConstIterator<uint64> GetUint64PropertiesIter() const;
Chris Masonea8a2c252011-06-27 22:16:30 -0700143
mukesh agrawal4d260da2012-01-30 11:53:52 -0800144 // Methods for registering a property.
145 //
146 // It is permitted to re-register a property (in which case the old
147 // binding is forgotten). However, the newly bound object must be of
148 // the same type.
149 //
150 // Note that types do not encode read-write permission. Hence, it
151 // is possible to change permissions by rebinding a property to the
152 // same object.
153 //
154 // (Corollary of the rebinding-to-same-type restriction: a
155 // PropertyStore cannot hold two properties of the same name, but
156 // differing types.)
Chris Masoneb925cc82011-06-22 15:39:57 -0700157 void RegisterBool(const std::string &name, bool *prop);
158 void RegisterConstBool(const std::string &name, const bool *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800159 void RegisterWriteOnlyBool(const std::string &name, bool *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700160 void RegisterInt16(const std::string &name, int16 *prop);
161 void RegisterConstInt16(const std::string &name, const int16 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800162 void RegisterWriteOnlyInt16(const std::string &name, int16 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700163 void RegisterInt32(const std::string &name, int32 *prop);
164 void RegisterConstInt32(const std::string &name, const int32 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800165 void RegisterWriteOnlyInt32(const std::string &name, int32 *prop);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800166 void RegisterUint32(const std::string &name, uint32 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700167 void RegisterString(const std::string &name, std::string *prop);
168 void RegisterConstString(const std::string &name, const std::string *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800169 void RegisterWriteOnlyString(const std::string &name, std::string *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700170 void RegisterStringmap(const std::string &name, Stringmap *prop);
171 void RegisterConstStringmap(const std::string &name, const Stringmap *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800172 void RegisterWriteOnlyStringmap(const std::string &name, Stringmap *prop);
Darin Petkovc0865312011-09-16 15:31:20 -0700173 void RegisterStringmaps(const std::string &name, Stringmaps *prop);
174 void RegisterConstStringmaps(const std::string &name, const Stringmaps *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800175 void RegisterWriteOnlyStringmaps(const std::string &name, Stringmaps *prop);
Chris Masone43b48a12011-07-01 13:37:07 -0700176 void RegisterStrings(const std::string &name, Strings *prop);
Chris Masone889666b2011-07-03 12:58:50 -0700177 void RegisterConstStrings(const std::string &name, const Strings *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800178 void RegisterWriteOnlyStrings(const std::string &name, Strings *prop);
Chris Masone889666b2011-07-03 12:58:50 -0700179 void RegisterUint8(const std::string &name, uint8 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700180 void RegisterConstUint8(const std::string &name, const uint8 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800181 void RegisterWriteOnlyUint8(const std::string &name, uint8 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700182 void RegisterUint16(const std::string &name, uint16 *prop);
183 void RegisterConstUint16(const std::string &name, const uint16 *prop);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800184 void RegisterWriteOnlyUint16(const std::string &name, uint16 *prop);
Chris Masoneb925cc82011-06-22 15:39:57 -0700185
Chris Masone27c4aa52011-07-02 13:10:14 -0700186 void RegisterDerivedBool(const std::string &name,
187 const BoolAccessor &accessor);
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800188 void RegisterDerivedInt32(const std::string &name,
189 const Int32Accessor &accessor);
Darin Petkov63138a92012-02-06 14:09:15 +0100190 void RegisterDerivedKeyValueStore(const std::string &name,
191 const KeyValueStoreAccessor &accessor);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400192 void RegisterDerivedRpcIdentifier(const std::string &name,
193 const RpcIdentifierAccessor &acc);
mukesh agrawal2366eed2012-03-20 18:21:50 -0700194 void RegisterDerivedRpcIdentifiers(const std::string &name,
195 const RpcIdentifiersAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700196 void RegisterDerivedString(const std::string &name,
197 const StringAccessor &accessor);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400198 void RegisterDerivedStringmap(const std::string &name,
199 const StringmapAccessor &accessor);
Chris Masone889666b2011-07-03 12:58:50 -0700200 void RegisterDerivedStringmaps(const std::string &name,
201 const StringmapsAccessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700202 void RegisterDerivedStrings(const std::string &name,
203 const StringsAccessor &accessor);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800204 void RegisterDerivedUint16(const std::string &name,
205 const Uint16Accessor &accessor);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700206 void RegisterDerivedUint64(const std::string &name,
207 const Uint64Accessor &accessor);
Chris Masone27c4aa52011-07-02 13:10:14 -0700208
209 private:
mukesh agrawalffa3d042011-10-06 15:26:10 -0700210 template <class V>
Paul Stewarte6e8e492013-01-17 11:00:50 -0800211 bool GetProperty(
212 const std::string &name,
213 V *value,
214 Error *error,
215 const std::map< std::string, std::tr1::shared_ptr<
216 AccessorInterface<V> > > &collection,
217 const std::string &value_type_english) const;
218
219 template <class V>
mukesh agrawalffa3d042011-10-06 15:26:10 -0700220 bool SetProperty(
221 const std::string &name,
222 const V &value,
223 Error *error,
224 std::map< std::string, std::tr1::shared_ptr< AccessorInterface<V> > > &,
225 const std::string &value_type_english);
226
Chris Masoneb925cc82011-06-22 15:39:57 -0700227 // These are std::maps instead of something cooler because the common
228 // operation is iterating through them and returning all properties.
229 std::map<std::string, BoolAccessor> bool_properties_;
230 std::map<std::string, Int16Accessor> int16_properties_;
231 std::map<std::string, Int32Accessor> int32_properties_;
Darin Petkov63138a92012-02-06 14:09:15 +0100232 std::map<std::string, KeyValueStoreAccessor> key_value_store_properties_;
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400233 std::map<std::string, RpcIdentifierAccessor> rpc_identifier_properties_;
mukesh agrawal2366eed2012-03-20 18:21:50 -0700234 std::map<std::string, RpcIdentifiersAccessor> rpc_identifiers_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700235 std::map<std::string, StringAccessor> string_properties_;
236 std::map<std::string, StringmapAccessor> stringmap_properties_;
Chris Masone889666b2011-07-03 12:58:50 -0700237 std::map<std::string, StringmapsAccessor> stringmaps_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700238 std::map<std::string, StringsAccessor> strings_properties_;
239 std::map<std::string, Uint8Accessor> uint8_properties_;
240 std::map<std::string, Uint16Accessor> uint16_properties_;
241 std::map<std::string, Uint32Accessor> uint32_properties_;
Paul Stewarte18c33b2012-07-10 20:48:44 -0700242 std::map<std::string, Uint64Accessor> uint64_properties_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700243
Chris Masoneb925cc82011-06-22 15:39:57 -0700244 DISALLOW_COPY_AND_ASSIGN(PropertyStore);
245};
246
247} // namespace shill
248
249#endif // SHILL_PROPERTY_STORE_