Libosmium  2.6.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>
47  class CollectionIterator : public std::iterator<std::forward_iterator_tag, 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  typedef typename std::conditional<std::is_const<TMember>::value, const unsigned char*, unsigned char*>::type data_type;
53 
54  data_type m_data;
55 
56  public:
57 
58  CollectionIterator() noexcept :
59  m_data(nullptr) {
60  }
61 
62  explicit CollectionIterator(data_type data) noexcept :
63  m_data(data) {
64  }
65 
67  m_data = reinterpret_cast<TMember*>(m_data)->next();
68  return *static_cast<CollectionIterator<TMember>*>(this);
69  }
70 
72  CollectionIterator<TMember> tmp(*this);
73  operator++();
74  return tmp;
75  }
76 
77  bool operator==(const CollectionIterator<TMember>& rhs) const noexcept {
78  return m_data == rhs.m_data;
79  }
80 
81  bool operator!=(const CollectionIterator<TMember>& rhs) const noexcept {
82  return m_data != rhs.m_data;
83  }
84 
85  unsigned char* data() const noexcept {
86  return m_data;
87  }
88 
89  TMember& operator*() const {
90  return *reinterpret_cast<TMember*>(m_data);
91  }
92 
93  TMember* operator->() const {
94  return reinterpret_cast<TMember*>(m_data);
95  }
96 
97  template <typename TChar, typename TTraits>
98  void print(std::basic_ostream<TChar, TTraits>& out) const {
99  out << static_cast<const void*>(m_data);
100  }
101 
102  }; // class CollectionIterator
103 
104  template <typename TChar, typename TTraits, typename TMember>
105  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const CollectionIterator<TMember>& iter) {
106  iter.print(out);
107  return out;
108  }
109 
110  template <typename TMember, osmium::item_type TCollectionItemType>
111  class Collection : public Item {
112 
113  public:
114 
117  typedef TMember value_type;
118 
119  static constexpr osmium::item_type itemtype = TCollectionItemType;
120 
122  Item(sizeof(Collection<TMember, TCollectionItemType>), TCollectionItemType) {
123  }
124 
125  bool empty() const {
127  }
128 
129  iterator begin() {
130  return iterator(data() + sizeof(Collection<TMember, TCollectionItemType>));
131  }
132 
133  iterator end() {
134  return iterator(data() + byte_size());
135  }
136 
137  const_iterator cbegin() const {
139  }
140 
141  const_iterator cend() const {
142  return const_iterator(data() + byte_size());
143  }
144 
145  const_iterator begin() const {
146  return cbegin();
147  }
148 
149  const_iterator end() const {
150  return cend();
151  }
152 
153  }; // class Collection
154 
155  } // namespace memory
156 
157 } // namespace osmium
158 
159 #endif // OSMIUM_MEMORY_COLLECTION_HPP
bool empty() const
Definition: collection.hpp:125
iterator end()
Definition: collection.hpp:133
Definition: collection.hpp:47
CollectionIterator< TMember > & operator++()
Definition: collection.hpp:66
type
Definition: entity_bits.hpp:60
CollectionIterator< TMember > iterator
Definition: collection.hpp:115
const_iterator cend() const
Definition: collection.hpp:141
item_type
Definition: item_type.hpp:43
CollectionIterator< TMember > operator++(int)
Definition: collection.hpp:71
CollectionIterator< const TMember > const_iterator
Definition: collection.hpp:116
const_iterator begin() const
Definition: collection.hpp:145
iterator begin()
Definition: collection.hpp:129
static constexpr osmium::item_type itemtype
Definition: collection.hpp:119
CollectionIterator() noexcept
Definition: collection.hpp:58
bool operator!=(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:81
TMember & operator*() const
Definition: collection.hpp:89
Definition: item.hpp:97
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
TMember value_type
Definition: collection.hpp:117
Definition: collection.hpp:111
item_size_type byte_size() const noexcept
Definition: item.hpp:147
unsigned char * data() const noexcept
Definition: collection.hpp:85
Collection()
Definition: collection.hpp:121
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition: collection.hpp:98
const_iterator cbegin() const
Definition: collection.hpp:137
data_type m_data
Definition: collection.hpp:54
CollectionIterator(data_type data) noexcept
Definition: collection.hpp:62
bool operator==(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:77
std::conditional< std::is_const< TMember >::value, const unsigned char *, unsigned char * >::type data_type
Definition: collection.hpp:52
TMember * operator->() const
Definition: collection.hpp:93
const_iterator end() const
Definition: collection.hpp:149