The Battle for Wesnoth  1.15.0-dev
ucs4_iterator_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include <iterator> //input_iterator_tag
17 #include <utility> //pair
18 #include <cstddef> //ptrdiff_t
19 #include <cassert> //assert
20 
21 namespace ucs4
22 {
23  template<typename string_type, typename update_implementation>
25  {
26  public:
27  typedef std::input_iterator_tag iterator_category;
28  typedef char32_t value_type;
29  typedef ptrdiff_t difference_type;
30  typedef char32_t* pointer;
31  typedef char32_t& reference;
32 
33  iterator_base(const string_type& str)
34  : current_char(0)
35  , string_end(str.end())
36  , current_substr(std::make_pair(str.begin(), str.begin()))
37  {
38  update();
39  }
40 
41  iterator_base(typename string_type::const_iterator const& begin, typename string_type::const_iterator const& end)
42  : current_char(0)
43  , string_end(end)
44  , current_substr(std::make_pair(begin, begin))
45  {
46  update();
47  }
48 
49  static iterator_base begin(const string_type& str)
50  {
51  return iterator_base(str.begin(), str.end());
52  }
53 
54  static iterator_base end(const string_type& str)
55  {
56  return iterator_base(str.end(), str.end());
57  }
58 
59  bool operator==(const iterator_base& a) const
60  {
61  return current_substr.first == a.current_substr.first;
62  }
63 
64  bool operator!=(const iterator_base& a) const
65  {
66  return ! (*this == a);
67  }
68 
70  {
71  current_substr.first = current_substr.second;
72  update();
73  return *this;
74  }
75 
76  char32_t operator*() const
77  {
78  return current_char;
79  }
80 
81  bool next_is_end() const
82  {
83  if(current_substr.second == string_end)
84  return true;
85  return false;
86  }
87 
88  const std::pair<typename string_type::const_iterator, typename string_type::const_iterator>& substr() const
89  {
90  return current_substr;
91  }
92  private:
93  void update()
94  {
95  assert(current_substr.first == current_substr.second);
96  if(current_substr.first == string_end)
97  return;
99  }
100 
101  char32_t current_char;
102  typename string_type::const_iterator string_end;
103  std::pair<typename string_type::const_iterator, typename string_type::const_iterator> current_substr;
104  };
105 
106 }
bool operator==(const iterator_base &a) const
static iterator_base begin(const string_type &str)
iterator_base(typename string_type::const_iterator const &begin, typename string_type::const_iterator const &end)
char32_t operator*() const
#define a
STL namespace.
iterator_base & operator++()
std::input_iterator_tag iterator_category
iterator_base(const string_type &str)
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:612
const std::pair< typename string_type::const_iterator, typename string_type::const_iterator > & substr() const
bool operator!=(const iterator_base &a) const
static iterator_base end(const string_type &str)
std::pair< typename string_type::const_iterator, typename string_type::const_iterator > current_substr
string_type::const_iterator string_end