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 | 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 | |
| 16 | namespace shill { |
| 17 | |
| 18 | // A templated abstract base class for objects that can be used to access |
| 19 | // properties stored in objects that are meant to be made available over RPC. |
| 20 | // The intended usage is that an object stores a maps of strings to |
| 21 | // AccessorInterfaces of the appropriate type, and then uses |
| 22 | // map[name]->Get() and map[name]->Set(value) to get and set the properties. |
| 23 | template <class T> |
| 24 | class AccessorInterface { |
| 25 | public: |
| 26 | AccessorInterface() {} |
| 27 | virtual ~AccessorInterface() {} |
| 28 | |
| 29 | // Provides read-only access. |
| 30 | virtual const T &Get() = 0; |
| 31 | // Attempts to set the wrapped value. Returns true upon success. |
| 32 | virtual bool Set(const T &value) = 0; |
| 33 | |
| 34 | private: |
| 35 | DISALLOW_COPY_AND_ASSIGN(AccessorInterface); |
| 36 | }; |
| 37 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 38 | // This class stores two dissimilar named properties, one named string |
| 39 | // property and one named unsigned integer property. |
| 40 | class StrIntPair { |
| 41 | public: |
| 42 | StrIntPair() {} |
| 43 | StrIntPair(std::pair<std::string, std::string> string_prop, |
| 44 | std::pair<std::string, uint32> uint_prop) { |
| 45 | string_property_[string_prop.first] = string_prop.second; |
| 46 | uint_property_[uint_prop.first] = uint_prop.second; |
| 47 | } |
| 48 | ~StrIntPair() {} |
| 49 | |
| 50 | const std::map<std::string, std::string> &string_property() const { |
| 51 | return string_property_; |
| 52 | } |
| 53 | const std::map<std::string, uint32> &uint_property() const { |
| 54 | return uint_property_; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | // These are stored as maps because it's way easier to coerce them to |
| 59 | // the right DBus types than if they were pairs. |
| 60 | std::map<std::string, std::string> string_property_; |
| 61 | std::map<std::string, uint32> uint_property_; |
| 62 | }; |
| 63 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 64 | typedef std::vector<std::string> Strings; |
| 65 | typedef std::map<std::string, std::string> Stringmap; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 66 | typedef std::vector<Stringmap> Stringmaps; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 67 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 68 | // Using a smart pointer here allows pointers to classes derived from |
| 69 | // AccessorInterface<> to be stored in maps and other STL container types. |
| 70 | typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 71 | typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 72 | typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 73 | typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 74 | typedef std::tr1::shared_ptr<AccessorInterface<Stringmap> > StringmapAccessor; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 75 | typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps> > StringmapsAccessor; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 76 | typedef std::tr1::shared_ptr<AccessorInterface<Strings> > StringsAccessor; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 77 | typedef std::tr1::shared_ptr<AccessorInterface<StrIntPair> > StrIntPairAccessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 78 | typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor; |
| 79 | typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 80 | typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 81 | |
| 82 | } // namespace shill |
| 83 | |
| 84 | #endif // SHILL_ACCESSOR_INTERFACE_ |