blob: efb85ef065d78dc9b211182c0afe4d2b98b1d311 [file] [log] [blame]
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001// 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 Masone889666b2011-07-03 12:58:50 -070011#include <utility>
Chris Masoneb925cc82011-06-22 15:39:57 -070012#include <vector>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013
14#include <base/basictypes.h>
15
16namespace shill {
17
mukesh agrawalffa3d042011-10-06 15:26:10 -070018class Error;
19
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020// 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.
25template <class T>
26class AccessorInterface {
27 public:
28 AccessorInterface() {}
29 virtual ~AccessorInterface() {}
30
Gaurav Shah1b7a6162011-11-09 11:41:01 -080031 // Provides read-only access. Sets |error| on failure.
32 virtual T Get(Error *error) = 0;
mukesh agrawalffa3d042011-10-06 15:26:10 -070033 // Attempts to set the wrapped value. Sets |error| on failure.
34 virtual void Set(const T &value, Error *error) = 0;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070035
36 private:
37 DISALLOW_COPY_AND_ASSIGN(AccessorInterface);
38};
39
Chris Masone889666b2011-07-03 12:58:50 -070040// This class stores two dissimilar named properties, one named string
41// property and one named unsigned integer property.
42class 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 Stewartced6a0b2011-11-08 15:32:04 -080066typedef std::vector<uint8_t> ByteArray;
67typedef std::vector<ByteArray> ByteArrays;
Chris Masonea8a2c252011-06-27 22:16:30 -070068typedef std::vector<std::string> Strings;
69typedef std::map<std::string, std::string> Stringmap;
Chris Masone889666b2011-07-03 12:58:50 -070070typedef std::vector<Stringmap> Stringmaps;
Chris Masonea8a2c252011-06-27 22:16:30 -070071
Chris Masone3bd3c8c2011-06-13 08:20:26 -070072// Using a smart pointer here allows pointers to classes derived from
73// AccessorInterface<> to be stored in maps and other STL container types.
74typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070075typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070076typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070077typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070078typedef std::tr1::shared_ptr<AccessorInterface<Stringmap> > StringmapAccessor;
Chris Masone889666b2011-07-03 12:58:50 -070079typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps> > StringmapsAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070080typedef std::tr1::shared_ptr<AccessorInterface<Strings> > StringsAccessor;
Chris Masone889666b2011-07-03 12:58:50 -070081typedef std::tr1::shared_ptr<AccessorInterface<StrIntPair> > StrIntPairAccessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070082typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor;
83typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070084typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070085
86} // namespace shill
87
88#endif // SHILL_ACCESSOR_INTERFACE_