Libosmium  2.1.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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>
42 
43 namespace osmium {
44 
45  namespace memory {
46 
47  template <class TMember>
48  class CollectionIterator : public std::iterator<std::forward_iterator_tag, TMember> {
49 
50  // This data_type is either 'unsigned char*' or 'const unsigned char*' depending
51  // on whether TMember is const. This allows this class to be used as an iterator and
52  // as a const_iterator.
53  typedef typename std::conditional<std::is_const<TMember>::value, const unsigned char*, unsigned char*>::type data_type;
54 
55  data_type m_data;
56 
57  public:
58 
59  CollectionIterator() noexcept :
60  m_data(nullptr) {
61  }
62 
63  CollectionIterator(data_type data) noexcept :
64  m_data(data) {
65  }
66 
68  m_data = reinterpret_cast<TMember*>(m_data)->next();
69  return *static_cast<CollectionIterator<TMember>*>(this);
70  }
71 
73  CollectionIterator<TMember> tmp(*this);
74  operator++();
75  return tmp;
76  }
77 
78  bool operator==(const CollectionIterator<TMember>& rhs) const noexcept {
79  return m_data == rhs.m_data;
80  }
81 
82  bool operator!=(const CollectionIterator<TMember>& rhs) const noexcept {
83  return m_data != rhs.m_data;
84  }
85 
86  unsigned char* data() const noexcept {
87  return m_data;
88  }
89 
90  TMember& operator*() const {
91  return *reinterpret_cast<TMember*>(m_data);
92  }
93 
94  TMember* operator->() const {
95  return reinterpret_cast<TMember*>(m_data);
96  }
97 
98  template <typename TChar, typename TTraits>
99  friend std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const CollectionIterator<TMember>& iter) {
100  return out << static_cast<const void*>(iter.m_data);
101  }
102 
103  }; // class CollectionIterator
104 
105  template <class TMember, osmium::item_type TCollectionItemType>
106  class Collection : public Item {
107 
108  public:
109 
112  typedef TMember value_type;
113 
114  static constexpr osmium::item_type itemtype = TCollectionItemType;
115 
117  Item(sizeof(Collection<TMember, TCollectionItemType>), TCollectionItemType) {
118  }
119 
120  bool empty() const {
122  }
123 
124  iterator begin() {
125  return iterator(data() + sizeof(Collection<TMember, TCollectionItemType>));
126  }
127 
128  iterator end() {
129  return iterator(data() + byte_size());
130  }
131 
132  const_iterator cbegin() const {
134  }
135 
136  const_iterator cend() const {
137  return const_iterator(data() + byte_size());
138  }
139 
140  const_iterator begin() const {
141  return cbegin();
142  }
143 
144  const_iterator end() const {
145  return cend();
146  }
147 
148  }; // class Collection
149 
150  } // namespace memory
151 
152 } // namespace osmium
153 
154 #endif // OSMIUM_MEMORY_COLLECTION_HPP
bool empty() const
Definition: collection.hpp:120
iterator end()
Definition: collection.hpp:128
Definition: collection.hpp:48
CollectionIterator< TMember > & operator++()
Definition: collection.hpp:67
type
Definition: entity_bits.hpp:60
CollectionIterator< TMember > iterator
Definition: collection.hpp:110
const_iterator cend() const
Definition: collection.hpp:136
item_type
Definition: item_type.hpp:42
CollectionIterator< TMember > operator++(int)
Definition: collection.hpp:72
CollectionIterator< const TMember > const_iterator
Definition: collection.hpp:111
const_iterator begin() const
Definition: collection.hpp:140
iterator begin()
Definition: collection.hpp:124
static constexpr osmium::item_type itemtype
Definition: collection.hpp:114
CollectionIterator() noexcept
Definition: collection.hpp:59
bool operator!=(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:82
TMember & operator*() const
Definition: collection.hpp:90
Definition: item.hpp:98
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
TMember value_type
Definition: collection.hpp:112
Definition: collection.hpp:106
item_size_type byte_size() const noexcept
Definition: item.hpp:148
unsigned char * data() const noexcept
Definition: collection.hpp:86
Collection()
Definition: collection.hpp:116
const_iterator cbegin() const
Definition: collection.hpp:132
data_type m_data
Definition: collection.hpp:55
CollectionIterator(data_type data) noexcept
Definition: collection.hpp:63
bool operator==(const CollectionIterator< TMember > &rhs) const noexcept
Definition: collection.hpp:78
std::conditional< std::is_const< TMember >::value, const unsigned char *, unsigned char * >::type data_type
Definition: collection.hpp:53
TMember * operator->() const
Definition: collection.hpp:94
const_iterator end() const
Definition: collection.hpp:144