Panda3D
 All Classes Functions Variables Enumerations
dtoolbase_cc.h
1 // Filename: dtoolbase_cc.h
2 // Created by: drose (13Sep00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef DTOOLBASE_CC_H
16 #define DTOOLBASE_CC_H
17 
18 // This file should never be included directly; it's intended to be
19 // included only from dtoolbase.h. Include that file instead.
20 
21 #ifdef __cplusplus
22 
23 #ifdef USE_TAU
24 // Tau provides this destructive version of stdbool.h that we must mask.
25 #define __PDT_STDBOOL_H_
26 #endif
27 
28 #ifdef CPPPARSER
29 #include <iostream>
30 #include <string>
31 
32 using namespace std;
33 
34 #define INLINE inline
35 #define TYPENAME typename
36 #define CONSTEXPR
37 #define NOEXCEPT noexcept
38 #define FINAL
39 #define OVERRIDE
40 #define MOVE(x) x
41 
42 #define EXPORT_TEMPLATE_CLASS(expcl, exptp, classname)
43 
44 // We define the macro PUBLISHED to mark C++ methods that are to be
45 // published via interrogate to scripting languages. However, if
46 // we're not running the interrogate pass (CPPPARSER isn't defined),
47 // this maps to public.
48 #define PUBLISHED __published
49 
50 typedef int streamsize;
51 typedef int ios_openmode;
52 typedef int ios_fmtflags;
53 typedef int ios_iostate;
54 typedef int ios_seekdir;
55 
56 #else // CPPPARSER
57 
58 #ifdef PHAVE_IOSTREAM
59 #include <iostream>
60 #include <fstream>
61 #include <iomanip>
62 #else
63 #include <iostream.h>
64 #include <fstream.h>
65 #include <iomanip.h>
66 #endif
67 
68 #ifdef PHAVE_SSTREAM
69 #include <sstream>
70 #else
71 #include "fakestringstream.h"
72 #endif
73 
74 #ifdef PHAVE_NEW
75 #include <new>
76 #endif
77 
78 #include <string>
79 
80 #ifdef HAVE_NAMESPACE
81 using namespace std;
82 #endif
83 
84 #ifdef HAVE_TYPENAME
85 #define TYPENAME typename
86 #else
87 #define TYPENAME
88 #endif
89 
90 #ifndef HAVE_WCHAR_T
91 // Some C++ libraries (os x 3.1) don't define this.
92 typedef unsigned short wchar_t;
93 #endif
94 
95 #ifndef HAVE_WSTRING
96 // Some C++ libraries (gcc 2.95) don't define this.
97 typedef basic_string<wchar_t> wstring;
98 #endif
99 
100 #ifndef HAVE_STREAMSIZE
101 // Some C++ libraries (Irix) don't define this.
102 typedef int streamsize;
103 #endif
104 
105 #ifndef HAVE_IOS_TYPEDEFS
106 typedef int ios_openmode;
107 typedef int ios_fmtflags;
108 typedef int ios_iostate;
109 // Old iostream libraries used ios::seek_dir instead of ios::seekdir.
110 typedef ios::seek_dir ios_seekdir;
111 #else
112 typedef ios::openmode ios_openmode;
113 typedef ios::fmtflags ios_fmtflags;
114 typedef ios::iostate ios_iostate;
115 typedef ios::seekdir ios_seekdir;
116 #endif
117 
118 #if defined(WIN32_VC) && defined(FORCE_INLINING)
119 // If FORCE_INLINING is defined, we use the keyword __forceinline,
120 // which tells MS VC++ to override its internal benefit heuristic
121 // and inline the fn if it is technically possible to do so.
122 #define INLINE __forceinline
123 #else
124 #define INLINE inline
125 #endif
126 
127 // Determine the availability of C++11 features.
128 #if defined(__has_extension) // Clang magic.
129 # if __has_extension(cxx_constexpr)
130 # define CONSTEXPR constexpr
131 # else
132 # define CONSTEXPR INLINE
133 # endif
134 # if __has_extension(cxx_noexcept)
135 # define NOEXCEPT noexcept
136 # else
137 # define NOEXCEPT
138 # endif
139 # if __has_extension(cxx_rvalue_references) && (__cplusplus >= 201103L)
140 # define USE_MOVE_SEMANTICS
141 # define MOVE(x) move(x)
142 # else
143 # define MOVE(x) x
144 # endif
145 # if __has_extension(cxx_override_control) && (__cplusplus >= 201103L)
146 # define FINAL final
147 # define OVERRIDE override
148 # else
149 # define FINAL
150 # define OVERRIDE
151 # endif
152 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && (__cplusplus >= 201103L)
153 // noexcept was introduced in GCC 4.6, constexpr in GCC 4.7, rvalue refs in
154 // GCC 4.3. However, GCC only started defining __cplusplus properly in 4.7.
155 # define CONSTEXPR constexpr
156 # define NOEXCEPT noexcept
157 # define USE_MOVE_SEMANTICS
158 # define FINAL final
159 # define OVERRIDE override
160 # define MOVE(x) move(x)
161 #elif defined(_MSC_VER) && _MSC_VER >= 1600
162 // MSVC 2010 has move semantics. Not much else.
163 # define CONSTEXPR INLINE
164 # define NOEXCEPT throw()
165 # define USE_MOVE_SEMANTICS
166 # define FINAL
167 # define OVERRIDE
168 # define MOVE(x) move(x)
169 #else
170 # define CONSTEXPR INLINE
171 # define NOEXCEPT
172 # define FINAL
173 # define OVERRIDE
174 # define MOVE(x) x
175 #endif
176 
177 #if defined(WIN32_VC) && !defined(LINK_ALL_STATIC) && defined(EXPORT_TEMPLATES)
178 // This macro must be used to export an instantiated template class
179 // from a DLL. If the template class name itself contains commas, it
180 // may be necessary to first define a macro for the class name, to
181 // allow proper macro parameter passing.
182 #define EXPORT_TEMPLATE_CLASS(expcl, exptp, classname) \
183  exptp template class expcl classname;
184 #else
185 #define EXPORT_TEMPLATE_CLASS(expcl, exptp, classname)
186 #endif
187 
188 // We define the macro PUBLISHED to mark C++ methods that are to be
189 // published via interrogate to scripting languages. However, if
190 // we're not running the interrogate pass (CPPPARSER isn't defined),
191 // this maps to public.
192 #define PUBLISHED public
193 
194 #endif // CPPPARSER
195 
196 // The ReferenceCount class is defined later, within Panda, but we
197 // need to pass around forward references to it here at the very low
198 // level.
199 class ReferenceCount;
200 
201 // We need a pointer to a global MemoryHook object, to manage all
202 // malloc and free requests from Panda. See the comments in
203 // MemoryHook itself.
204 class MemoryHook;
205 EXPCL_DTOOL extern MemoryHook *memory_hook;
206 EXPCL_DTOOL void init_memory_hook();
207 
208 // Now redefine some handy macros to hook into the above MemoryHook
209 // object.
210 #ifndef USE_MEMORY_NOWRAPPERS
211 #define PANDA_MALLOC_SINGLE(size) (memory_hook->heap_alloc_single(size))
212 #define PANDA_FREE_SINGLE(ptr) memory_hook->heap_free_single(ptr)
213 #define PANDA_MALLOC_ARRAY(size) (memory_hook->heap_alloc_array(size))
214 #define PANDA_REALLOC_ARRAY(ptr, size) (memory_hook->heap_realloc_array(ptr, size))
215 #define PANDA_FREE_ARRAY(ptr) memory_hook->heap_free_array(ptr)
216 #else
217 #define PANDA_MALLOC_SINGLE(size) ::malloc(size)
218 #define PANDA_FREE_SINGLE(ptr) ::free(ptr)
219 #define PANDA_MALLOC_ARRAY(size) ::malloc(size)
220 #define PANDA_REALLOC_ARRAY(ptr, size) ::realloc(ptr, size)
221 #define PANDA_FREE_ARRAY(ptr) ::free(ptr)
222 #endif // USE_MEMORY_NOWRAPPERS
223 
224 #if defined(HAVE_THREADS) && defined(SIMPLE_THREADS)
225 // We need another forward-reference function to allow low-level code
226 // to cooperatively yield the timeslice, in SIMPLE_THREADS mode.
227 extern EXPCL_DTOOL void (*global_thread_yield)();
228 extern EXPCL_DTOOL void (*global_thread_consider_yield)();
229 
230 INLINE void thread_yield() {
231  (*global_thread_yield)();
232 }
233 INLINE void thread_consider_yield() {
234  (*global_thread_consider_yield)();
235 }
236 
237 #else
238 
239 INLINE void thread_yield() {
240 }
241 INLINE void thread_consider_yield() {
242 }
243 
244 #endif // HAVE_THREADS && SIMPLE_THREADS
245 
246 #if defined(USE_TAU) && defined(WIN32)
247 // Hack around tau's lack of DLL export declarations for Profiler class.
248 extern EXPCL_DTOOL bool __tau_shutdown;
249 class EXPCL_DTOOL TauProfile {
250 public:
251  TauProfile(void *&tautimer, char *name, char *type, int group, char *group_name) {
252  Tau_profile_c_timer(&tautimer, name, type, group, group_name);
253  _tautimer = tautimer;
254  TAU_PROFILE_START(_tautimer);
255  }
256  ~TauProfile() {
257  if (!__tau_shutdown) {
258  TAU_PROFILE_STOP(_tautimer);
259  }
260  }
261 
262 private:
263  void *_tautimer;
264 };
265 
266 #undef TAU_PROFILE
267 #define TAU_PROFILE(name, type, group) \
268  static void *__tautimer; \
269  TauProfile __taupr(__tautimer, name, type, group, #group)
270 
271 #undef TAU_PROFILE_EXIT
272 #define TAU_PROFILE_EXIT(msg) \
273  __tau_shutdown = true; \
274  Tau_exit(msg);
275 
276 #endif // USE_TAU
277 
278 #endif // __cplusplus
279 #endif
A base class for all things that want to be reference-counted.
This class provides a wrapper around the various possible malloc schemes Panda might employ...
Definition: memoryHook.h:43