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
46 iterator begin() { return _begin; }
47 const_iterator begin() const { return _begin; }
48 iterator end() { return _end; }
49 const_iterator end() const { return _end; }
50
51 const_iterator cbegin() const { return _begin; }
52 const_iterator cend() const { return _end; }
53
54 reverse_iterator rbegin() { return reverse_iterator(end()); };
55 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
56 reverse_iterator rend() { return reverse_iterator(begin()); }
57 const_reverse_iterator rend() const { return reverse_iterator(begin()); }
58
59 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
60 const_reverse_iterator crend() const { return reverse_iterator(begin()); }
61
62private:
63 iterator _begin;
64 iterator _end;
65};
66
67/// Something that owns an object. Used in basic_owning_view to move the value somewhere fixed before getting the
68/// itertors and constructing the base.
69template <typename T>
70class basic_owner
71{
72public:
73 explicit basic_owner(T&& x) :
74 _value(std::move(x))
75 { }
76
77protected:
78 T _value;
79};
80
81/// A form of basic_view that owns the object it is iterating over.
82template <typename TContainer,
83 typename TIterator,
84 typename TConstIterator = TIterator
85 >
86class basic_owning_view :
87 private basic_owner<TContainer>,
88 public basic_view<TIterator, TConstIterator>
89{
90 using basic_owner<TContainer>::_value;
91
92public:
93 template <typename FBegin, typename FEnd>
94 basic_owning_view(TContainer&& container, FBegin begin, FEnd end) :
95 basic_owner<TContainer>(std::move(container)),
96 basic_view<TIterator, TConstIterator>(begin(_value), end(_value))
97 { }
98};
99
100}
Copyright (c) 2014-2020 by Travis Gockel.
STL namespace.