accessor_interface.h revision 889666b49d97f61a993a4e642e5728cce1c3758a
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#include <utility>
12#include <vector>
13
14#include <base/basictypes.h>
15
16namespace 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.
23template <class T>
24class 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
38// This class stores two dissimilar named properties, one named string
39// property and one named unsigned integer property.
40class 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
64typedef std::vector<std::string> Strings;
65typedef std::map<std::string, std::string> Stringmap;
66typedef std::vector<Stringmap> Stringmaps;
67
68// Using a smart pointer here allows pointers to classes derived from
69// AccessorInterface<> to be stored in maps and other STL container types.
70typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor;
71typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor;
72typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor;
73typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor;
74typedef std::tr1::shared_ptr<AccessorInterface<Stringmap> > StringmapAccessor;
75typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps> > StringmapsAccessor;
76typedef std::tr1::shared_ptr<AccessorInterface<Strings> > StringsAccessor;
77typedef std::tr1::shared_ptr<AccessorInterface<StrIntPair> > StrIntPairAccessor;
78typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor;
79typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor;
80typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor;
81
82}  // namespace shill
83
84#endif  // SHILL_ACCESSOR_INTERFACE_
85