blob: 682695b44c0fac7c0f1d963bac9c0381eb267536 [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 agrawalbebf1b82013-04-23 15:06:33 -070037 // Attempts to set the wrapped value. Sets |error| on failure. The
38 // return value indicates whether or not the wrapped value was
39 // modified. If the new value is the same as the old value, Set
40 // returns false, but with |error| unchanged.
41 virtual bool Set(const T &value, Error *error) = 0;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070042
43 private:
44 DISALLOW_COPY_AND_ASSIGN(AccessorInterface);
45};
46
Paul Stewartced6a0b2011-11-08 15:32:04 -080047typedef std::vector<uint8_t> ByteArray;
48typedef std::vector<ByteArray> ByteArrays;
mukesh agrawal2366eed2012-03-20 18:21:50 -070049// Note that while the RpcIdentifiers type has the same concrete
50// representation as the Strings type, it may be serialized
51// differently. Accordingly, PropertyStore tracks RpcIdentifiers
52// separately from Strings. We create a separate typedef here, to make
53// the PropertyStore-related code read more simply.
Jason Glasgowacdc11f2012-03-30 14:12:22 -040054typedef std::string RpcIdentifier;
mukesh agrawal2366eed2012-03-20 18:21:50 -070055typedef std::vector<std::string> RpcIdentifiers;
Chris Masonea8a2c252011-06-27 22:16:30 -070056typedef std::vector<std::string> Strings;
57typedef std::map<std::string, std::string> Stringmap;
Chris Masone889666b2011-07-03 12:58:50 -070058typedef std::vector<Stringmap> Stringmaps;
mukesh agrawale7c7e652013-06-18 17:19:39 -070059typedef std::vector<uint16> Uint16s;
Chris Masonea8a2c252011-06-27 22:16:30 -070060
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061// Using a smart pointer here allows pointers to classes derived from
62// AccessorInterface<> to be stored in maps and other STL container types.
mukesh agrawale7c7e652013-06-18 17:19:39 -070063typedef std::tr1::shared_ptr<AccessorInterface<bool>> BoolAccessor;
64typedef std::tr1::shared_ptr<AccessorInterface<int16>> Int16Accessor;
65typedef std::tr1::shared_ptr<AccessorInterface<int32>> Int32Accessor;
mukesh agrawal2366eed2012-03-20 18:21:50 -070066// See comment above RpcIdentifiers typedef, for the reason why the
67// RpcIdentifiersAccessor exists (even though it has the same
68// underlying type as StringsAccessor).
69typedef std::tr1::shared_ptr<
mukesh agrawale7c7e652013-06-18 17:19:39 -070070 AccessorInterface<RpcIdentifier>> RpcIdentifierAccessor;
Jason Glasgowacdc11f2012-03-30 14:12:22 -040071typedef std::tr1::shared_ptr<
mukesh agrawale7c7e652013-06-18 17:19:39 -070072 AccessorInterface<std::vector<std::string>>>RpcIdentifiersAccessor;
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;
Darin Petkov63138a92012-02-06 14:09:15 +010077typedef std::tr1::shared_ptr<
mukesh agrawale7c7e652013-06-18 17:19:39 -070078 AccessorInterface<KeyValueStore>> KeyValueStoreAccessor;
79typedef std::tr1::shared_ptr<AccessorInterface<uint8>> Uint8Accessor;
80typedef std::tr1::shared_ptr<AccessorInterface<uint16>> Uint16Accessor;
81typedef std::tr1::shared_ptr<AccessorInterface<Uint16s>> Uint16sAccessor;
82typedef std::tr1::shared_ptr<AccessorInterface<uint32>> Uint32Accessor;
83typedef std::tr1::shared_ptr<AccessorInterface<uint64>> Uint64Accessor;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070084
85} // namespace shill
86
87#endif // SHILL_ACCESSOR_INTERFACE_