GEOS 3.10.1
util.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2001-2002 Vivid Solutions Inc.
7 * Copyright (C) 2006 Refractions Research Inc.
8 *
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
13 *
14 **********************************************************************
15 *
16 * Utility header to retain a bit of backward compatibility.
17 * Try to avoid including this header directly.
18 *
19 **********************************************************************/
20
21#ifndef GEOS_UTIL_H
22#define GEOS_UTIL_H
23
24#include <geos/util/GEOSException.h>
25#include <geos/util/IllegalArgumentException.h>
26#include <geos/util/TopologyException.h>
27#include <geos/util/GeometricShapeFactory.h>
28
29#include <memory>
30#include <type_traits>
31
32//
33// Private macros definition
34//
35
36namespace geos {
37template<class T>
38void
39ignore_unused_variable_warning(T const &) {}
40
41namespace detail {
42#if __cplusplus >= 201402L
43using std::make_unique;
44#else
45// Backport of std::make_unique to C++11
46// Source: https://stackoverflow.com/a/19472607
47template<class T>
48struct _Unique_if {
49 typedef std::unique_ptr<T> _Single_object;
50};
51
52template<class T>
53struct _Unique_if<T[]> {
54 typedef std::unique_ptr<T[]> _Unknown_bound;
55};
56
57template<class T, std::size_t N>
58struct _Unique_if<T[N]> {
59 typedef void _Known_bound;
60};
61
62template<class T, class... Args>
63typename _Unique_if<T>::_Single_object
64make_unique(Args &&... args) {
65 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
66}
67
68template<class T>
69typename _Unique_if<T>::_Unknown_bound
70make_unique(std::size_t n) {
71 typedef typename std::remove_extent<T>::type U;
72 return std::unique_ptr<T>(new U[n]());
73}
74
75template<class T, class... Args>
76typename _Unique_if<T>::_Known_bound
77make_unique(Args &&...) = delete;
78
79#endif
80
90template<typename To, typename From> inline To down_cast(From* f)
91{
92 static_assert(
93 (std::is_base_of<From,
94 typename std::remove_pointer<To>::type>::value),
95 "target type not derived from source type");
96#if GEOS_DEBUG
97 assert(f == nullptr || dynamic_cast<To>(f) != nullptr);
98#endif
99 return static_cast<To>(f);
100}
101
102// Avoid "redundant move" warning when calling std::move() to return
103// unique_ptr<Derived> from a function with return type unique_ptr<Base>
104// The std::move is required for the gcc 4.9 series, which has not addressed
105// CWG defect 1579 ("return by converting move constructor")
106// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1579
107#if __GNUC__ > 0 && __GNUC__ < 5
108#define RETURN_UNIQUE_PTR(x) (std::move(x))
109#else
110#define RETURN_UNIQUE_PTR(x) (x)
111#endif
112
113} // namespace detail
114} // namespace geos
115
116#endif // GEOS_UTIL_H
Basic namespace for all GEOS functionalities.
Definition: geos.h:40