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