blob: c1c47dedc369bfa3251b17013b856209e7713e2e [file] [log] [blame]
Chris Masonea8a2c252011-06-27 22:16:30 -07001// 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 Shah1b7a6162011-11-09 11:41:01 -080012#include "shill/error.h"
13
14class Error;
Chris Masonea8a2c252011-06-27 22:16:30 -070015
16namespace 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 Shah1b7a6162011-11-09 11:41:01 -080021
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 Masonea8a2c252011-06-27 22:16:30 -070025template <class V>
26class PropertyConstIterator {
27 public:
28 virtual ~PropertyConstIterator() {}
29
Gaurav Shah1b7a6162011-11-09 11:41:01 -080030 virtual void Advance() {
31 if (!AtEnd())
32 ++it_;
33 }
Chris Masonea8a2c252011-06-27 22:16:30 -070034
35 bool AtEnd() { return it_ == collection_.end(); }
36
37 const std::string &Key() const { return it_->first; }
38
Gaurav Shah1b7a6162011-11-09 11:41:01 -080039 V Value(Error *error) const { return it_->second->Get(error); }
Chris Masonea8a2c252011-06-27 22:16:30 -070040
Chris Masonea8a2c252011-06-27 22:16:30 -070041
Gaurav Shah1b7a6162011-11-09 11:41:01 -080042 protected:
Chris Masonea8a2c252011-06-27 22:16:30 -070043 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 Shah1b7a6162011-11-09 11:41:01 -080051 private:
52 friend class PropertyStore;
53
Chris Masonea8a2c252011-06-27 22:16:30 -070054 const typename std::map<std::string, VAccessorPtr> &collection_;
55 typename std::map<std::string, VAccessorPtr>::const_iterator it_;
56};
57
Gaurav Shah1b7a6162011-11-09 11:41:01 -080058// A version of the iterator that always advances to the next readable
59// property.
60template <class V>
61class ReadablePropertyConstIterator : public PropertyConstIterator<V> {
62 public:
63 virtual ~ReadablePropertyConstIterator() {}
Chris Masonea8a2c252011-06-27 22:16:30 -070064
Gaurav Shah1b7a6162011-11-09 11:41:01 -080065 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 Masonea8a2c252011-06-27 22:16:30 -070095
96} // namespace shill
97
98#endif // SHILL_PROPERTY_ITERATOR_