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 | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 37 | // Attempts to set the wrapped value. Sets |error| on failure. |
| 38 | virtual void Set(const T &value, Error *error) = 0; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | DISALLOW_COPY_AND_ASSIGN(AccessorInterface); |
| 42 | }; |
| 43 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 44 | typedef std::vector<uint8_t> ByteArray; |
| 45 | typedef std::vector<ByteArray> ByteArrays; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 46 | // Note that while the RpcIdentifiers type has the same concrete |
| 47 | // representation as the Strings type, it may be serialized |
| 48 | // differently. Accordingly, PropertyStore tracks RpcIdentifiers |
| 49 | // separately from Strings. We create a separate typedef here, to make |
| 50 | // the PropertyStore-related code read more simply. |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 51 | typedef std::string RpcIdentifier; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 52 | typedef std::vector<std::string> RpcIdentifiers; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 53 | typedef std::vector<std::string> Strings; |
| 54 | typedef std::map<std::string, std::string> Stringmap; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 55 | typedef std::vector<Stringmap> Stringmaps; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 56 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 57 | // Using a smart pointer here allows pointers to classes derived from |
| 58 | // AccessorInterface<> to be stored in maps and other STL container types. |
| 59 | typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 60 | typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 61 | typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 62 | // See comment above RpcIdentifiers typedef, for the reason why the |
| 63 | // RpcIdentifiersAccessor exists (even though it has the same |
| 64 | // underlying type as StringsAccessor). |
| 65 | typedef std::tr1::shared_ptr< |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 66 | AccessorInterface<RpcIdentifier> > RpcIdentifierAccessor; |
| 67 | typedef std::tr1::shared_ptr< |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 68 | AccessorInterface<std::vector<std::string> > >RpcIdentifiersAccessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 69 | typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 70 | typedef std::tr1::shared_ptr<AccessorInterface<Stringmap> > StringmapAccessor; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 71 | typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps> > StringmapsAccessor; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 72 | typedef std::tr1::shared_ptr<AccessorInterface<Strings> > StringsAccessor; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 73 | typedef std::tr1::shared_ptr< |
| 74 | AccessorInterface<KeyValueStore> > KeyValueStoreAccessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 75 | typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor; |
| 76 | typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 77 | typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor; |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 78 | typedef std::tr1::shared_ptr<AccessorInterface<uint64> > Uint64Accessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 79 | |
| 80 | } // namespace shill |
| 81 | |
| 82 | #endif // SHILL_ACCESSOR_INTERFACE_ |