Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -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_ACCESSOR_INTERFACE_ |
| 6 | #define SHILL_ACCESSOR_INTERFACE_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <tr1/memory> |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 11 | #include <utility> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 12 | #include <vector> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | |
| 14 | #include <base/basictypes.h> |
| 15 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 16 | #include "shill/key_value_store.h" |
| 17 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 18 | namespace shill { |
| 19 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 20 | class Error; |
| 21 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | // A templated abstract base class for objects that can be used to access |
| 23 | // properties stored in objects that are meant to be made available over RPC. |
| 24 | // The intended usage is that an object stores a maps of strings to |
| 25 | // AccessorInterfaces of the appropriate type, and then uses |
| 26 | // map[name]->Get() and map[name]->Set(value) to get and set the properties. |
| 27 | template <class T> |
| 28 | class AccessorInterface { |
| 29 | public: |
| 30 | AccessorInterface() {} |
| 31 | virtual ~AccessorInterface() {} |
| 32 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 33 | // Reset the property to its default value. Sets |error| on failure. |
| 34 | virtual void Clear(Error *error) = 0; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 35 | // Provides read-only access. Sets |error| on failure. |
| 36 | virtual T Get(Error *error) = 0; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 37 | // Attempts to set the wrapped value. Sets |error| on failure. The |
| 38 | // return value indicates whether or not the wrapped value was |
| 39 | // modified. If the new value is the same as the old value, Set |
| 40 | // returns false, but with |error| unchanged. |
| 41 | virtual bool Set(const T &value, Error *error) = 0; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | |
| 43 | private: |
| 44 | DISALLOW_COPY_AND_ASSIGN(AccessorInterface); |
| 45 | }; |
| 46 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 47 | typedef std::vector<uint8_t> ByteArray; |
| 48 | typedef std::vector<ByteArray> ByteArrays; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 49 | // Note that while the RpcIdentifiers type has the same concrete |
| 50 | // representation as the Strings type, it may be serialized |
| 51 | // differently. Accordingly, PropertyStore tracks RpcIdentifiers |
| 52 | // separately from Strings. We create a separate typedef here, to make |
| 53 | // the PropertyStore-related code read more simply. |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 54 | typedef std::string RpcIdentifier; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 55 | typedef std::vector<std::string> RpcIdentifiers; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 56 | typedef std::vector<std::string> Strings; |
| 57 | typedef std::map<std::string, std::string> Stringmap; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 58 | typedef std::vector<Stringmap> Stringmaps; |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 59 | typedef std::vector<uint16> Uint16s; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 60 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 61 | // Using a smart pointer here allows pointers to classes derived from |
| 62 | // AccessorInterface<> to be stored in maps and other STL container types. |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 63 | typedef std::tr1::shared_ptr<AccessorInterface<bool>> BoolAccessor; |
| 64 | typedef std::tr1::shared_ptr<AccessorInterface<int16>> Int16Accessor; |
| 65 | typedef std::tr1::shared_ptr<AccessorInterface<int32>> Int32Accessor; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 66 | // See comment above RpcIdentifiers typedef, for the reason why the |
| 67 | // RpcIdentifiersAccessor exists (even though it has the same |
| 68 | // underlying type as StringsAccessor). |
| 69 | typedef std::tr1::shared_ptr< |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 70 | AccessorInterface<RpcIdentifier>> RpcIdentifierAccessor; |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 71 | typedef std::tr1::shared_ptr< |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 72 | AccessorInterface<std::vector<std::string>>>RpcIdentifiersAccessor; |
| 73 | typedef std::tr1::shared_ptr<AccessorInterface<std::string>> StringAccessor; |
| 74 | typedef std::tr1::shared_ptr<AccessorInterface<Stringmap>> StringmapAccessor; |
| 75 | typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps>> StringmapsAccessor; |
| 76 | typedef std::tr1::shared_ptr<AccessorInterface<Strings>> StringsAccessor; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 77 | typedef std::tr1::shared_ptr< |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 78 | AccessorInterface<KeyValueStore>> KeyValueStoreAccessor; |
| 79 | typedef std::tr1::shared_ptr<AccessorInterface<uint8>> Uint8Accessor; |
| 80 | typedef std::tr1::shared_ptr<AccessorInterface<uint16>> Uint16Accessor; |
| 81 | typedef std::tr1::shared_ptr<AccessorInterface<Uint16s>> Uint16sAccessor; |
| 82 | typedef std::tr1::shared_ptr<AccessorInterface<uint32>> Uint32Accessor; |
| 83 | typedef std::tr1::shared_ptr<AccessorInterface<uint64>> Uint64Accessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 84 | |
| 85 | } // namespace shill |
| 86 | |
| 87 | #endif // SHILL_ACCESSOR_INTERFACE_ |