Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
collection.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_MEMORY_COLLECTION_HPP
2 #define OSMIUM_MEMORY_COLLECTION_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <iterator>
37 #include <iosfwd>
38 #include <type_traits>
39 
40 #include <osmium/memory/item.hpp>
41 
42 namespace osmium {
43 
44  namespace memory {
45 
46  template <typename TMember>
48 
49  // This data_type is either 'unsigned char*' or 'const unsigned char*' depending
50  // on whether TMember is const. This allows this class to be used as an iterator and
51  // 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;
61  using pointer = value_type*;
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 
78  CollectionIterator<TMember> tmp(*this);
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 m_data != rhs.m_data;
89  }
90 
91  unsigned char* data() const noexcept {
92  return m_data;
93  }
94 
95  TMember& operator*() const {
96  return *reinterpret_cast<TMember*>(m_data);
97  }
98 
99  TMember* operator->() const {
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 
123  using value_type = TMember;
124 
125  static constexpr osmium::item_type itemtype = TCollectionItemType;
126 
128  Item(sizeof(Collection<TMember, TCollectionItemType>), TCollectionItemType) {
129  }
130 
131  bool empty() const {
132  return sizeof(Collection<TMember, TCollectionItemType>) == byte_size();
133  }
134 
137  }
138 
140  return iterator(data() + byte_size());
141  }
142 
145  }
146 
148  return const_iterator(data() + byte_size());
149  }
150 
152  return cbegin();
153  }
154 
155  const_iterator end() const {
156  return cend();
157  }
158 
159  }; // class Collection
160 
161  } // namespace memory
162 
163 } // namespace osmium
164 
165 #endif // OSMIUM_MEMORY_COLLECTION_HPP
const_iterator end() const
Definition: collection.hpp:155
iterator end()
Definition: collection.hpp:139
Definition: collection.hpp:47
CollectionIterator< TMember > & operator++()
Definition: collection.hpp:72
type
Definition: entity_bits.hpp:63
value_type & reference
Definition: collection.hpp:62
std::ptrdiff_t difference_type
Definition: collection.hpp:60
item_type
Definition: item_type.hpp:43
CollectionIterator< TMember > operator++(int)
Definition: collection.hpp:77
std::forward_iterator_tag iterator_category
Definition: collection.hpp:58
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition: collection.hpp:104
iterator begin()
Definition: collection.hpp:135
const_iterator cbegin() const
Definition: collection.hpp:143
CollectionIterator() noexcept
Definition: collection.hpp:64
bool empty() const
Definition: collection.hpp:131
bool operator!=(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:87
Definition: item.hpp:105
Definition: relation.hpp:54
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: collection.hpp:117
typename std::conditional< std::is_const< TMember >::value, const unsigned char *, unsigned char * >::type data_type
Definition: collection.hpp:52
const_iterator cend() const
Definition: collection.hpp:147
unsigned char * data() const noexcept
Definition: collection.hpp:91
TMember & operator*() const
Definition: collection.hpp:95
const_iterator begin() const
Definition: collection.hpp:151
Collection()
Definition: collection.hpp:127
TMember value_type
Definition: collection.hpp:59
data_type m_data
Definition: collection.hpp:54
value_type * pointer
Definition: collection.hpp:61
CollectionIterator(data_type data) noexcept
Definition: collection.hpp:68
bool operator==(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:83
TMember * operator->() const
Definition: collection.hpp:99