Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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_ITERATOR_ |
| 6 | #define SHILL_PROPERTY_ITERATOR_ |
| 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 | |
| 14 | class Error; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 15 | |
| 16 | namespace shill { |
| 17 | |
| 18 | // An iterator wrapper class to hide the details of what kind of data structure |
| 19 | // we're using to store key/value pairs for properties. |
| 20 | // Intended for use with PropertyStore. |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 21 | |
| 22 | // TODO(gauravsh): Consider getting rid of PropertyConstIterator, and just |
| 23 | // keeping ReadablePropertyConstIterator since it doesn't look like the caller |
| 24 | // is ever interested in anything but readable properties. |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 25 | template <class V> |
| 26 | class PropertyConstIterator { |
| 27 | public: |
| 28 | virtual ~PropertyConstIterator() {} |
| 29 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 30 | virtual void Advance() { |
| 31 | if (!AtEnd()) |
| 32 | ++it_; |
| 33 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 34 | |
| 35 | bool AtEnd() { return it_ == collection_.end(); } |
| 36 | |
| 37 | const std::string &Key() const { return it_->first; } |
| 38 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 39 | V Value(Error *error) const { return it_->second->Get(error); } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 40 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 41 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 42 | protected: |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 43 | typedef std::tr1::shared_ptr<AccessorInterface<V> > VAccessorPtr; |
| 44 | |
| 45 | explicit PropertyConstIterator( |
| 46 | const typename std::map<std::string, VAccessorPtr> &collection) |
| 47 | : collection_(collection), |
| 48 | it_(collection_.begin()) { |
| 49 | } |
| 50 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 51 | private: |
| 52 | friend class PropertyStore; |
| 53 | |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 54 | const typename std::map<std::string, VAccessorPtr> &collection_; |
| 55 | typename std::map<std::string, VAccessorPtr>::const_iterator it_; |
| 56 | }; |
| 57 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 58 | // A version of the iterator that always advances to the next readable |
| 59 | // property. |
| 60 | template <class V> |
| 61 | class ReadablePropertyConstIterator : public PropertyConstIterator<V> { |
| 62 | public: |
| 63 | virtual ~ReadablePropertyConstIterator() {} |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 64 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 65 | virtual void Advance() { |
| 66 | Error error; |
| 67 | while (!PropertyConstIterator<V>::AtEnd()) { |
| 68 | PropertyConstIterator<V>::Advance(); |
| 69 | if (PropertyConstIterator<V>::AtEnd()) |
| 70 | return; |
| 71 | error.Reset(); |
| 72 | PropertyConstIterator<V>::Value(&error); |
| 73 | if (error.IsSuccess()) |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | friend class PropertyStore; |
| 80 | |
| 81 | typedef std::tr1::shared_ptr<AccessorInterface<V> > VAccessorPtr; |
| 82 | |
| 83 | explicit ReadablePropertyConstIterator( |
| 84 | const typename std::map<std::string, VAccessorPtr> &collection) |
| 85 | : PropertyConstIterator<V>(collection) { |
| 86 | if (PropertyConstIterator<V>::AtEnd()) |
| 87 | return; |
| 88 | Error error; |
| 89 | PropertyConstIterator<V>::Value(&error); |
| 90 | // Start at the next readable property (if any). |
| 91 | if (!error.IsSuccess()) |
| 92 | Advance(); |
| 93 | } |
| 94 | }; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 95 | |
| 96 | } // namespace shill |
| 97 | |
| 98 | #endif // SHILL_PROPERTY_ITERATOR_ |