Skip to content

Commit f91eb31

Browse files
authored
Merge pull request #1712 from joto/geom-add-nodiscard
Sprinkle [[nodiscard]] around the geometry code
2 parents 1934cd5 + ac8b19e commit f91eb31

File tree

3 files changed

+73
-46
lines changed

3 files changed

+73
-46
lines changed

src/geom-from-osm.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void create_point(geometry_t *geom, osmium::Node const &node);
4141
* \param node The input node.
4242
* \returns The created geometry.
4343
*/
44-
geometry_t create_point(osmium::Node const &node);
44+
[[nodiscard]] geometry_t create_point(osmium::Node const &node);
4545

4646
/**
4747
* Create a linestring geometry from a way. Nodes without location are ignored.
@@ -67,7 +67,7 @@ void create_linestring(geometry_t *geom, osmium::Way const &way);
6767
* \param way The input way.
6868
* \returns The created geometry.
6969
*/
70-
geometry_t create_linestring(osmium::Way const &way);
70+
[[nodiscard]] geometry_t create_linestring(osmium::Way const &way);
7171

7272
/**
7373
* Create a polygon geometry from a way.
@@ -87,7 +87,7 @@ void create_polygon(geometry_t *geom, osmium::Way const &way);
8787
* \param way The input way.
8888
* \returns The created geometry.
8989
*/
90-
geometry_t create_polygon(osmium::Way const &way);
90+
[[nodiscard]] geometry_t create_polygon(osmium::Way const &way);
9191

9292
/**
9393
* Create a multilinestring geometry from a bunch of ways (usually this
@@ -114,7 +114,8 @@ void create_multilinestring(geometry_t *geom,
114114
* \param ways Buffer containing all the input ways.
115115
* \returns The created geometry.
116116
*/
117-
geometry_t create_multilinestring(osmium::memory::Buffer const &ways);
117+
[[nodiscard]] geometry_t
118+
create_multilinestring(osmium::memory::Buffer const &ways);
118119

119120
/**
120121
* Create a (multi)polygon geometry from a relation and member ways.
@@ -139,8 +140,9 @@ void create_multipolygon(geometry_t *geom, osmium::Relation const &relation,
139140
* \param way_buffer Buffer containing all member ways.
140141
* \returns The created geometry.
141142
*/
142-
geometry_t create_multipolygon(osmium::Relation const &relation,
143-
osmium::memory::Buffer const &way_buffer);
143+
[[nodiscard]] geometry_t
144+
create_multipolygon(osmium::Relation const &relation,
145+
osmium::memory::Buffer const &way_buffer);
144146

145147
/**
146148
* Create a geometry collection from a relation and node/way members.
@@ -164,7 +166,8 @@ void create_collection(geometry_t *geom,
164166
* \param way_buffer Buffer containing all member nodes and ways.
165167
* \returns The created geometry.
166168
*/
167-
geometry_t create_collection(osmium::memory::Buffer const &member_buffer);
169+
[[nodiscard]] geometry_t
170+
create_collection(osmium::memory::Buffer const &member_buffer);
168171

169172
} // namespace geom
170173

