1c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 2c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Copyright (C) 2012 The Android Open Source Project 3c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 4c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Licensed under the Apache License, Version 2.0 (the "License"); 5c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// you may not use this file except in compliance with the License. 6c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// You may obtain a copy of the License at 7c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 8c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// http://www.apache.org/licenses/LICENSE-2.0 9c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 10c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Unless required by applicable law or agreed to in writing, software 11c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// distributed under the License is distributed on an "AS IS" BASIS, 12c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// See the License for the specific language governing permissions and 14c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// limitations under the License. 15c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 163bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 17c45688bb3881f0c2216e6ec0e19ebda0be33e871Ben Chan#ifndef SHILL_ACCESSOR_INTERFACE_H_ 18c45688bb3881f0c2216e6ec0e19ebda0be33e871Ben Chan#define SHILL_ACCESSOR_INTERFACE_H_ 193bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 203bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone#include <map> 218a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenko#include <memory> 22a41ab517725d036b63420f8445550246f8f50b99Alex Vakulenko#include <string> 23889666b49d97f61a993a4e642e5728cce1c3758aChris Masone#include <utility> 24b925cc8f481d21fddd9569fc68861f6e5b6e3eaeChris Masone#include <vector> 253bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 26cc67c52a2c00f90e877971d552208dd99825d84eBen Chan#include <base/macros.h> 273bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 2863138a9b8249fd69c83a772ee3170551a589d57aDarin Petkov#include "shill/key_value_store.h" 2963138a9b8249fd69c83a772ee3170551a589d57aDarin Petkov 303bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masonenamespace shill { 313bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 32ffa3d0433d419aa9251f3768a0090a27b6b1c434mukesh agrawalclass Error; 33ffa3d0433d419aa9251f3768a0090a27b6b1c434mukesh agrawal 343bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone// A templated abstract base class for objects that can be used to access 353bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone// properties stored in objects that are meant to be made available over RPC. 363bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone// The intended usage is that an object stores a maps of strings to 373bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone// AccessorInterfaces of the appropriate type, and then uses 383bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone// map[name]->Get() and map[name]->Set(value) to get and set the properties. 393bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masonetemplate <class T> 403bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masoneclass AccessorInterface { 413bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone public: 423bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone AccessorInterface() {} 433bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone virtual ~AccessorInterface() {} 443bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 45292dc0f18ce6ba7ea1419efc4d8ef909712bb7c2mukesh agrawal // Reset the property to its default value. Sets |error| on failure. 46a794cd60a7339d576ea2eed263a4f0a20fb255afPaul Stewart virtual void Clear(Error* error) = 0; 471b7a616197af7ff753dbe7614a8e207b1e10ac1aGaurav Shah // Provides read-only access. Sets |error| on failure. 48a794cd60a7339d576ea2eed263a4f0a20fb255afPaul Stewart virtual T Get(Error* error) = 0; 49bebf1b8bce52b88c2cc2d93200b9405f9c19cf21mukesh agrawal // Attempts to set the wrapped value. Sets |error| on failure. The 50bebf1b8bce52b88c2cc2d93200b9405f9c19cf21mukesh agrawal // return value indicates whether or not the wrapped value was 51bebf1b8bce52b88c2cc2d93200b9405f9c19cf21mukesh agrawal // modified. If the new value is the same as the old value, Set 52bebf1b8bce52b88c2cc2d93200b9405f9c19cf21mukesh agrawal // returns false, but with |error| unchanged. 53a794cd60a7339d576ea2eed263a4f0a20fb255afPaul Stewart virtual bool Set(const T& value, Error* error) = 0; 543bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 553bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone private: 563bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone DISALLOW_COPY_AND_ASSIGN(AccessorInterface); 573bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone}; 583bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 59ced6a0b2c2c3952ba79e10709345b573d2e06af6Paul Stewarttypedef std::vector<uint8_t> ByteArray; 60ced6a0b2c2c3952ba79e10709345b573d2e06af6Paul Stewarttypedef std::vector<ByteArray> ByteArrays; 612366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// Note that while the RpcIdentifiers type has the same concrete 622366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// representation as the Strings type, it may be serialized 632366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// differently. Accordingly, PropertyStore tracks RpcIdentifiers 642366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// separately from Strings. We create a separate typedef here, to make 652366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// the PropertyStore-related code read more simply. 66acdc11f4961994f894f978713d1a22f2eb23687dJason Glasgowtypedef std::string RpcIdentifier; 672366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawaltypedef std::vector<std::string> RpcIdentifiers; 68a8a2c25df27d77f278511769f0c9029b03dff053Chris Masonetypedef std::vector<std::string> Strings; 69a8a2c25df27d77f278511769f0c9029b03dff053Chris Masonetypedef std::map<std::string, std::string> Stringmap; 70889666b49d97f61a993a4e642e5728cce1c3758aChris Masonetypedef std::vector<Stringmap> Stringmaps; 717fab89734d88724a288e96a9996b15548c5294c7Ben Chantypedef std::vector<uint16_t> Uint16s; 72a8a2c25df27d77f278511769f0c9029b03dff053Chris Masone 733bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone// Using a smart pointer here allows pointers to classes derived from 743bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone// AccessorInterface<> to be stored in maps and other STL container types. 758a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr<AccessorInterface<bool>> BoolAccessor; 767fab89734d88724a288e96a9996b15548c5294c7Ben Chantypedef std::shared_ptr<AccessorInterface<int16_t>> Int16Accessor; 777fab89734d88724a288e96a9996b15548c5294c7Ben Chantypedef std::shared_ptr<AccessorInterface<int32_t>> Int32Accessor; 782366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// See comment above RpcIdentifiers typedef, for the reason why the 792366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// RpcIdentifiersAccessor exists (even though it has the same 802366eede28daa298f240ddbc3c8f36cc0e7b7ad1mukesh agrawal// underlying type as StringsAccessor). 818a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr< 82e7c7e65bba9e964aa500ef53cc79215426acf50cmukesh agrawal AccessorInterface<RpcIdentifier>> RpcIdentifierAccessor; 838a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr< 848a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenko AccessorInterface<std::vector<std::string>>> RpcIdentifiersAccessor; 858a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr<AccessorInterface<std::string>> StringAccessor; 868a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr<AccessorInterface<Stringmap>> StringmapAccessor; 878a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr<AccessorInterface<Stringmaps>> StringmapsAccessor; 888a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr<AccessorInterface<Strings>> StringsAccessor; 898a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr< 90e7c7e65bba9e964aa500ef53cc79215426acf50cmukesh agrawal AccessorInterface<KeyValueStore>> KeyValueStoreAccessor; 917fab89734d88724a288e96a9996b15548c5294c7Ben Chantypedef std::shared_ptr<AccessorInterface<uint8_t>> Uint8Accessor; 921ee315fe16a7e91f43f1b24f5f2c0a47b52e3455Victor Dodontypedef std::shared_ptr<AccessorInterface<ByteArray>> ByteArrayAccessor; 937fab89734d88724a288e96a9996b15548c5294c7Ben Chantypedef std::shared_ptr<AccessorInterface<uint16_t>> Uint16Accessor; 948a5322984f2d81bcbfd8d44c59747a11bd9b904bAlex Vakulenkotypedef std::shared_ptr<AccessorInterface<Uint16s>> Uint16sAccessor; 957fab89734d88724a288e96a9996b15548c5294c7Ben Chantypedef std::shared_ptr<AccessorInterface<uint32_t>> Uint32Accessor; 967fab89734d88724a288e96a9996b15548c5294c7Ben Chantypedef std::shared_ptr<AccessorInterface<uint64_t>> Uint64Accessor; 973bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 983bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone} // namespace shill 993bd3c8c33917221d1074f1aa19272e45c0ce2793Chris Masone 100c45688bb3881f0c2216e6ec0e19ebda0be33e871Ben Chan#endif // SHILL_ACCESSOR_INTERFACE_H_ 101