// Really dumb iterator_adaptor example program -----------------------------// // (C) Copyright Beman Dawes 2002. Permission to copy, use, modify, sell and // distribute this software is granted provided this copyright notice appears // in all copies. This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. #include #include #include class my_base { public: std::string name; my_base() : name( "foo" ) {} void operator++() { name = "bar"; } bool operator==( const my_base & rhs ) const { return name == rhs.name; } }; // if my_base had "const std::string & operator*() const { return name; }" // then custom policies would not be required. class my_policies : public boost::default_iterator_policies { public: template typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const { return x.base().name; } }; typedef boost::iterator_adaptor< my_base, my_policies, // default_iterator_policies would do if my_base::op* were present const std::string, const std::string &, const std::string *, std::forward_iterator_tag, std::string::difference_type > my_iterator; int main() { my_iterator itr; my_iterator::value_type str( "hello, iterator\n" ); //verify value_type present std::cout << str; std::cout << *itr << std::endl; // foo std::cout << *++itr << std::endl; // bar std::cout << (itr == my_iterator() ? "bingo!" : "oops!" ) << std::endl; // oops std::cout << (itr == ++my_iterator() ? "bingo!" : "oops!" ) << std::endl; // bingo return 0; }