Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame^] | 11 | #include <vector> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 12 | |
| 13 | #include <base/basictypes.h> |
| 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | // A templated abstract base class for objects that can be used to access |
| 18 | // properties stored in objects that are meant to be made available over RPC. |
| 19 | // The intended usage is that an object stores a maps of strings to |
| 20 | // AccessorInterfaces of the appropriate type, and then uses |
| 21 | // map[name]->Get() and map[name]->Set(value) to get and set the properties. |
| 22 | template <class T> |
| 23 | class AccessorInterface { |
| 24 | public: |
| 25 | AccessorInterface() {} |
| 26 | virtual ~AccessorInterface() {} |
| 27 | |
| 28 | // Provides read-only access. |
| 29 | virtual const T &Get() = 0; |
| 30 | // Attempts to set the wrapped value. Returns true upon success. |
| 31 | virtual bool Set(const T &value) = 0; |
| 32 | |
| 33 | private: |
| 34 | DISALLOW_COPY_AND_ASSIGN(AccessorInterface); |
| 35 | }; |
| 36 | |
| 37 | // Using a smart pointer here allows pointers to classes derived from |
| 38 | // AccessorInterface<> to be stored in maps and other STL container types. |
| 39 | typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame^] | 40 | typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 41 | typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame^] | 42 | typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 43 | typedef std::tr1::shared_ptr< |
| 44 | AccessorInterface<std::map<std::string, std::string> > > StringmapAccessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame^] | 45 | typedef std::tr1::shared_ptr< |
| 46 | AccessorInterface<std::vector<std::string> > > StringsAccessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 47 | typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor; |
| 48 | typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame^] | 49 | typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 50 | |
| 51 | } // namespace shill |
| 52 | |
| 53 | #endif // SHILL_ACCESSOR_INTERFACE_ |