Panda3D
iterator_types.h
1 // Filename: iterator_types.h
2 // Created by: drose (10Feb99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef ITERATOR_TYPES_H
16 #define ITERATOR_TYPES_H
17 
18 
19 ////////////////////////////////////////////////////////////////////
20 // Class : first_of_pair_iterator
21 // Description : This is an iterator adaptor that converts any
22 // iterator that returns a pair (e.g. a map iterator)
23 // into one that returns just the first component of
24 // that pair.
25 ////////////////////////////////////////////////////////////////////
26 template<class pair_iterator>
27 class first_of_pair_iterator : public pair_iterator {
28 public:
29  typedef TYPENAME pair_iterator::value_type::first_type value_type;
30 
32  first_of_pair_iterator(const pair_iterator &init) : pair_iterator(init) { }
33  first_of_pair_iterator(const first_of_pair_iterator<pair_iterator> &copy) : pair_iterator(copy) { }
34 
35  value_type operator *() {
36  return pair_iterator::operator *().first;
37  }
38 };
39 
40 ////////////////////////////////////////////////////////////////////
41 // Class : second_of_pair_iterator
42 // Description : This is an iterator adaptor that converts any
43 // iterator that returns a pair (e.g. a map iterator)
44 // into one that returns just the second component of
45 // that pair.
46 ////////////////////////////////////////////////////////////////////
47 template<class pair_iterator>
48 class second_of_pair_iterator : public pair_iterator {
49 public:
50  typedef TYPENAME pair_iterator::value_type::second_type value_type;
51 
53  second_of_pair_iterator(const pair_iterator &init) : pair_iterator(init) { }
54  second_of_pair_iterator(const second_of_pair_iterator<pair_iterator> &copy) : pair_iterator(copy) { }
55 
56  value_type operator *() {
57  return pair_iterator::operator *().second;
58  }
59 };
60 
61 ////////////////////////////////////////////////////////////////////
62 // Class : typecast_iterator
63 // Description : This is an iterator adaptor that explicitly typecasts
64 // each value returned by the base iterator to the
65 // indicated type.
66 ////////////////////////////////////////////////////////////////////
67 template<class base_iterator, class new_type>
68 class typecast_iterator : public base_iterator {
69 public:
70  typedef new_type value_type;
71 
72  typecast_iterator() { }
73  typecast_iterator(const base_iterator &init) : base_iterator(init) { }
74  typecast_iterator(const typecast_iterator<base_iterator, new_type> &copy) : base_iterator(copy) { }
75 
76  value_type operator *() {
77  return (new_type)base_iterator::operator *();
78  }
79 };
80 
81 #endif
This is an iterator adaptor that converts any iterator that returns a pair (e.g.
This is an iterator adaptor that converts any iterator that returns a pair (e.g.
This is an iterator adaptor that explicitly typecasts each value returned by the base iterator to the...