blob: 0fb3141fa304480491bfeff3597fef658bade3c5 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
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 Masonea8a2c252011-06-27 22:16:30 -070016
Darin Petkov4682aa82012-05-31 16:24:11 +020017#ifndef SHILL_PROPERTY_ITERATOR_H_
18#define SHILL_PROPERTY_ITERATOR_H_
Chris Masonea8a2c252011-06-27 22:16:30 -070019
20#include <map>
21#include <string>
22
23#include "shill/accessor_interface.h"
Gaurav Shah1b7a6162011-11-09 11:41:01 -080024#include "shill/error.h"
25
Chris Masonea8a2c252011-06-27 22:16:30 -070026namespace shill {
27
28// An iterator wrapper class to hide the details of what kind of data structure
Darin Petkov4682aa82012-05-31 16:24:11 +020029// 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 Masonea8a2c252011-06-27 22:16:30 -070031template <class V>
Darin Petkov4682aa82012-05-31 16:24:11 +020032class ReadablePropertyConstIterator {
Chris Masonea8a2c252011-06-27 22:16:30 -070033 public:
Darin Petkov4682aa82012-05-31 16:24:11 +020034 ~ReadablePropertyConstIterator() {}
Chris Masonea8a2c252011-06-27 22:16:30 -070035
Darin Petkov4682aa82012-05-31 16:24:11 +020036 bool AtEnd() const { return it_ == collection_.end(); }
37
38 void Advance() {
39 if (!AtEnd()) {
40 do {
41 ++it_;
42 } while (MustAdvance());
43 }
Gaurav Shah1b7a6162011-11-09 11:41:01 -080044 }
Chris Masonea8a2c252011-06-27 22:16:30 -070045
Paul Stewart1a212a62015-06-16 13:13:10 -070046 const std::string& Key() const { return it_->first; }
Chris Masonea8a2c252011-06-27 22:16:30 -070047
Paul Stewart1a212a62015-06-16 13:13:10 -070048 const V& value() const { return value_; }
Gaurav Shah1b7a6162011-11-09 11:41:01 -080049
50 private:
51 friend class PropertyStore;
52
Ben Chane2ee5e02014-09-19 19:29:42 -070053 typedef std::shared_ptr<AccessorInterface<V>> VAccessorPtr;
Gaurav Shah1b7a6162011-11-09 11:41:01 -080054
55 explicit ReadablePropertyConstIterator(
Paul Stewart1a212a62015-06-16 13:13:10 -070056 const typename std::map<std::string, VAccessorPtr>& collection)
Darin Petkov4682aa82012-05-31 16:24:11 +020057 : collection_(collection),
58 it_(collection_.begin()),
59 value_() {
60 if (MustAdvance()) {
Gaurav Shah1b7a6162011-11-09 11:41:01 -080061 Advance();
Darin Petkov4682aa82012-05-31 16:24:11 +020062 }
Gaurav Shah1b7a6162011-11-09 11:41:01 -080063 }
Darin Petkov4682aa82012-05-31 16:24:11 +020064
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 Stewart1a212a62015-06-16 13:13:10 -070073 const typename std::map<std::string, VAccessorPtr>& collection_;
Darin Petkov4682aa82012-05-31 16:24:11 +020074 typename std::map<std::string, VAccessorPtr>::const_iterator it_;
75 V value_;
Gaurav Shah1b7a6162011-11-09 11:41:01 -080076};
Chris Masonea8a2c252011-06-27 22:16:30 -070077
78} // namespace shill
79
Darin Petkov4682aa82012-05-31 16:24:11 +020080#endif // SHILL_PROPERTY_ITERATOR_H_