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 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 5 | #ifndef SHILL_PROPERTY_STORE_INSPECTOR_H_ |
| 6 | #define SHILL_PROPERTY_STORE_INSPECTOR_H_ |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 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 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 33 | // Methods to allow the getting of properties stored in the referenced |
| 34 | // |store_| by name. Upon success, these methods return true and return the |
| 35 | // property value in |value| if non-NULL. Upon failure, they return false and |
| 36 | // leave |value| untouched. |
| 37 | bool GetBoolProperty(const std::string &name, bool *value); |
| 38 | bool GetInt16Property(const std::string &name, int16 *value); |
| 39 | bool GetInt32Property(const std::string &name, int32 *value); |
| 40 | bool GetKeyValueStoreProperty(const std::string &name, KeyValueStore *value); |
| 41 | bool GetStringProperty(const std::string &name, std::string *value); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 42 | bool GetStringmapProperty(const std::string &name, |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 43 | std::map<std::string, std::string> *values); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 44 | bool GetStringsProperty(const std::string &name, |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 45 | std::vector<std::string> *values); |
| 46 | bool GetUint8Property(const std::string &name, uint8 *value); |
| 47 | bool GetUint16Property(const std::string &name, uint16 *value); |
| 48 | bool GetUint32Property(const std::string &name, uint32 *value); |
| 49 | bool GetRpcIdentifierProperty(const std::string &name, RpcIdentifier *value); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | template <class V> |
| 53 | bool GetProperty( |
| 54 | const std::string &name, |
| 55 | V *value, |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 56 | ReadablePropertyConstIterator<V>(PropertyStore::*getter)() const); |
| 57 | |
| 58 | const PropertyStore *store_; |
| 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(PropertyStoreInspector); |
| 61 | }; |
| 62 | |
| 63 | } // namespace shill |
| 64 | |
| 65 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 66 | #endif // SHILL_PROPERTY_STORE_INSPECTOR_H_ |