00001 // Filename: iterator_types.h 00002 // Created by: drose (10Feb99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef ITERATOR_TYPES_H 00016 #define ITERATOR_TYPES_H 00017 00018 00019 //////////////////////////////////////////////////////////////////// 00020 // Class : first_of_pair_iterator 00021 // Description : This is an iterator adaptor that converts any 00022 // iterator that returns a pair (e.g. a map iterator) 00023 // into one that returns just the first component of 00024 // that pair. 00025 //////////////////////////////////////////////////////////////////// 00026 template<class pair_iterator> 00027 class first_of_pair_iterator : public pair_iterator { 00028 public: 00029 typedef TYPENAME pair_iterator::value_type::first_type value_type; 00030 00031 first_of_pair_iterator() { } 00032 first_of_pair_iterator(const pair_iterator &init) : pair_iterator(init) { } 00033 first_of_pair_iterator(const first_of_pair_iterator<pair_iterator> ©) : pair_iterator(copy) { } 00034 00035 value_type operator *() { 00036 return pair_iterator::operator *().first; 00037 } 00038 }; 00039 00040 //////////////////////////////////////////////////////////////////// 00041 // Class : second_of_pair_iterator 00042 // Description : This is an iterator adaptor that converts any 00043 // iterator that returns a pair (e.g. a map iterator) 00044 // into one that returns just the second component of 00045 // that pair. 00046 //////////////////////////////////////////////////////////////////// 00047 template<class pair_iterator> 00048 class second_of_pair_iterator : public pair_iterator { 00049 public: 00050 typedef TYPENAME pair_iterator::value_type::second_type value_type; 00051 00052 second_of_pair_iterator() { } 00053 second_of_pair_iterator(const pair_iterator &init) : pair_iterator(init) { } 00054 second_of_pair_iterator(const second_of_pair_iterator<pair_iterator> ©) : pair_iterator(copy) { } 00055 00056 value_type operator *() { 00057 return pair_iterator::operator *().second; 00058 } 00059 }; 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Class : typecast_iterator 00063 // Description : This is an iterator adaptor that explicitly typecasts 00064 // each value returned by the base iterator to the 00065 // indicated type. 00066 //////////////////////////////////////////////////////////////////// 00067 template<class base_iterator, class new_type> 00068 class typecast_iterator : public base_iterator { 00069 public: 00070 typedef new_type value_type; 00071 00072 typecast_iterator() { } 00073 typecast_iterator(const base_iterator &init) : base_iterator(init) { } 00074 typecast_iterator(const typecast_iterator<base_iterator, new_type> ©) : base_iterator(copy) { } 00075 00076 value_type operator *() { 00077 return (new_type)base_iterator::operator *(); 00078 } 00079 }; 00080 00081 #endif