Libosmium  2.17.0
Fast and flexible C++ library for working with OpenStreetMap data
Loading...
Searching...
No Matches
collection.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_MEMORY_COLLECTION_HPP
2#define OSMIUM_MEMORY_COLLECTION_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
37
38#include <iosfwd>
39#include <iterator>
40#include <type_traits>
41
42namespace osmium {
43
44 namespace memory {
45
46 template <typename TMember>
48
49 // This data_type is either 'unsigned char*' or 'const unsigned
50 // char*' depending on whether TMember is const. This allows this
51 // class to be used as an iterator and as a const_iterator.
52 using data_type = typename std::conditional<std::is_const<TMember>::value, const unsigned char*, unsigned char*>::type;
53
55
56 public:
57
58 using iterator_category = std::forward_iterator_tag;
59 using value_type = TMember;
60 using difference_type = std::ptrdiff_t;
63
64 CollectionIterator() noexcept :
65 m_data(nullptr) {
66 }
67
68 explicit CollectionIterator(data_type data) noexcept :
69 m_data(data) {
70 }
71
73 m_data = reinterpret_cast<TMember*>(m_data)->next();
74 return *static_cast<CollectionIterator<TMember>*>(this);
75 }
76
79 operator++();
80 return tmp;
81 }
82
83 bool operator==(const CollectionIterator<TMember>& rhs) const noexcept {
84 return m_data == rhs.m_data;
85 }
86
87 bool operator!=(const CollectionIterator<TMember>& rhs) const noexcept {
88 return !(*this == rhs);
89 }
90
91 unsigned char* data() const noexcept {
92 return m_data;
93 }
94
95 TMember& operator*() const noexcept {
96 return *reinterpret_cast<TMember*>(m_data);
97 }
98
99 TMember* operator->() const noexcept {
100 return reinterpret_cast<TMember*>(m_data);
101 }
102
103 template <typename TChar, typename TTraits>
104 void print(std::basic_ostream<TChar, TTraits>& out) const {
105 out << static_cast<const void*>(m_data);
106 }
107
108 }; // class CollectionIterator
109
110 template <typename TChar, typename TTraits, typename TMember>
111 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const CollectionIterator<TMember>& iter) {
112 iter.print(out);
113 return out;
114 }
115
116 template <typename TMember, osmium::item_type TCollectionItemType>
117 class Collection : public Item {
118
119 public:
120
121 using value_type = TMember;
122 using reference = TMember&;
123 using const_reference = const TMember&;
126 using size_type = std::size_t;
127
128 static constexpr osmium::item_type itemtype = TCollectionItemType;
129
130 constexpr static bool is_compatible_to(const osmium::item_type t) noexcept {
131 return t == itemtype;
132 }
133
134 Collection() noexcept :
135 Item(sizeof(Collection<TMember, TCollectionItemType>), TCollectionItemType) {
136 }
137
143 bool empty() const noexcept {
145 }
146
152 size_type size() const noexcept {
153 return static_cast<size_type>(std::distance(begin(), end()));
154 }
155
156 iterator begin() noexcept {
157 return iterator{data() + sizeof(Collection<TMember, TCollectionItemType>)};
158 }
159
160 iterator end() noexcept {
161 return iterator{data() + byte_size()};
162 }
163
164 const_iterator cbegin() const noexcept {
166 }
167
168 const_iterator cend() const noexcept {
169 return const_iterator{data() + byte_size()};
170 }
171
172 const_iterator begin() const noexcept {
173 return cbegin();
174 }
175
176 const_iterator end() const noexcept {
177 return cend();
178 }
179
180 }; // class Collection
181
182 } // namespace memory
183
184} // namespace osmium
185
186#endif // OSMIUM_MEMORY_COLLECTION_HPP
Definition: collection.hpp:47
bool operator==(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:83
CollectionIterator(data_type data) noexcept
Definition: collection.hpp:68
typename std::conditional< std::is_const< TMember >::value, const unsigned char *, unsigned char * >::type data_type
Definition: collection.hpp:52
value_type & reference
Definition: collection.hpp:62
TMember & operator*() const noexcept
Definition: collection.hpp:95
unsigned char * data() const noexcept
Definition: collection.hpp:91
std::forward_iterator_tag iterator_category
Definition: collection.hpp:58
data_type m_data
Definition: collection.hpp:54
value_type * pointer
Definition: collection.hpp:61
CollectionIterator< TMember > & operator++()
Definition: collection.hpp:72
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition: collection.hpp:104
CollectionIterator() noexcept
Definition: collection.hpp:64
TMember * operator->() const noexcept
Definition: collection.hpp:99
TMember value_type
Definition: collection.hpp:59
bool operator!=(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:87
CollectionIterator< TMember > operator++(int)
Definition: collection.hpp:77
std::ptrdiff_t difference_type
Definition: collection.hpp:60
Definition: collection.hpp:117
bool empty() const noexcept
Definition: collection.hpp:143
TMember & reference
Definition: collection.hpp:122
std::size_t size_type
Definition: collection.hpp:126
const_iterator end() const noexcept
Definition: collection.hpp:176
static constexpr bool is_compatible_to(const osmium::item_type t) noexcept
Definition: collection.hpp:130
static constexpr osmium::item_type itemtype
Definition: collection.hpp:128
TMember value_type
Definition: collection.hpp:121
const_iterator begin() const noexcept
Definition: collection.hpp:172
iterator end() noexcept
Definition: collection.hpp:160
size_type size() const noexcept
Definition: collection.hpp:152
const_iterator cend() const noexcept
Definition: collection.hpp:168
const_iterator cbegin() const noexcept
Definition: collection.hpp:164
iterator begin() noexcept
Definition: collection.hpp:156
Collection() noexcept
Definition: collection.hpp:134
const TMember & const_reference
Definition: collection.hpp:123
Definition: item.hpp:105
item_size_type byte_size() const noexcept
Definition: item.hpp:163
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const CollectionIterator< TMember > &iter)
Definition: collection.hpp:111
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
item_type
Definition: item_type.hpp:43