JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
basic_view.hpp
Go to the documentation of this file.
1/// \file jsonv/detail/basic_view.hpp
2///
3/// Copyright (c) 2014-2020 by Travis Gockel. All rights reserved.
4///
5/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
6/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
7/// version.
8///
9/// \author Travis Gockel (travis@gockelhut.com)
10#pragma once
11
12#include <jsonv/config.hpp>
13
14#include <iterator>
15#include <utility>
16
17namespace jsonv::detail
18{
19
20/// A view template used for array and object views of a \c value. This class allows traversing a \c value with a range
21/// based for loop.
22///
23/// \note
24/// A view does nothing to preserve the lifetime of the underlying container, nor does it remain valid if the container
25/// is modified.
26template <typename TIterator,
27 typename TConstIterator = TIterator
28 >
29class basic_view
30{
31public:
32 typedef TIterator iterator;
33 typedef TConstIterator const_iterator;
34 typedef std::reverse_iterator<iterator> reverse_iterator;
35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
36 typedef typename std::iterator_traits<iterator>::value_type value_type;
37 typedef typename std::iterator_traits<iterator>::reference reference;
38 typedef typename std::iterator_traits<iterator>::pointer pointer;
39
40public:
41 basic_view(iterator begin_, iterator end_) :
42 _begin(begin_),
43 _end(end_)
44 { }
45
47 iterator begin() { return _begin; }
49 const_iterator begin() const { return _begin; }
51 iterator end() { return _end; }
53 const_iterator end() const { return _end; }
54
56 const_iterator cbegin() const { return _begin; }
58 const_iterator cend() const { return _end; }
59
61 reverse_iterator rbegin() { return reverse_iterator(end()); };
63 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
65 reverse_iterator rend() { return reverse_iterator(begin()); }
67 const_reverse_iterator rend() const { return reverse_iterator(begin()); }
68
70 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
72 const_reverse_iterator crend() const { return reverse_iterator(begin()); }
73
74private:
75 iterator _begin;
76 iterator _end;
77};
78
79/// Something that owns an object. Used in basic_owning_view to move the value somewhere fixed before getting the
80/// itertors and constructing the base.
81template <typename T>
82class basic_owner
83{
84public:
85 explicit basic_owner(T&& x) :
86 _value(std::move(x))
87 { }
88
89protected:
90 T _value;
91};
92
93/// A form of basic_view that owns the object it is iterating over.
94template <typename TContainer,
95 typename TIterator,
96 typename TConstIterator = TIterator
97 >
98class basic_owning_view :
99 private basic_owner<TContainer>,
100 public basic_view<TIterator, TConstIterator>
101{
102 using basic_owner<TContainer>::_value;
103
104public:
105 template <typename FBegin, typename FEnd>
106 basic_owning_view(TContainer&& container, FBegin begin, FEnd end) :
107 basic_owner<TContainer>(std::move(container)),
108 basic_view<TIterator, TConstIterator>(begin(_value), end(_value))
109 { }
110};
111
112}
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:121
STL namespace.