1 // Copyright (c) 2013 The Chromium 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 #include <iterator> 6 #include <map> 7 #include <string> 8 9 #include "scoped_refptr.h" 10 11 struct Foo { 12 int dummy; 13 }; 14 15 typedef std::map<std::string, scoped_refptr<const Foo>> MyMap; 16 17 class MyIter 18 : public std::iterator<std::input_iterator_tag, scoped_refptr<const Foo>> { 19 public: MyIter()20 MyIter() {} MyIter(const MyIter & other)21 MyIter(const MyIter& other) : it_(other.it_) {} MyIter(MyMap::const_iterator it)22 explicit MyIter(MyMap::const_iterator it) : it_(it) {} operator ++()23 MyIter& operator++() { 24 ++it_; 25 return *this; 26 } operator *()27 const scoped_refptr<const Foo> operator*() { return it_->second; } operator !=(const MyIter & other)28 bool operator!=(const MyIter& other) { return it_ != other.it_; } operator ==(const MyIter & other)29 bool operator==(const MyIter& other) { return it_ == other.it_; } 30 31 private: 32 MyMap::const_iterator it_; 33 }; 34 TestsAScopedRefptr()35void TestsAScopedRefptr() { 36 MyMap map; 37 map["foo"] = new Foo; 38 map["bar"] = new Foo; 39 MyIter my_begin(map.begin()); 40 MyIter my_end(map.end()); 41 for (MyIter it = my_begin; it != my_end; ++it) { 42 const Foo* item = NULL; 43 if (*it) 44 item = *it; 45 } 46 } 47