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