Home | History | Annotate | Download | only in shill
      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 
     30 namespace shill {
     31 
     32 class 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.
     39 template <class T>
     40 class 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 
     59 typedef std::vector<uint8_t> ByteArray;
     60 typedef 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.
     66 typedef std::string RpcIdentifier;
     67 typedef std::vector<std::string> RpcIdentifiers;
     68 typedef std::vector<std::string> Strings;
     69 typedef std::map<std::string, std::string> Stringmap;
     70 typedef std::vector<Stringmap> Stringmaps;
     71 typedef 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.
     75 typedef std::shared_ptr<AccessorInterface<bool>> BoolAccessor;
     76 typedef std::shared_ptr<AccessorInterface<int16_t>> Int16Accessor;
     77 typedef 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).
     81 typedef std::shared_ptr<
     82     AccessorInterface<RpcIdentifier>> RpcIdentifierAccessor;
     83 typedef std::shared_ptr<
     84     AccessorInterface<std::vector<std::string>>> RpcIdentifiersAccessor;
     85 typedef std::shared_ptr<AccessorInterface<std::string>> StringAccessor;
     86 typedef std::shared_ptr<AccessorInterface<Stringmap>> StringmapAccessor;
     87 typedef std::shared_ptr<AccessorInterface<Stringmaps>> StringmapsAccessor;
     88 typedef std::shared_ptr<AccessorInterface<Strings>> StringsAccessor;
     89 typedef std::shared_ptr<
     90     AccessorInterface<KeyValueStore>> KeyValueStoreAccessor;
     91 typedef std::shared_ptr<AccessorInterface<uint8_t>> Uint8Accessor;
     92 typedef std::shared_ptr<AccessorInterface<ByteArray>> ByteArrayAccessor;
     93 typedef std::shared_ptr<AccessorInterface<uint16_t>> Uint16Accessor;
     94 typedef std::shared_ptr<AccessorInterface<Uint16s>> Uint16sAccessor;
     95 typedef std::shared_ptr<AccessorInterface<uint32_t>> Uint32Accessor;
     96 typedef std::shared_ptr<AccessorInterface<uint64_t>> Uint64Accessor;
     97 
     98 }  // namespace shill
     99 
    100 #endif  // SHILL_ACCESSOR_INTERFACE_H_
    101