GEOS  3.9.1dev
util.h
Go to the documentation of this file.
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/AssertionFailedException.h>
28 //#include <geos/util/UnsupportedOperationException.h>
29 //#include <geos/util/CoordinateArrayFilter.h>
30 //#include <geos/util/UniqueCoordinateArrayFilter.h>
32 //#include <geos/util/math.h>
33 
34 #include <memory>
35 #include <type_traits>
36 
37 //
38 // Private macros definition
39 //
40 
41 namespace geos {
42 template<class T>
43 void
45 
46 namespace detail {
47 #if __cplusplus >= 201402L
48 using std::make_unique;
49 #else
50 // Backport of std::make_unique to C++11
51 // Source: https://stackoverflow.com/a/19472607
52 template<class T>
53 struct _Unique_if {
54  typedef std::unique_ptr<T> _Single_object;
55 };
56 
57 template<class T>
58 struct _Unique_if<T[]> {
59  typedef std::unique_ptr<T[]> _Unknown_bound;
60 };
61 
62 template<class T, size_t N>
63 struct _Unique_if<T[N]> {
64  typedef void _Known_bound;
65 };
66 
67 template<class T, class... Args>
69 make_unique(Args &&... args) {
70  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
71 }
72 
73 template<class T>
75 make_unique(size_t n) {
76  typedef typename std::remove_extent<T>::type U;
77  return std::unique_ptr<T>(new U[n]());
78 }
79 
80 template<class T, class... Args>
82 make_unique(Args &&...) = delete;
83 
84 #endif
85 
95 template<typename To, typename From> inline To down_cast(From* f)
96 {
97  static_assert(
98  (std::is_base_of<From,
99  typename std::remove_pointer<To>::type>::value),
100  "target type not derived from source type");
101 #if GEOS_DEBUG
102  assert(f == nullptr || dynamic_cast<To>(f) != nullptr);
103 #endif
104  return static_cast<To>(f);
105 }
106 
107 } // namespace detail
108 } // namespace geos
109 
110 #endif // GEOS_UTIL_H
std::unique_ptr< T > _Single_object
Definition: util.h:54
std::unique_ptr< T[]> _Unknown_bound
Definition: util.h:59
_Unique_if< T >::_Single_object make_unique(Args &&...args)
Definition: util.h:69
Basic namespace for all GEOS functionalities.
void ignore_unused_variable_warning(T const &)
Definition: util.h:44
_Unique_if< T >::_Known_bound make_unique(Args &&...)=delete
To down_cast(From *f)
Definition: util.h:95