blob: 3a626966b713732c90731d8954ba4bbbfed1159f [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
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
Chris Masone889666b2011-07-03 12:58:50 -070038// 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
Chris Masonea8a2c252011-06-27 22:16:30 -070064typedef std::vector<std::string> Strings;
65typedef std::map<std::string, std::string> Stringmap;
Chris Masone889666b2011-07-03 12:58:50 -070066typedef std::vector<Stringmap> Stringmaps;
Chris Masonea8a2c252011-06-27 22:16:30 -070067
Chris Masone3bd3c8c2011-06-13 08:20:26 -070068// 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;
Chris Masoneb925cc82011-06-22 15:39:57 -070071typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070072typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070073typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070074typedef std::tr1::shared_ptr<AccessorInterface<Stringmap> > StringmapAccessor;
Chris Masone889666b2011-07-03 12:58:50 -070075typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps> > StringmapsAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070076typedef std::tr1::shared_ptr<AccessorInterface<Strings> > StringsAccessor;
Chris Masone889666b2011-07-03 12:58:50 -070077typedef std::tr1::shared_ptr<AccessorInterface<StrIntPair> > StrIntPairAccessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070078typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor;
79typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070080typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070081
82} // namespace shill
83
84#endif // SHILL_ACCESSOR_INTERFACE_