blob: ceefc976737aca5f459fe552e0194288238d15c3 [file] [log] [blame]
Darin Petkov4682aa82012-05-31 16:24:11 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masonea8a2c252011-06-27 22:16:30 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov4682aa82012-05-31 16:24:11 +02005#ifndef SHILL_PROPERTY_ITERATOR_H_
6#define SHILL_PROPERTY_ITERATOR_H_
Chris Masonea8a2c252011-06-27 22:16:30 -07007
8#include <map>
9#include <string>
10
11#include "shill/accessor_interface.h"
Gaurav Shah1b7a6162011-11-09 11:41:01 -080012#include "shill/error.h"
13
Chris Masonea8a2c252011-06-27 22:16:30 -070014namespace shill {
15
16// An iterator wrapper class to hide the details of what kind of data structure
Darin Petkov4682aa82012-05-31 16:24:11 +020017// 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 Masonea8a2c252011-06-27 22:16:30 -070019template <class V>
Darin Petkov4682aa82012-05-31 16:24:11 +020020class ReadablePropertyConstIterator {
Chris Masonea8a2c252011-06-27 22:16:30 -070021 public:
Darin Petkov4682aa82012-05-31 16:24:11 +020022 ~ReadablePropertyConstIterator() {}
Chris Masonea8a2c252011-06-27 22:16:30 -070023
Darin Petkov4682aa82012-05-31 16:24:11 +020024 bool AtEnd() const { return it_ == collection_.end(); }
25
26 void Advance() {
27 if (!AtEnd()) {
28 do {
29 ++it_;
30 } while (MustAdvance());
31 }
Gaurav Shah1b7a6162011-11-09 11:41:01 -080032 }
Chris Masonea8a2c252011-06-27 22:16:30 -070033
Paul Stewart1a212a62015-06-16 13:13:10 -070034 const std::string& Key() const { return it_->first; }
Chris Masonea8a2c252011-06-27 22:16:30 -070035
Paul Stewart1a212a62015-06-16 13:13:10 -070036 const V& value() const { return value_; }
Gaurav Shah1b7a6162011-11-09 11:41:01 -080037
38 private:
39 friend class PropertyStore;
40
Ben Chane2ee5e02014-09-19 19:29:42 -070041 typedef std::shared_ptr<AccessorInterface<V>> VAccessorPtr;
Gaurav Shah1b7a6162011-11-09 11:41:01 -080042
43 explicit ReadablePropertyConstIterator(
Paul Stewart1a212a62015-06-16 13:13:10 -070044 const typename std::map<std::string, VAccessorPtr>& collection)
Darin Petkov4682aa82012-05-31 16:24:11 +020045 : collection_(collection),
46 it_(collection_.begin()),
47 value_() {
48 if (MustAdvance()) {
Gaurav Shah1b7a6162011-11-09 11:41:01 -080049 Advance();
Darin Petkov4682aa82012-05-31 16:24:11 +020050 }
Gaurav Shah1b7a6162011-11-09 11:41:01 -080051 }
Darin Petkov4682aa82012-05-31 16:24:11 +020052
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
Paul Stewart1a212a62015-06-16 13:13:10 -070061 const typename std::map<std::string, VAccessorPtr>& collection_;
Darin Petkov4682aa82012-05-31 16:24:11 +020062 typename std::map<std::string, VAccessorPtr>::const_iterator it_;
63 V value_;
Gaurav Shah1b7a6162011-11-09 11:41:01 -080064};
Chris Masonea8a2c252011-06-27 22:16:30 -070065
66} // namespace shill
67
Darin Petkov4682aa82012-05-31 16:24:11 +020068#endif // SHILL_PROPERTY_ITERATOR_H_