All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
singleton_iterator.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_SINGLETON_ITERATOR_HPP
16 #define PAAL_SINGLETON_ITERATOR_HPP
17 
18 #include <boost/iterator/iterator_facade.hpp>
19 #include <boost/optional/optional.hpp>
20 #include <boost/range/iterator_range.hpp>
21 
22 namespace paal {
23 namespace utils {
24 
29 template <typename Elem>
30 class singleton_iterator : public boost::iterator_facade<
31  singleton_iterator<Elem>, typename std::decay<Elem>::type,
32  boost::forward_traversal_tag, Elem> {
33  template <typename E>
34  friend singleton_iterator<E> make_singleton_iterator_begin(E&&);
35  template <typename E>
37 
44  singleton_iterator(Elem elem) : m_elem(elem) {}
45 
46  friend class boost::iterator_core_access;
47 
48  void increment() { m_elem = boost::none; }
49 
50  bool equal(const singleton_iterator &other) const {
51  // This doesn't need Elem to have (==) operator.
52  return (!m_elem && !other.m_elem) || (m_elem && other.m_elem);
53  }
54 
55  Elem dereference() const { return m_elem.get(); }
56 
57  boost::optional<Elem> m_elem;
58 
59  public:
66 };
67 
76 template <typename Elem>
78  return singleton_iterator<Elem>(std::forward<Elem>(elem));
79 }
80 
88 template <typename Elem>
90  return singleton_iterator<Elem>();
91 }
92 
101 template <typename Elem>
102 auto make_singleton_range(Elem&& elem)
103 -> decltype(boost::make_iterator_range(
104  make_singleton_iterator_begin(std::forward<Elem>(elem)),
105  make_singleton_iterator_end<Elem>()
106 )) {
107  return boost::make_iterator_range(
108  make_singleton_iterator_begin(std::forward<Elem>(elem)),
109  make_singleton_iterator_end<Elem>()
110  );
111 }
112 
113 }
114 }
115 
116 #endif // PAAL_SINGLETON_ITERATOR_HPP
auto make_singleton_range(Elem &&elem) -> decltype(boost::make_iterator_range(make_singleton_iterator_begin(std::forward< Elem >(elem)), make_singleton_iterator_end< Elem >()))
function to create a singleton range
friend singleton_iterator< E > make_singleton_iterator_end()
function to create end of singleton_iterator
singleton_iterator()
public constructor to satisfy the concept requirements. However one should use make_singleton_iterato...
singleton_iterator< Elem > make_singleton_iterator_end()
function to create end of singleton_iterator
Iterator to range containing single element.
singleton_iterator< Elem > make_singleton_iterator_begin(Elem &&elem)
function to create begin of singleton_iterator