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