OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
pointer_iterator.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_MOL_IMPL_POINTER_ITERATOR_HH
20 #define OST_MOL_IMPL_POINTER_ITERATOR_HH
21 
22 #include <vector>
23 
24 namespace ost { namespace mol { namespace impl {
25 
26 template <typename T>
27 class pointer_it : public std::iterator<std::forward_iterator_tag, T>{
28 public:
29  pointer_it(T* s): s_(s) { }
30 
32  {
33  ++s_;
34  return *this;
35  }
37  {
38  s_+=rhs;
39  return *this;
40  }
41 
42  bool operator==(const pointer_it<T>& rhs) const
43  {
44  return rhs.s_==s_;
45  }
46 
47  bool operator!=(const pointer_it<T>&rhs) const
48  {
49  return !(*this==rhs);
50  }
51 
52  T& operator*()
53  {
54  return *s_;
55  }
56 
58  {
59  return s_;
60  }
61 private:
62  T* s_;
63 };
64 
65 template <typename T>
66 inline pointer_it<T> begin(const std::vector<T>& values)
67 {
68  return pointer_it<T>(values.empty() ? NULL : const_cast<T*>(&values.front()));
69 }
70 
71 template <typename T>
72 inline pointer_it<T> end(const std::vector<T>& values)
73 {
74  return pointer_it<T>(values.empty() ? NULL : const_cast<T*>(&values.back()+1));
75 }
76 
77 }}}
78 
79 #endif