2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file pointer.h Smart pointer types. */
16 * Alias a number of smart pointer types to our own custom names, depending
17 * on the actual types provided by the implementation:
18 * * ttd_shared_ptr is aliased to std::shared_ptr.
19 * * ttd_unique_ptr is aliased to std::unique_ptr if available, else to
20 * ttd_shared_ptr (as a consequence, ttd_unique_ptr can only be used with
21 * one template argument, not two).
22 * * ttd_unique_free_ptr is an implementation of a ttd_unique_ptr (possibly
23 * through ttd_shared_ptr) that frees (not deletes) its managed pointer
29 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
31 # define ttd_shared_ptr std::shared_ptr
32 # define ttd_unique_ptr std::unique_ptr
33 # define TTD_UNIQUE_PTR_USABLE 1
35 # include <tr1/memory>
36 # define ttd_shared_ptr std::tr1::shared_ptr
37 # define ttd_unique_ptr ttd_shared_ptr
38 # define TTD_UNIQUE_PTR_USABLE 0
46 # define ttd_shared_ptr std::tr1::shared_ptr
47 # define ttd_unique_ptr ttd_shared_ptr
49 # define ttd_shared_ptr std::shared_ptr
50 # define ttd_unique_ptr std::unique_ptr
52 /* Trying to derive from std::unique_ptr results in MSVC complaining that it
53 * cannot generate the default copy constructor for the derived class. */
54 # define TTD_UNIQUE_PTR_USABLE 0
58 #if TTD_UNIQUE_PTR_USABLE
60 struct ttd_delete_free
{
61 void operator() (void *p
) { free(p
); }
65 struct ttd_unique_free_ptr
: ttd_unique_ptr
<T
, ttd_delete_free
> {
66 CONSTEXPR
ttd_unique_free_ptr()
67 : ttd_unique_ptr
<T
, ttd_delete_free
> () { }
69 explicit ttd_unique_free_ptr (T
*t
)
70 : ttd_unique_ptr
<T
, ttd_delete_free
> (t
) { }
76 struct ttd_unique_free_ptr
: ttd_shared_ptr
<T
> {
77 /* Using free directly as deleter requires a cast for overload
78 * resolution, and that seems to cause calling convention confusion
79 * in MSVC, so we use this proxy function. */
80 static void ttd_free (void *p
)
85 CONSTEXPR
ttd_unique_free_ptr() : ttd_shared_ptr
<T
> () { }
87 explicit ttd_unique_free_ptr (T
*t
) : ttd_shared_ptr
<T
> (t
, ttd_free
)
93 ttd_shared_ptr
<T
>::reset();
96 template <typename TT
>
99 ttd_shared_ptr
<T
>::reset (p
, ttd_free
);
106 #endif /* POINTER_H */