blob: 916a976d5ef8925bbe73525fb66cf3274dd419f5 [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 Masoneb925cc82011-06-22 15:39:57 -070011#include <vector>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070012
13#include <base/basictypes.h>
14
15namespace shill {
16
17// A templated abstract base class for objects that can be used to access
18// properties stored in objects that are meant to be made available over RPC.
19// The intended usage is that an object stores a maps of strings to
20// AccessorInterfaces of the appropriate type, and then uses
21// map[name]->Get() and map[name]->Set(value) to get and set the properties.
22template <class T>
23class AccessorInterface {
24 public:
25 AccessorInterface() {}
26 virtual ~AccessorInterface() {}
27
28 // Provides read-only access.
29 virtual const T &Get() = 0;
30 // Attempts to set the wrapped value. Returns true upon success.
31 virtual bool Set(const T &value) = 0;
32
33 private:
34 DISALLOW_COPY_AND_ASSIGN(AccessorInterface);
35};
36
37// Using a smart pointer here allows pointers to classes derived from
38// AccessorInterface<> to be stored in maps and other STL container types.
39typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070040typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070041typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070042typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070043typedef std::tr1::shared_ptr<
44 AccessorInterface<std::map<std::string, std::string> > > StringmapAccessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070045typedef std::tr1::shared_ptr<
46 AccessorInterface<std::vector<std::string> > > StringsAccessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070047typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor;
48typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070049typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070050
51} // namespace shill
52
53#endif // SHILL_ACCESSOR_INTERFACE_