scnlib  0.1.2
FormattedinputformodernC++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
types.h
Go to the documentation of this file.
1 // Copyright 2017-2019 Elias Kosunen
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // This file is a part of scnlib:
16 // https://github.com/eliaskosunen/scnlib
17 
18 #ifndef SCN_DETAIL_TYPES_H
19 #define SCN_DETAIL_TYPES_H
20 
21 #include "visitor.h"
22 
24 
25 namespace scn {
27 
28  template <typename Stream,
29  typename Traits,
30  typename Allocator,
31  typename CharT = typename Stream::char_type>
32  error getline(Stream& s,
33  std::basic_string<CharT, Traits, Allocator>& str,
34  CharT until)
35  {
36  str.clear();
37  auto res = read_into_if(s, std::back_inserter(str),
38  predicates::until<CharT>{until}, true);
39  if (!res) {
40  return res.error();
41  }
42  return {};
43  }
44  template <typename Stream,
45  typename Traits,
46  typename Allocator,
47  typename CharT = typename Stream::char_type>
48  error getline(Stream& s, std::basic_string<CharT, Traits, Allocator>& str)
49  {
50  return getline(s, str, detail::default_widen<CharT>::widen('\n'));
51  }
52 
53  namespace detail {
54  template <typename CharT>
55  struct ignore_iterator {
56  using value_type = CharT;
57  using pointer = value_type*;
59  using difference_type = std::ptrdiff_t;
60  using iterator_category = std::input_iterator_tag;
61 
62  ignore_iterator() = default;
63  explicit ignore_iterator(difference_type n) : i(n) {}
64 
66  {
67  static CharT c(0);
68  return c;
69  }
71  {
72  return &operator*();
73  }
74 
76  {
77  ++i;
78  return *this;
79  }
81  {
82  auto tmp(*this);
83  operator++();
84  return tmp;
85  }
86 
87  void swap(ignore_iterator& other)
88  {
89  using std::swap;
90  swap(i, other.i);
91  }
92 
94  {
95  return i == other.i;
96  }
98  {
99  return !(*this == other);
100  }
101 
103  {
105  }
106 
108  };
109 
110  template <typename CharT>
112  {
113  a.swap(b);
114  }
115  } // namespace detail
116 
117  template <typename Stream, typename CharT = typename Stream::char_type>
118  auto ignore_all(Stream& s) ->
119  typename std::enable_if<!is_sized_stream<Stream>::value, error>::type
120  {
123  if (!res && res.error() != error::end_of_stream) {
124  return res.error();
125  }
126  return {};
127  }
128  template <typename Stream, typename CharT = typename Stream::char_type>
129  auto ignore_all(Stream& s) ->
130  typename std::enable_if<is_sized_stream<Stream>::value, error>::type
131  {
132  s.skip_all();
133  return {};
134  }
135 
136  template <typename Stream>
137  error ignore_until(Stream& s, typename Stream::char_type until)
138  {
139  auto res = read_into_if(
142  if (!res) {
143  return res.error();
144  }
145  return {};
146  }
147 
148  template <typename Stream, typename CharT = typename Stream::char_type>
149  auto ignore_n(Stream& s, std::ptrdiff_t count) ->
150  typename std::enable_if<!is_sized_stream<Stream>::value, error>::type
151  {
155  if (!res) {
156  return res.error();
157  }
158  return {};
159  }
160  template <typename Stream, typename CharT = typename Stream::char_type>
161  auto ignore_n(Stream& s, std::ptrdiff_t count) ->
162  typename std::enable_if<is_sized_stream<Stream>::value, error>::type
163  {
164  s.skip(static_cast<size_t>(count));
165  return {};
166  }
167 
168  template <typename Stream>
170  std::ptrdiff_t count,
171  typename Stream::char_type until)
172  {
173  auto res = read_into_if(
177  if (!res) {
178  return res.error();
179  }
180  return {};
181  }
182 
183  template <typename Stream,
184  typename std::enable_if<is_sized_stream<Stream>::value>::type* =
185  nullptr>
187  {
188  return s.read_char();
189  }
190 
191  template <typename T, typename Stream>
193  {
194  using context_type = basic_empty_context<Stream>;
195  using char_type = typename Stream::char_type;
196 
197  auto args = make_args<context_type>();
198  auto ctx = context_type(s, 1, args);
199 
200  auto ret = skip_stream_whitespace(ctx);
201  if (!ret) {
202  return ret;
203  }
204 
205  T val{};
207  ret = sc.scan(val, ctx);
208  if (!ret) {
209  return ret;
210  }
211  return val;
212  }
213 
215 } // namespace scn
216 
218 
219 #endif // SCN_DETAIL_TYPES_H
bool operator!=(ignore_iterator &other)
Definition: types.h:97
void swap(ignore_iterator &other)
Definition: types.h:87
SCN_CONSTEXPR T max(T a, T b) noexcept
Definition: util.h:121
#define SCN_END_NAMESPACE
Definition: config.h:376
value_type & reference
Definition: types.h:58
bool operator==(ignore_iterator &other)
Definition: types.h:93
value_type * pointer
Definition: types.h:57
ignore_iterator & operator++()
Definition: types.h:75
static ignore_iterator max()
Definition: types.h:102
SCN_BEGIN_NAMESPACE error getline(Stream &s, std::basic_string< CharT, Traits, Allocator > &str, CharT until)
Definition: types.h:32
pointer operator->() const
Definition: types.h:70
reference operator*() const
Definition: types.h:65
expected< typename Stream::char_type > getchar(Stream &s)
Definition: types.h:186
ignore_iterator(difference_type n)
Definition: types.h:63
void swap(ignore_iterator< CharT > &a, ignore_iterator< CharT > &b)
Definition: types.h:111
auto ignore_n(Stream &s, std::ptrdiff_t count) -> typename std::enable_if<!is_sized_stream< Stream >::value, error >::type
Definition: types.h:149
expected-like type.
Definition: result.h:186
#define SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
Definition: config.h:130
SCN_CONSTEXPR14 void swap(small_vector< T, N > &l, small_vector< T, N > &r) noexcept(noexcept(l.swap(r)))
Definition: small_vector.h:851
std::input_iterator_tag iterator_category
Definition: types.h:60
result< size_t > read_into_if(Stream &s, Iterator it, Predicate &&p, bool keep_final=false)
Definition: visitor.h:185
Error class.
Definition: result.h:32
#define SCN_BEGIN_NAMESPACE
Definition: config.h:375
expected< T > get_value(Stream &s)
Definition: types.h:192
std::ptrdiff_t difference_type
Definition: types.h:59
error ignore_n_until(Stream &s, std::ptrdiff_t count, typename Stream::char_type until)
Definition: types.h:169
#define SCN_CLANG_PUSH_IGNORE_UNDEFINED_TEMPLATE
Definition: config.h:129
auto ignore_all(Stream &s) -> typename std::enable_if<!is_sized_stream< Stream >::value, error >::type
Definition: types.h:118
error ignore_until(Stream &s, typename Stream::char_type until)
Definition: types.h:137
SCN_CLANG_POP error skip_stream_whitespace(Context &ctx) noexcept
Definition: visitor.h:1271
difference_type i
Definition: types.h:107
ignore_iterator operator++(int)
Definition: types.h:80