Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 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_ITERATOR_H_ |
| 6 | #define SHILL_PROPERTY_ITERATOR_H_ |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "shill/accessor_interface.h" |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 12 | #include "shill/error.h" |
| 13 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 14 | namespace shill { |
| 15 | |
| 16 | // 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] | 17 | // we're using to store key/value pairs for properties. It is intended for use |
| 18 | // with PropertyStore and always advances to the next readable property. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 19 | template <class V> |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 20 | class ReadablePropertyConstIterator { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 21 | public: |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 22 | ~ReadablePropertyConstIterator() {} |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 23 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 24 | bool AtEnd() const { return it_ == collection_.end(); } |
| 25 | |
| 26 | void Advance() { |
| 27 | if (!AtEnd()) { |
| 28 | do { |
| 29 | ++it_; |
| 30 | } while (MustAdvance()); |
| 31 | } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 32 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 33 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 34 | const std::string &Key() const { return it_->first; } |
| 35 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 36 | const V &value() const { return value_; } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 37 | |
| 38 | private: |
| 39 | friend class PropertyStore; |
| 40 | |
| 41 | typedef std::tr1::shared_ptr<AccessorInterface<V> > VAccessorPtr; |
| 42 | |
| 43 | explicit ReadablePropertyConstIterator( |
| 44 | const typename std::map<std::string, VAccessorPtr> &collection) |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 45 | : collection_(collection), |
| 46 | it_(collection_.begin()), |
| 47 | value_() { |
| 48 | if (MustAdvance()) { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 49 | Advance(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 50 | } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 51 | } |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 52 | |
| 53 | bool MustAdvance() { return !AtEnd() && !RetrieveCurrentValue(); } |
| 54 | |
| 55 | bool RetrieveCurrentValue() { |
| 56 | Error error; |
| 57 | value_ = it_->second->Get(&error); |
| 58 | return error.IsSuccess(); |
| 59 | } |
| 60 | |
| 61 | const typename std::map<std::string, VAccessorPtr> &collection_; |
| 62 | typename std::map<std::string, VAccessorPtr>::const_iterator it_; |
| 63 | V value_; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 64 | }; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 65 | |
| 66 | } // namespace shill |
| 67 | |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 68 | #endif // SHILL_PROPERTY_ITERATOR_H_ |