JSON Voorhees
Killer JSON for C++
basic_view.hpp
Go to the documentation of this file.
1 /** \file jsonv/detail/basic_view.hpp
2  *
3  * Copyright (c) 2014 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 **/
11 #ifndef __JSONV_DETAIL_BASIC_VIEW_HPP_INCLUDED__
12 #define __JSONV_DETAIL_BASIC_VIEW_HPP_INCLUDED__
13 
14 #include <jsonv/config.hpp>
15 
16 #include <iterator>
17 #include <utility>
18 
19 namespace jsonv
20 {
21 namespace detail
22 {
23 
24 /** A view template used for array and object views of a \c value. This class allows traversing a \c value with a range
25  * based for loop.
26  *
27  * \note
28  * A view does nothing to preserve the lifetime of the underlying container, nor does it remain valid if the container
29  * is modified.
30 **/
31 template <typename TIterator,
32  typename TConstIterator = TIterator
33  >
35 {
36 public:
37  typedef TIterator iterator;
38  typedef TConstIterator const_iterator;
39  typedef std::reverse_iterator<iterator> reverse_iterator;
40  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
41  typedef typename std::iterator_traits<iterator>::value_type value_type;
42  typedef typename std::iterator_traits<iterator>::reference reference;
43  typedef typename std::iterator_traits<iterator>::pointer pointer;
44 
45 public:
46  basic_view(iterator begin_, iterator end_) :
47  _begin(begin_),
48  _end(end_)
49  { }
50 
51  iterator begin() { return _begin; }
52  const_iterator begin() const { return _begin; }
53  iterator end() { return _end; }
54  const_iterator end() const { return _end; }
55 
56  const_iterator cbegin() const { return _begin; }
57  const_iterator cend() const { return _end; }
58 
59  reverse_iterator rbegin() { return reverse_iterator(end()); };
60  const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
61  reverse_iterator rend() { return reverse_iterator(begin()); }
62  const_reverse_iterator rend() const { return reverse_iterator(begin()); }
63 
64  const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
65  const_reverse_iterator crend() const { return reverse_iterator(begin()); }
66 
67 private:
68  iterator _begin;
69  iterator _end;
70 };
71 
72 /** Something that owns an object. Used in basic_owning_view to move the value somewhere fixed before getting the
73  * itertors and constructing the base.
74 **/
75 template <typename T>
77 {
78 public:
79  explicit basic_owner(T&& x) :
80  _value(std::move(x))
81  { }
82 
83 protected:
84  T _value;
85 };
86 
87 /** A form of basic_view that owns the object it is iterating over. **/
88 template <typename TContainer,
89  typename TIterator,
90  typename TConstIterator = TIterator
91  >
93  private basic_owner<TContainer>,
94  public basic_view<TIterator, TConstIterator>
95 {
97 
98 public:
99  template <typename FBegin, typename FEnd>
100  basic_owning_view(TContainer&& container, FBegin begin, FEnd end) :
101  basic_owner<TContainer>(std::move(container)),
102  basic_view<TIterator, TConstIterator>(begin(_value), end(_value))
103  { }
104 };
105 
106 }
107 }
108 
109 #endif/*__JSONV_DETAIL_BASIC_VIEW_HPP_INCLUDED__*/
Copyright (c) 2014-2019 by Travis Gockel.
A form of basic_view that owns the object it is iterating over.
Definition: basic_view.hpp:92
Something that owns an object.
Definition: basic_view.hpp:76
A view template used for array and object views of a value.
Definition: basic_view.hpp:34