scnlib  0.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
context.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_CONTEXT_H
19 #define SCN_DETAIL_CONTEXT_H
20 
21 #include "args.h"
22 #include "locale.h"
23 #include "range.h"
24 
25 namespace scn {
27 
28  template <typename WrappedRange,
29  typename LocaleRef =
30  basic_default_locale_ref<typename WrappedRange::char_type>>
31  class basic_context : public LocaleRef {
32  public:
33  using range_type = WrappedRange;
34  using iterator = typename range_type::iterator;
35  using sentinel = typename range_type::sentinel;
36  using char_type = typename range_type::char_type;
37  using locale_type = LocaleRef;
40 
41  template <typename T>
43  template <typename... Args>
45 
46  template <typename R>
47  basic_context(R&& r) : LocaleRef{}, m_range(std::forward<R>(r))
48  {
49  }
50  template <typename R>
51  basic_context(R&& r, LocaleRef&& loc)
52  : LocaleRef(std::move(loc)), m_range(std::forward<R>(r))
53  {
54  }
55 
57  {
58  return m_range.begin();
59  }
60  const sentinel& end() const
61  {
62  return m_range.end();
63  }
64 
66  {
67  return m_range;
68  }
69  const range_type& range() const
70  {
71  return m_range;
72  }
73 
74  LocaleRef& locale() noexcept
75  {
76  return static_cast<LocaleRef&>(*this);
77  }
78  const LocaleRef& locale() const noexcept
79  {
80  return static_cast<const LocaleRef&>(*this);
81  }
82 
83  private:
84  range_type m_range;
85  };
86 
87  template <typename Context>
88  auto get_arg(const basic_args<Context>& args, std::ptrdiff_t id)
90  {
91  auto a = args.get(id);
92  if (!a) {
94  "Argument id out of range");
95  }
96  return a;
97  }
98  template <typename Context, typename ParseCtx>
99  auto get_arg(const basic_args<Context>& args,
100  ParseCtx& pctx,
101  std::ptrdiff_t id) -> expected<basic_arg<Context>>
102  {
103  return pctx.check_arg_id(id) ? get_arg(args, id)
105  "Argument id out of range");
106  }
107  template <typename Context, typename ParseCtx>
109  ParseCtx&,
112  {
113  return error(error::invalid_format_string, "Argument id out of range");
114  }
115 
116  template <typename Context, typename ParseCtx>
117  auto next_arg(const basic_args<Context>& args, ParseCtx& pctx)
119  {
120  return get_arg(args, pctx.next_arg_id());
121  }
122 
124 } // namespace scn
125 
126 #endif // SCN_DETAIL_CONTEXT_H
#define SCN_END_NAMESPACE
Definition: config.h:401
WrappedRange range_type
Definition: context.h:33
typename range_type::char_type char_type
Definition: context.h:36
const LocaleRef & locale() const noexcept
Definition: context.h:78
range_type & range()
Definition: context.h:65
const sentinel & end() const
Definition: context.h:60
Format string was invalid.
Definition: result.h:41
basic_context(R &&r)
Definition: context.h:47
expected-like type.
Definition: result.h:180
typename range_type::iterator iterator
Definition: context.h:34
basic_context(R &&r, LocaleRef &&loc)
Definition: context.h:51
#define SCN_NODISCARD
Definition: config.h:259
LocaleRef locale_type
Definition: context.h:37
Error class.
Definition: result.h:32
typename range_type::sentinel sentinel
Definition: context.h:35
#define SCN_BEGIN_NAMESPACE
Definition: config.h:400
auto get_arg(const basic_args< Context > &args, std::ptrdiff_t id) -> expected< basic_arg< Context >>
Definition: context.h:88
const range_type & range() const
Definition: context.h:69
SCN_NODISCARD iterator & begin()
Definition: context.h:56
LocaleRef & locale() noexcept
Definition: context.h:74
A view over a (sub)string.
Definition: string_view.h:60
Type-erased scanning argument.
Definition: args.h:30
auto next_arg(const basic_args< Context > &args, ParseCtx &pctx) -> expected< basic_arg< Context >>
Definition: context.h:117