Libosmium  2.3.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-2015 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 <class 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  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  friend std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const CollectionIterator<TMember>& iter) {
99  return out << static_cast<const void*>(iter.m_data);
100  }
101 
102  }; // class CollectionIterator
103 
104  template <class TMember, osmium::item_type TCollectionItemType>
105  class Collection : public Item {
106 
107  public:
108 
111  typedef TMember value_type;
112 
113  static constexpr osmium::item_type itemtype = TCollectionItemType;
114 
116  Item(sizeof(Collection<TMember, TCollectionItemType>), TCollectionItemType) {
117  }
118 
119  bool empty() const {
121  }
122 
123  iterator begin() {
124  return iterator(data() + sizeof(Collection<TMember, TCollectionItemType>));
125  }
126 
127  iterator end() {
128  return iterator(data() + byte_size());
129  }
130 
131  const_iterator cbegin() const {
133  }
134 
135  const_iterator cend() const {
136  return const_iterator(data() + byte_size());
137  }
138 
139  const_iterator begin() const {
140  return cbegin();
141  }
142 
143  const_iterator end() const {
144  return cend();
145  }
146 
147  }; // class Collection
148 
149  } // namespace memory
150 
151 } // namespace osmium
152 
153 #endif // OSMIUM_MEMORY_COLLECTION_HPP
bool empty() const
Definition: collection.hpp:119
iterator end()
Definition: collection.hpp:127
Definition: collection.hpp:47
CollectionIterator< TMember > & operator++()
Definition: collection.hpp:66
type
Definition: entity_bits.hpp:60
CollectionIterator< TMember > iterator
Definition: collection.hpp:109
const_iterator cend() const
Definition: collection.hpp:135
item_type
Definition: item_type.hpp:43
CollectionIterator< TMember > operator++(int)
Definition: collection.hpp:71
CollectionIterator< const TMember > const_iterator
Definition: collection.hpp:110
const_iterator begin() const
Definition: collection.hpp:139
iterator begin()
Definition: collection.hpp:123
static constexpr osmium::item_type itemtype
Definition: collection.hpp:113
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:55
TMember value_type
Definition: collection.hpp:111
Definition: collection.hpp:105
item_size_type byte_size() const noexcept
Definition: item.hpp:147
unsigned char * data() const noexcept
Definition: collection.hpp:85
Collection()
Definition: collection.hpp:115
const_iterator cbegin() const
Definition: collection.hpp:131
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:143