Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // 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_PROPERTY_STORE_INSPECTOR_ |
| 6 | #define SHILL_PROPERTY_STORE_INSPECTOR_ |
| 7 | |
| 8 | #ifndef UNIT_TEST |
| 9 | #error "Do not use PropertyStoreInspector in non-test code." |
| 10 | #endif |
| 11 | |
| 12 | #include "shill/property_store.h" |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | // This class supplies functions to unit tests for inspecting a PropertyStore |
| 17 | // class to get single attributes. We never need to do this in the real |
| 18 | // PropertyStore, since the only interface to it over the control API is |
| 19 | // "GetProperties" which iterates over the entire store's properties. |
| 20 | // |
| 21 | // This code is done inefficiently -- if we were ever meant to retrieve |
| 22 | // single properties in production code, we'd access the map directly. |
| 23 | // For this reason, this class should only be used in unit tests. |
| 24 | class PropertyStoreInspector { |
| 25 | public: |
| 26 | PropertyStoreInspector(); |
| 27 | explicit PropertyStoreInspector(const PropertyStore *store); |
| 28 | virtual ~PropertyStoreInspector(); |
| 29 | |
| 30 | const PropertyStore *store() const { return store_; } |
| 31 | void set_store(const PropertyStore *store) { store_ = store; } |
| 32 | |
| 33 | // Methods to allow the getting, of properties stored in the referenced |
| 34 | // |store_| by name. Upon success, these methods return true, return |
| 35 | // the property value in |value|, and leave |error| untouched. Upon |
| 36 | // failure, they return false and if non-NULL, |error| is set appropriately. |
| 37 | // The value for |error| may be returned by the property getter itself, or |
| 38 | // if the property is not found in the store, these functions return |
| 39 | // Error::kNotFound. |
| 40 | bool GetBoolProperty(const std::string &name, bool *value, Error *error); |
| 41 | bool GetInt16Property(const std::string &name, int16 *value, Error *error); |
| 42 | bool GetInt32Property(const std::string &name, int32 *value, Error *error); |
| 43 | bool GetKeyValueStoreProperty(const std::string &name, |
| 44 | KeyValueStore *value, |
| 45 | Error *error); |
| 46 | bool GetStringProperty(const std::string &name, |
| 47 | std::string *value, |
| 48 | Error *error); |
| 49 | bool GetStringmapProperty(const std::string &name, |
| 50 | std::map<std::string, std::string> *values, |
| 51 | Error *error); |
| 52 | bool GetStringsProperty(const std::string &name, |
| 53 | std::vector<std::string> *values, |
| 54 | Error *error); |
| 55 | bool GetUint8Property(const std::string &name, uint8 *value, Error *error); |
| 56 | bool GetUint16Property(const std::string &name, uint16 *value, Error *error); |
| 57 | bool GetUint32Property(const std::string &name, uint32 *value, Error *error); |
| 58 | bool GetRpcIdentifierProperty(const std::string &name, |
| 59 | RpcIdentifier *value, |
| 60 | Error *error); |
| 61 | |
| 62 | // Methods to test the whether a property is enumerable for reading -- rather |
| 63 | // specifically that trying to do so would either succeed or return |
| 64 | // an error other than Error::kNotFound. Therefore, this function will |
| 65 | // return true for both successfully readable properties or properties that |
| 66 | // return a different error for their getter (e.g., Error:kPermissionDenied). |
| 67 | bool ContainsBoolProperty(const std::string &name); |
| 68 | bool ContainsInt16Property(const std::string &name); |
| 69 | bool ContainsInt32Property(const std::string &name); |
| 70 | bool ContainsKeyValueStoreProperty(const std::string &name); |
| 71 | bool ContainsStringProperty(const std::string &name); |
| 72 | bool ContainsStringmapProperty(const std::string &name); |
| 73 | bool ContainsStringsProperty(const std::string &name); |
| 74 | bool ContainsUint8Property(const std::string &name); |
| 75 | bool ContainsUint16Property(const std::string &name); |
| 76 | bool ContainsUint32Property(const std::string &name); |
| 77 | bool ContainsRpcIdentifierProperty(const std::string &name); |
| 78 | |
| 79 | private: |
| 80 | template <class V> |
| 81 | bool GetProperty( |
| 82 | const std::string &name, |
| 83 | V *value, |
| 84 | Error *error, |
| 85 | ReadablePropertyConstIterator<V>(PropertyStore::*getter)() const); |
| 86 | |
| 87 | template <class V> |
| 88 | bool ContainsProperty( |
| 89 | const std::string &name, |
| 90 | ReadablePropertyConstIterator<V>(PropertyStore::*getter)() const); |
| 91 | |
| 92 | const PropertyStore *store_; |
| 93 | |
| 94 | DISALLOW_COPY_AND_ASSIGN(PropertyStoreInspector); |
| 95 | }; |
| 96 | |
| 97 | } // namespace shill |
| 98 | |
| 99 | |
| 100 | #endif // SHILL_PROPERTY_STORE_INSPECTOR_ |