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 | #include "shill/property_store_inspector.h" |
| 6 | |
| 7 | #include "shill/error.h" |
| 8 | |
| 9 | using std::map; |
| 10 | using std::string; |
| 11 | using std::vector; |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | PropertyStoreInspector::PropertyStoreInspector() {} |
| 16 | |
| 17 | PropertyStoreInspector::PropertyStoreInspector(const PropertyStore *store) |
| 18 | : store_(store) {} |
| 19 | |
| 20 | PropertyStoreInspector::~PropertyStoreInspector() {} |
| 21 | |
| 22 | bool PropertyStoreInspector::GetBoolProperty(const string &name, |
| 23 | bool *value, |
| 24 | Error *error) { |
| 25 | return GetProperty(name, value, error, &PropertyStore::GetBoolPropertiesIter); |
| 26 | } |
| 27 | |
| 28 | bool PropertyStoreInspector::GetInt16Property(const string &name, |
| 29 | int16 *value, |
| 30 | Error *error) { |
| 31 | return GetProperty(name, value, error, |
| 32 | &PropertyStore::GetInt16PropertiesIter); |
| 33 | } |
| 34 | |
| 35 | bool PropertyStoreInspector::GetInt32Property(const string &name, |
| 36 | int32 *value, |
| 37 | Error *error) { |
| 38 | return GetProperty(name, value, error, |
| 39 | &PropertyStore::GetInt32PropertiesIter); |
| 40 | } |
| 41 | |
| 42 | bool PropertyStoreInspector::GetKeyValueStoreProperty(const string &name, |
| 43 | KeyValueStore *value, |
| 44 | Error *error) { |
| 45 | return GetProperty(name, value, error, |
| 46 | &PropertyStore::GetKeyValueStorePropertiesIter); |
| 47 | } |
| 48 | |
| 49 | bool PropertyStoreInspector::GetStringProperty(const string &name, |
| 50 | string *value, |
| 51 | Error *error) { |
| 52 | return GetProperty(name, value, error, |
| 53 | &PropertyStore::GetStringPropertiesIter); |
| 54 | } |
| 55 | |
| 56 | bool PropertyStoreInspector::GetStringmapProperty(const string &name, |
| 57 | map<string, string> *values, |
| 58 | Error *error) { |
| 59 | return GetProperty(name, values, error, |
| 60 | &PropertyStore::GetStringmapPropertiesIter); |
| 61 | } |
| 62 | |
| 63 | bool PropertyStoreInspector::GetStringsProperty(const string &name, |
| 64 | vector<string> *values, |
| 65 | Error *error) { |
| 66 | return GetProperty(name, values, error, |
| 67 | &PropertyStore::GetStringsPropertiesIter); |
| 68 | } |
| 69 | |
| 70 | bool PropertyStoreInspector::GetUint8Property(const string &name, |
| 71 | uint8 *value, |
| 72 | Error *error) { |
| 73 | return GetProperty(name, value, error, |
| 74 | &PropertyStore::GetUint8PropertiesIter); |
| 75 | } |
| 76 | |
| 77 | bool PropertyStoreInspector::GetUint16Property(const string &name, |
| 78 | uint16 *value, |
| 79 | Error *error) { |
| 80 | return GetProperty(name, value, error, |
| 81 | &PropertyStore::GetUint16PropertiesIter); |
| 82 | } |
| 83 | |
| 84 | bool PropertyStoreInspector::GetUint32Property(const string &name, |
| 85 | uint32 *value, |
| 86 | Error *error) { |
| 87 | return GetProperty(name, value, error, |
| 88 | &PropertyStore::GetUint32PropertiesIter); |
| 89 | } |
| 90 | |
| 91 | bool PropertyStoreInspector::GetRpcIdentifierProperty(const string &name, |
| 92 | RpcIdentifier *value, |
| 93 | Error *error) { |
| 94 | return GetProperty(name, value, error, |
| 95 | &PropertyStore::GetRpcIdentifierPropertiesIter); |
| 96 | } |
| 97 | |
| 98 | bool PropertyStoreInspector::ContainsBoolProperty(const string &name) { |
| 99 | return ContainsProperty(name, &PropertyStore::GetBoolPropertiesIter); |
| 100 | } |
| 101 | |
| 102 | bool PropertyStoreInspector::ContainsInt16Property(const string &name) { |
| 103 | return ContainsProperty(name, &PropertyStore::GetInt16PropertiesIter); |
| 104 | } |
| 105 | |
| 106 | bool PropertyStoreInspector::ContainsInt32Property(const string &name) { |
| 107 | return ContainsProperty(name, &PropertyStore::GetInt32PropertiesIter); |
| 108 | } |
| 109 | |
| 110 | bool PropertyStoreInspector::ContainsKeyValueStoreProperty(const string &name) { |
| 111 | return ContainsProperty(name, &PropertyStore::GetKeyValueStorePropertiesIter); |
| 112 | } |
| 113 | |
| 114 | bool PropertyStoreInspector::ContainsStringProperty(const string &name) { |
| 115 | return ContainsProperty(name, &PropertyStore::GetStringPropertiesIter); |
| 116 | } |
| 117 | |
| 118 | bool PropertyStoreInspector::ContainsStringmapProperty(const string &name) { |
| 119 | return ContainsProperty(name, &PropertyStore::GetStringmapPropertiesIter); |
| 120 | } |
| 121 | |
| 122 | bool PropertyStoreInspector::ContainsStringsProperty(const string &name) { |
| 123 | return ContainsProperty(name, &PropertyStore::GetStringsPropertiesIter); |
| 124 | } |
| 125 | |
| 126 | bool PropertyStoreInspector::ContainsUint8Property(const string &name) { |
| 127 | return ContainsProperty(name, &PropertyStore::GetUint8PropertiesIter); |
| 128 | } |
| 129 | |
| 130 | bool PropertyStoreInspector::ContainsUint16Property(const string &name) { |
| 131 | return ContainsProperty(name, &PropertyStore::GetUint16PropertiesIter); |
| 132 | } |
| 133 | |
| 134 | bool PropertyStoreInspector::ContainsUint32Property(const string &name) { |
| 135 | return ContainsProperty(name, &PropertyStore::GetUint32PropertiesIter); |
| 136 | } |
| 137 | |
| 138 | bool PropertyStoreInspector::ContainsRpcIdentifierProperty(const string &name) { |
| 139 | return ContainsProperty(name, &PropertyStore::GetRpcIdentifierPropertiesIter); |
| 140 | } |
| 141 | |
| 142 | template <class V> |
| 143 | bool PropertyStoreInspector::GetProperty( |
| 144 | const string &name, |
| 145 | V *value, |
| 146 | Error *error, |
| 147 | ReadablePropertyConstIterator<V>(PropertyStore::*getter)() const) { |
| 148 | ReadablePropertyConstIterator<V> it = ((*store_).*getter)(); |
| 149 | for ( ; !it.AtEnd(); it.Advance()) { |
| 150 | if (it.Key() == name) { |
| 151 | *value = it.Value(error); |
| 152 | return error->IsSuccess(); |
| 153 | } |
| 154 | } |
| 155 | error->Populate(Error::kNotFound); |
| 156 | return false; |
| 157 | }; |
| 158 | |
| 159 | template <class V> |
| 160 | bool PropertyStoreInspector::ContainsProperty( |
| 161 | const std::string &name, |
| 162 | ReadablePropertyConstIterator<V>(PropertyStore::*getter)() const) { |
| 163 | V value; |
| 164 | Error error; |
| 165 | GetProperty(name, &value, &error, getter); |
| 166 | return error.type() != Error::kNotFound; |
| 167 | } |
| 168 | |
| 169 | } // namespace shill |