src/geom.hpp

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ namespace geom {
3131
class nullgeom_t
3232
{
3333
public:
34-
static std::size_t num_geometries() noexcept { return 0; }
34+
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
35+
{
36+
return 0;
37+
}
3538

36-
constexpr friend bool operator==(nullgeom_t, nullgeom_t) noexcept
39+
[[nodiscard]] constexpr friend bool operator==(nullgeom_t,
40+
nullgeom_t) noexcept
3741
{
3842
return true;
3943
}
4044

41-
constexpr friend bool operator!=(nullgeom_t, nullgeom_t) noexcept
45+
[[nodiscard]] constexpr friend bool operator!=(nullgeom_t,
46+
nullgeom_t) noexcept
4247
{
4348
return false;
4449
}
@@ -56,20 +61,25 @@ class point_t
5661

5762
constexpr point_t(double x, double y) noexcept : m_x(x), m_y(y) {}
5863

59-
static std::size_t num_geometries() noexcept { return 1; }
64+
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
65+
{
66+
return 1;
67+
}
6068

61-
constexpr double x() const noexcept { return m_x; }
62-
constexpr double y() const noexcept { return m_y; }
69+
[[nodiscard]] constexpr double x() const noexcept { return m_x; }
70+
[[nodiscard]] constexpr double y() const noexcept { return m_y; }
6371

6472
constexpr void set_x(double value) noexcept { m_x = value; }
6573
constexpr void set_y(double value) noexcept { m_y = value; }
6674

67-
constexpr friend bool operator==(point_t a, point_t b) noexcept
75+
[[nodiscard]] constexpr friend bool operator==(point_t a,
76+
point_t b) noexcept
6877
{
6978
return a.x() == b.x() && a.y() == b.y();
7079
}
7180

72-
constexpr friend bool operator!=(point_t a, point_t b) noexcept
81+
[[nodiscard]] constexpr friend bool operator!=(point_t a,
82+
point_t b) noexcept
7383
{
7484
return !(a == b);
7585
}
@@ -95,7 +105,10 @@ class point_list_t : public std::vector<point_t>
95105
: std::vector<point_t>(list.begin(), list.end())
96106
{}
97107

98-
static std::size_t num_geometries() noexcept { return 1; }
108+
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
109+
{
110+
return 1;
111+
}
99112

100113
friend bool operator==(point_list_t const &a,
101114
point_list_t const &b) noexcept;
@@ -126,15 +139,21 @@ class polygon_t
126139

127140
explicit polygon_t(ring_t &&ring) : m_outer(std::move(ring)) {}
128141

129-
static std::size_t num_geometries() noexcept { return 1; }
142+
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
143+
{
144+
return 1;
145+
}
130146

131-
ring_t const &outer() const noexcept { return m_outer; }
147+
[[nodiscard]] ring_t const &outer() const noexcept { return m_outer; }
132148

133-
ring_t &outer() noexcept { return m_outer; }
149+
[[nodiscard]] ring_t &outer() noexcept { return m_outer; }
134150

135-
std::vector<ring_t> const &inners() const noexcept { return m_inners; }
151+
[[nodiscard]] std::vector<ring_t> const &inners() const noexcept
152+
{
153+
return m_inners;
154+
}
136155

137-
std::vector<ring_t> &inners() noexcept { return m_inners; }
156+
[[nodiscard]] std::vector<ring_t> &inners() noexcept { return m_inners; }
138157

139158
void add_inner_ring(ring_t &&ring) { m_inners.push_back(std::move(ring)); }
140159

@@ -154,20 +173,23 @@ class multigeometry_t : public std::vector<GEOM>
154173
public:
155174
using const_iterator = typename std::vector<GEOM>::const_iterator;
156175

157-
std::size_t num_geometries() const noexcept { return this->size(); }
176+
[[nodiscard]] std::size_t num_geometries() const noexcept
177+
{
178+
return this->size();
179+
}
158180

159181
void add_geometry(GEOM &&geom) { this->push_back(std::move(geom)); }
160182

161-
GEOM &add_geometry() { return this->emplace_back(); }
183+
[[nodiscard]] GEOM &add_geometry() { return this->emplace_back(); }
162184

163-
friend bool operator==(multigeometry_t const &a,
164-
multigeometry_t const &b) noexcept
185+
[[nodiscard]] friend bool operator==(multigeometry_t const &a,
186+
multigeometry_t const &b) noexcept
165187
{
166188
return std::equal(a.cbegin(), a.cend(), b.cbegin(), b.cend());
167189
}
168190

169-
friend bool operator!=(multigeometry_t const &a,
170-
multigeometry_t const &b) noexcept
191+
[[nodiscard]] friend bool operator!=(multigeometry_t const &a,
192+
multigeometry_t const &b) noexcept
171193
{
172194
return !(a == b);
173195
}
@@ -196,57 +218,57 @@ class geometry_t
196218
: m_geom(std::move(geom)), m_srid(srid)
197219
{}
198220

199-
constexpr int srid() const noexcept { return m_srid; }
221+
[[nodiscard]] constexpr int srid() const noexcept { return m_srid; }
200222

201223
constexpr void set_srid(int srid) noexcept { m_srid = srid; }
202224

203-
constexpr bool is_null() const noexcept
225+
[[nodiscard]] constexpr bool is_null() const noexcept
204226
{
205227
return std::holds_alternative<nullgeom_t>(m_geom);
206228
}
207-
constexpr bool is_point() const noexcept
229+
[[nodiscard]] constexpr bool is_point() const noexcept
208230
{
209231
return std::holds_alternative<point_t>(m_geom);
210232
}
211-
constexpr bool is_linestring() const noexcept
233+
[[nodiscard]] constexpr bool is_linestring() const noexcept
212234
{
213235
return std::holds_alternative<linestring_t>(m_geom);
214236
}
215-
constexpr bool is_polygon() const noexcept
237+
[[nodiscard]] constexpr bool is_polygon() const noexcept
216238
{
217239
return std::holds_alternative<polygon_t>(m_geom);
218240
}
219-
constexpr bool is_multipoint() const noexcept
241+
[[nodiscard]] constexpr bool is_multipoint() const noexcept
220242
{
221243
return std::holds_alternative<multipoint_t>(m_geom);
222244
}
223-
constexpr bool is_multilinestring() const noexcept
245+
[[nodiscard]] constexpr bool is_multilinestring() const noexcept
224246
{
225247
return std::holds_alternative<multilinestring_t>(m_geom);
226248
}
227-
constexpr bool is_multipolygon() const noexcept
249+
[[nodiscard]] constexpr bool is_multipolygon() const noexcept
228250
{
229251
return std::holds_alternative<multipolygon_t>(m_geom);
230252
}
231-
constexpr bool is_collection() const noexcept
253+
[[nodiscard]] constexpr bool is_collection() const noexcept
232254
{
233255
return std::holds_alternative<collection_t>(m_geom);
234256
}
235257

236-
constexpr bool is_multi() const noexcept
258+
[[nodiscard]] constexpr bool is_multi() const noexcept
237259
{
238260
return is_multipoint() || is_multilinestring() || is_multipolygon() ||
239261
is_collection();
240262
}
241263

242264
template <typename T>
243-
constexpr T const &get() const
265+
[[nodiscard]] constexpr T const &get() const
244266
{
245267
return std::get<T>(m_geom);
246268
}
247269

248270
template <typename T>
249-
constexpr T &get()
271+
[[nodiscard]] constexpr T &get()
250272
{
251273
return std::get<T>(m_geom);
252274
}
@@ -265,12 +287,14 @@ class geometry_t
265287
return std::visit(std::forward<V>(visitor), m_geom);
266288
}
267289

268-
friend bool operator==(geometry_t const &a, geometry_t const &b) noexcept
290+
[[nodiscard]] friend bool operator==(geometry_t const &a,
291+
geometry_t const &b) noexcept
269292
{
270293
return (a.srid() == b.srid()) && (a.m_geom == b.m_geom);
271294
}
272295

273-
friend bool operator!=(geometry_t const &a, geometry_t const &b) noexcept
296+
[[nodiscard]] friend bool operator!=(geometry_t const &a,
297+
geometry_t const &b) noexcept
274298
{
275299
return !(a == b);
276300
}

src/wkb.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
* \param ensure_multi Wrap non-multi geometries in multi geometries
2828
* \returns String with EWKB encoded geometry
2929
*/
30-
std::string geom_to_ewkb(geom::geometry_t const &geom,
31-
bool ensure_multi = false);
30+
[[nodiscard]] std::string geom_to_ewkb(geom::geometry_t const &geom,
31+
bool ensure_multi = false);
3232

3333
/**
3434
* Convert EWKB geometry to geometry object. If the input is empty, a null
@@ -37,18 +37,18 @@ std::string geom_to_ewkb(geom::geometry_t const &geom,
3737
* \param wkb Input EWKB geometry in binary format
3838
* \returns Geometry
3939
*/
40-
geom::geometry_t ewkb_to_geom(std::string const &wkb);
40+
[[nodiscard]] geom::geometry_t ewkb_to_geom(std::string const &wkb);
4141

4242
/**
4343
* Decode one hex character (0-9A-F or 0-9a-f) and return its value. Throw
4444
* an exception if not a valid hex character.
4545
*/
46-
unsigned char decode_hex_char(char c);
46+
[[nodiscard]] unsigned char decode_hex_char(char c);
4747

4848
/**
4949
* Decode a string of hex characters. Throws an exception if the input is not
5050
* a valid hex encoding.
5151
*/
52-
std::string decode_hex(char const *hex);
52+
[[nodiscard]] std::string decode_hex(char const *hex);
5353

5454
#endif // OSM2PGSQL_WKB_HPP

0 commit comments

Comments
 (0)