Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 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 | // |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 16 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 17 | #ifndef SHILL_PROPERTY_ITERATOR_H_ |
| 18 | #define SHILL_PROPERTY_ITERATOR_H_ |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 19 | |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "shill/accessor_interface.h" |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 24 | #include "shill/error.h" |
| 25 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 26 | namespace shill { |
| 27 | |
| 28 | // An iterator wrapper class to hide the details of what kind of data structure |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 29 | // we're using to store key/value pairs for properties. It is intended for use |
| 30 | // with PropertyStore and always advances to the next readable property. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 31 | template <class V> |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 32 | class ReadablePropertyConstIterator { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 33 | public: |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 34 | ~ReadablePropertyConstIterator() {} |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 35 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 36 | bool AtEnd() const { return it_ == collection_.end(); } |
| 37 | |
| 38 | void Advance() { |
| 39 | if (!AtEnd()) { |
| 40 | do { |
| 41 | ++it_; |
| 42 | } while (MustAdvance()); |
| 43 | } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 44 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 45 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 46 | const std::string& Key() const { return it_->first; } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 47 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 48 | const V& value() const { return value_; } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | friend class PropertyStore; |
| 52 | |
Ben Chan | e2ee5e0 | 2014-09-19 19:29:42 -0700 | [diff] [blame] | 53 | typedef std::shared_ptr<AccessorInterface<V>> VAccessorPtr; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 54 | |
| 55 | explicit ReadablePropertyConstIterator( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 56 | const typename std::map<std::string, VAccessorPtr>& collection) |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 57 | : collection_(collection), |
| 58 | it_(collection_.begin()), |
| 59 | value_() { |
| 60 | if (MustAdvance()) { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 61 | Advance(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 62 | } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 63 | } |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 64 | |
| 65 | bool MustAdvance() { return !AtEnd() && !RetrieveCurrentValue(); } |
| 66 | |
| 67 | bool RetrieveCurrentValue() { |
| 68 | Error error; |
| 69 | value_ = it_->second->Get(&error); |
| 70 | return error.IsSuccess(); |
| 71 | } |
| 72 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 73 | const typename std::map<std::string, VAccessorPtr>& collection_; |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 74 | typename std::map<std::string, VAccessorPtr>::const_iterator it_; |
| 75 | V value_; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 76 | }; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 77 | |
| 78 | } // namespace shill |
| 79 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 80 | #endif // SHILL_PROPERTY_ITERATOR_H_ |