blob: 50dab1e884b0fe685dd6b1eecfc8a86f3ea484fb [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;
mukesh agrawal2366eed2012-03-20 18:21:50 -070046// Note that while the RpcIdentifiers type has the same concrete
47// representation as the Strings type, it may be serialized
48// differently. Accordingly, PropertyStore tracks RpcIdentifiers
49// separately from Strings. We create a separate typedef here, to make
50// the PropertyStore-related code read more simply.
Jason Glasgowacdc11f2012-03-30 14:12:22 -040051typedef std::string RpcIdentifier;
mukesh agrawal2366eed2012-03-20 18:21:50 -070052typedef std::vector<std::string> RpcIdentifiers;
Chris Masonea8a2c252011-06-27 22:16:30 -070053typedef std::vector<std::string> Strings;
54typedef std::map<std::string, std::string> Stringmap;
Chris Masone889666b2011-07-03 12:58:50 -070055typedef std::vector<Stringmap> Stringmaps;
Chris Masonea8a2c252011-06-27 22:16:30 -070056
Chris Masone3bd3c8c2011-06-13 08:20:26 -070057// Using a smart pointer here allows pointers to classes derived from
58// AccessorInterface<> to be stored in maps and other STL container types.
59typedef std::tr1::shared_ptr<AccessorInterface<bool> > BoolAccessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070060typedef std::tr1::shared_ptr<AccessorInterface<int16> > Int16Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061typedef std::tr1::shared_ptr<AccessorInterface<int32> > Int32Accessor;
mukesh agrawal2366eed2012-03-20 18:21:50 -070062// See comment above RpcIdentifiers typedef, for the reason why the
63// RpcIdentifiersAccessor exists (even though it has the same
64// underlying type as StringsAccessor).
65typedef std::tr1::shared_ptr<
Jason Glasgowacdc11f2012-03-30 14:12:22 -040066 AccessorInterface<RpcIdentifier> > RpcIdentifierAccessor;
67typedef std::tr1::shared_ptr<
mukesh agrawal2366eed2012-03-20 18:21:50 -070068 AccessorInterface<std::vector<std::string> > >RpcIdentifiersAccessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070069typedef std::tr1::shared_ptr<AccessorInterface<std::string> > StringAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070070typedef std::tr1::shared_ptr<AccessorInterface<Stringmap> > StringmapAccessor;
Chris Masone889666b2011-07-03 12:58:50 -070071typedef std::tr1::shared_ptr<AccessorInterface<Stringmaps> > StringmapsAccessor;
Chris Masonea8a2c252011-06-27 22:16:30 -070072typedef std::tr1::shared_ptr<AccessorInterface<Strings> > StringsAccessor;
Darin Petkov63138a92012-02-06 14:09:15 +010073typedef std::tr1::shared_ptr<
74 AccessorInterface<KeyValueStore> > KeyValueStoreAccessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070075typedef std::tr1::shared_ptr<AccessorInterface<uint8> > Uint8Accessor;
76typedef std::tr1::shared_ptr<AccessorInterface<uint16> > Uint16Accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -070077typedef std::tr1::shared_ptr<AccessorInterface<uint32> > Uint32Accessor;
Paul Stewarte18c33b2012-07-10 20:48:44 -070078typedef std::tr1::shared_ptr<AccessorInterface<uint64> > Uint64Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070079
80} // namespace shill
81
82#endif // SHILL_ACCESSOR_INTERFACE_