Libosmium  2.1.0
Fast and flexible C++ library for working with OpenStreetMap data
item.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_MEMORY_ITEM_HPP
2 #define OSMIUM_MEMORY_ITEM_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 <cstddef>
37 #include <cstdint>
38 #include <type_traits>
39 
40 namespace osmium {
41 
42  // forward declaration, see osmium/osm/item_type.hpp for declaration
43  enum class item_type : uint16_t;
44 
45  namespace builder {
46  class Builder;
47  }
48 
49  namespace memory {
50 
51  typedef uint32_t item_size_type;
52 
53  // align datastructures to this many bytes
54  constexpr item_size_type align_bytes = 8;
55 
56  template <typename T>
57  inline T padded_length(T length) noexcept {
58  return (length + align_bytes - 1) & ~(align_bytes - 1);
59  }
60 
64  namespace detail {
65 
70  class ItemHelper {
71 
72  protected:
73 
74  ItemHelper() = default;
75 
76  ~ItemHelper() = default;
77 
78  ItemHelper(const ItemHelper&) = default;
79  ItemHelper(ItemHelper&&) = default;
80 
81  ItemHelper& operator=(const ItemHelper&) = default;
82  ItemHelper& operator=(ItemHelper&&) = default;
83 
84  public:
85 
86  unsigned char* data() noexcept {
87  return reinterpret_cast<unsigned char*>(this);
88  }
89 
90  const unsigned char* data() const noexcept {
91  return reinterpret_cast<const unsigned char*>(this);
92  }
93 
94  }; // class ItemHelper
95 
96  } // namespace detail
97 
98  class Item : public osmium::memory::detail::ItemHelper {
99 
100  item_size_type m_size;
102  uint16_t m_removed : 1;
103  uint16_t m_padding : 15;
104 
105  template <class TMember>
106  friend class CollectionIterator;
107 
108  template <class TMember>
109  friend class ItemIterator;
110 
112 
113  Item& add_size(const item_size_type size) noexcept {
114  m_size += size;
115  return *this;
116  }
117 
118  protected:
119 
120  explicit Item(item_size_type size=0, item_type type=item_type()) noexcept :
121  m_size(size),
122  m_type(type),
123  m_removed(false),
124  m_padding(0) {
125  }
126 
127  Item(const Item&) = delete;
128  Item(Item&&) = delete;
129 
130  Item& operator=(const Item&) = delete;
131  Item& operator=(Item&&) = delete;
132 
133  Item& set_type(const item_type item_type) noexcept {
134  m_type = item_type;
135  return *this;
136  }
137 
138  public:
139 
140  unsigned char* next() noexcept {
141  return data() + padded_size();
142  }
143 
144  const unsigned char* next() const noexcept {
145  return data() + padded_size();
146  }
147 
148  item_size_type byte_size() const noexcept {
149  return m_size;
150  }
151 
152  item_size_type padded_size() const {
153  return padded_length(m_size);
154  }
155 
156  item_type type() const noexcept {
157  return m_type;
158  }
159 
160  bool removed() const noexcept {
161  return m_removed;
162  }
163 
164  void set_removed(bool removed) noexcept {
165  m_removed = removed;
166  }
167 
168  }; // class Item
169 
170 
171  } // namespace memory
172 
173 } // namespace osmium
174 
175 #endif // OSMIUM_MEMORY_ITEM_HPP
Definition: collection.hpp:48
Item & set_type(const item_type item_type) noexcept
Definition: item.hpp:133
Definition: item_iterator.hpp:131
unsigned char * next() noexcept
Definition: item.hpp:140
item_type
Definition: item_type.hpp:42
constexpr item_size_type align_bytes
Definition: item.hpp:54
item_size_type padded_size() const
Definition: item.hpp:152
T padded_length(T length) noexcept
Definition: item.hpp:57
Definition: item.hpp:98
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
uint16_t m_removed
Definition: item.hpp:102
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:113
const unsigned char * next() const noexcept
Definition: item.hpp:144
uint16_t m_padding
Definition: item.hpp:103
item_size_type m_size
Definition: item.hpp:100
void set_removed(bool removed) noexcept
Definition: item.hpp:164
bool removed() const noexcept
Definition: item.hpp:160
item_size_type byte_size() const noexcept
Definition: item.hpp:148
Item(item_size_type size=0, item_type type=item_type()) noexcept
Definition: item.hpp:120
Item & operator=(const Item &)=delete
uint32_t size() const noexcept
Definition: builder.hpp:123
uint32_t item_size_type
Definition: item.hpp:51
item_type m_type
Definition: item.hpp:101
item_type type() const noexcept
Definition: item.hpp:156
Definition: builder.hpp:57