Translations update
[openttd/fttd.git] / src / core / pointer.h
blob073c39115067630c874d54f72ab6e3b75798621a
1 /*
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/>.
6 */
8 /** @file pointer.h Smart pointer types. */
10 #ifndef POINTER_H
11 #define POINTER_H
13 #include <cstdlib>
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
24 * on destruction.
28 #ifdef __GNUC__
29 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
30 # include <memory>
31 # define ttd_shared_ptr std::shared_ptr
32 # define ttd_unique_ptr std::unique_ptr
33 # define TTD_UNIQUE_PTR_USABLE 1
34 # else
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
39 # endif
40 #endif /* __GNUC__ */
43 #ifdef _MSC_VER
44 # include <memory>
45 # if _MSC_VER < 1600
46 # define ttd_shared_ptr std::tr1::shared_ptr
47 # define ttd_unique_ptr ttd_shared_ptr
48 # else
49 # define ttd_shared_ptr std::shared_ptr
50 # define ttd_unique_ptr std::unique_ptr
51 # endif
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
55 #endif /* _MSC_VER */
58 #if TTD_UNIQUE_PTR_USABLE
60 struct ttd_delete_free {
61 void operator() (void *p) { free(p); }
64 template <typename T>
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) { }
73 #else
75 template <typename 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)
82 free (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)
91 void reset (void)
93 ttd_shared_ptr<T>::reset();
96 template <typename TT>
97 void reset (TT *p)
99 ttd_shared_ptr<T>::reset (p, ttd_free);
103 #endif
106 #endif /* POINTER_H */