blob: 0419dd171a357e2db42e23d103c4b041eb50b15b [file] [log] [blame]
Darin Petkov63138a92012-02-06 14:09:15 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07002// 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
Darin Petkov63138a92012-02-06 14:09:15 +010016#include "shill/key_value_store.h"
17
Chris Masone3bd3c8c2011-06-13 08:20:26 -070018namespace shill {
19
mukesh agrawalffa3d042011-10-06 15:26:10 -070020class Error;
21
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022// A templated abstract base class for objects that can be used to access
23// properties stored in objects that are meant to be made available over RPC.
24// The intended usage is that an object stores a maps of strings to
25// AccessorInterfaces of the appropriate type, and then uses
26// map[name]->Get() and map[name]->Set(value) to get and set the properties.
27template <class T>
28class AccessorInterface {
29 public:
30 AccessorInterface() {}
31 virtual ~AccessorInterface() {}
32
mukesh agrawal292dc0f2012-01-26 18:02:46 -080033 // Reset the property to its default value. Sets |error| on failure.
34 virtual void Clear(Error *error) = 0;
Gaurav Shah1b7a6162011-11-09 11:41:01 -080035 // Provides read-only access. Sets |error| on failure.
36 virtual T Get(Error *error) = 0;
mukesh agrawalffa3d042011-10-06 15:26:10 -070037 // Attempts to set the wrapped value. Sets |error| on failure.
38 virtual void Set(const T &value, Error *error) = 0;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070039
40 private:
41 DISALLOW_COPY_AND_ASSIGN(AccessorInterface);
42};
43
Paul Stewartced6a0b2011-11-08 15:32:04 -080044typedef std::vector<uint8_t> ByteArray;
45typedef std::vector<ByteArray> ByteArrays;
Chris Masonea8a2c252011-06-27 22:16:30 -070046typedef std::vector<std::string> Strings;
47typedef std::map<std::string, std::string> Stringmap;
Chris Masone889666b2011-07-03 12:58:50 -070048typedef std::vector<Stringmap> Stringmaps;
Chris Masonea8a2c252011-06-27 22:16:30 -070049
Chris Masone3bd3c8c2011-06-13 08:20:26 -070050// Using a smart pointer here allows pointers to classes derived from
51// AccessorInterface<> to be stored in maps and other STL container types.
52typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070053typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070054typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070055typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070056typedef std::tr1::shared_ptr<AccessorInterface<Stringmap> > StringmapAccessor;
Chris Masone889666b2011-07-03 12:58:50 -070057typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps> > StringmapsAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070058typedef std::tr1::shared_ptr<AccessorInterface<Strings> > StringsAccessor;
Darin Petkov63138a92012-02-06 14:09:15 +010059typedef std::tr1::shared_ptr<
60 AccessorInterface<KeyValueStore> > KeyValueStoreAccessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor;
62typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070063typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070064
65} // namespace shill
66
67#endif // SHILL_ACCESSOR_INTERFACE_