GEOS  3.11.0rc0
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 <cassert>
25 #include <memory>
26 #include <type_traits>
27 
28 //
29 // Private macros definition
30 //
31 
32 namespace geos {
33 template<class T>
34 void
35 ignore_unused_variable_warning(T const &) {}
36 
37 namespace detail {
38 #if __cplusplus >= 201402L
39 using std::make_unique;
40 #else
41 // Backport of std::make_unique to C++11
42 // Source: https://stackoverflow.com/a/19472607
43 template<class T>
44 struct _Unique_if {
45  typedef std::unique_ptr<T> _Single_object;
46 };
47 
48 template<class T>
49 struct _Unique_if<T[]> {
50  typedef std::unique_ptr<T[]> _Unknown_bound;
51 };
52 
53 template<class T, std::size_t N>
54 struct _Unique_if<T[N]> {
55  typedef void _Known_bound;
56 };
57 
58 template<class T, class... Args>
59 typename _Unique_if<T>::_Single_object
60 make_unique(Args &&... args) {
61  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
62 }
63 
64 template<class T>
65 typename _Unique_if<T>::_Unknown_bound
66 make_unique(std::size_t n) {
67  typedef typename std::remove_extent<T>::type U;
68  return std::unique_ptr<T>(new U[n]());
69 }
70 
71 template<class T, class... Args>
72 typename _Unique_if<T>::_Known_bound
73 make_unique(Args &&...) = delete;
74 
75 #endif
76 
86 template<typename To, typename From> inline To down_cast(From* f)
87 {
88  static_assert(
89  (std::is_base_of<From,
90  typename std::remove_pointer<To>::type>::value),
91  "target type not derived from source type");
92 #if GEOS_DEBUG
93  assert(f == nullptr || dynamic_cast<To>(f) != nullptr);
94 #endif
95  return static_cast<To>(f);
96 }
97 
98 // Avoid "redundant move" warning when calling std::move() to return
99 // unique_ptr<Derived> from a function with return type unique_ptr<Base>
100 // The std::move is required for the gcc 4.9 series, which has not addressed
101 // CWG defect 1579 ("return by converting move constructor")
102 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1579
103 #if __GNUC__ > 0 && __GNUC__ < 5
104 #define RETURN_UNIQUE_PTR(x) (std::move(x))
105 #else
106 #define RETURN_UNIQUE_PTR(x) (x)
107 #endif
108 
109 } // namespace detail
110 } // namespace geos
111 
112 #endif // GEOS_UTIL_H
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25