1//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef SHILL_ACCESSOR_INTERFACE_H_
18#define SHILL_ACCESSOR_INTERFACE_H_
19
20#include <map>
21#include <memory>
22#include <string>
23#include <utility>
24#include <vector>
25
26#include <base/macros.h>
27
28#include "shill/key_value_store.h"
29
30namespace shill {
31
32class Error;
33
34// A templated abstract base class for objects that can be used to access
35// properties stored in objects that are meant to be made available over RPC.
36// The intended usage is that an object stores a maps of strings to
37// AccessorInterfaces of the appropriate type, and then uses
38// map[name]->Get() and map[name]->Set(value) to get and set the properties.
39template <class T>
40class AccessorInterface {
41 public:
42  AccessorInterface() {}
43  virtual ~AccessorInterface() {}
44
45  // Reset the property to its default value. Sets |error| on failure.
46  virtual void Clear(Error* error) = 0;
47  // Provides read-only access. Sets |error| on failure.
48  virtual T Get(Error* error) = 0;
49  // Attempts to set the wrapped value. Sets |error| on failure.  The
50  // return value indicates whether or not the wrapped value was
51  // modified. If the new value is the same as the old value, Set
52  // returns false, but with |error| unchanged.
53  virtual bool Set(const T& value, Error* error) = 0;
54
55 private:
56  DISALLOW_COPY_AND_ASSIGN(AccessorInterface);
57};
58
59typedef std::vector<uint8_t> ByteArray;
60typedef std::vector<ByteArray> ByteArrays;
61// Note that while the RpcIdentifiers type has the same concrete
62// representation as the Strings type, it may be serialized
63// differently. Accordingly, PropertyStore tracks RpcIdentifiers
64// separately from Strings. We create a separate typedef here, to make
65// the PropertyStore-related code read more simply.
66typedef std::string RpcIdentifier;
67typedef std::vector<std::string> RpcIdentifiers;
68typedef std::vector<std::string> Strings;
69typedef std::map<std::string, std::string> Stringmap;
70typedef std::vector<Stringmap> Stringmaps;
71typedef std::vector<uint16_t> Uint16s;
72
73// Using a smart pointer here allows pointers to classes derived from
74// AccessorInterface<> to be stored in maps and other STL container types.
75typedef std::shared_ptr<AccessorInterface<bool>> BoolAccessor;
76typedef std::shared_ptr<AccessorInterface<int16_t>> Int16Accessor;
77typedef std::shared_ptr<AccessorInterface<int32_t>> Int32Accessor;
78// See comment above RpcIdentifiers typedef, for the reason why the
79// RpcIdentifiersAccessor exists (even though it has the same
80// underlying type as StringsAccessor).
81typedef std::shared_ptr<
82    AccessorInterface<RpcIdentifier>> RpcIdentifierAccessor;
83typedef std::shared_ptr<
84    AccessorInterface<std::vector<std::string>>> RpcIdentifiersAccessor;
85typedef std::shared_ptr<AccessorInterface<std::string>> StringAccessor;
86typedef std::shared_ptr<AccessorInterface<Stringmap>> StringmapAccessor;
87typedef std::shared_ptr<AccessorInterface<Stringmaps>> StringmapsAccessor;
88typedef std::shared_ptr<AccessorInterface<Strings>> StringsAccessor;
89typedef std::shared_ptr<
90    AccessorInterface<KeyValueStore>> KeyValueStoreAccessor;
91typedef std::shared_ptr<AccessorInterface<uint8_t>> Uint8Accessor;
92typedef std::shared_ptr<AccessorInterface<ByteArray>> ByteArrayAccessor;
93typedef std::shared_ptr<AccessorInterface<uint16_t>> Uint16Accessor;
94typedef std::shared_ptr<AccessorInterface<Uint16s>> Uint16sAccessor;
95typedef std::shared_ptr<AccessorInterface<uint32_t>> Uint32Accessor;
96typedef std::shared_ptr<AccessorInterface<uint64_t>> Uint64Accessor;
97
98}  // namespace shill
99
100#endif  // SHILL_ACCESSOR_INTERFACE_H_
101