Move "loop" related tests in their own dir. This is just to break the ice... ideally...
[gnash.git] / libbase / utility.h
blobf2e1d3d55369133d4cd3695139f26c7eadc1c338
1 // utility.h -- Various little utility functions, macros & typedefs.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef GNASH_UTILITY_H
21 #define GNASH_UTILITY_H
23 // HAVE_FINITE, HAVE_PTHREADS, WIN32, NDEBUG etc.
24 #ifdef HAVE_CONFIG_H
25 #include "gnashconfig.h"
26 #endif
28 #include <cstdlib>
29 #include <cassert>
30 #include <cstring>
31 #include <string>
32 #include <typeinfo>
34 #ifdef HAVE_PTHREADS
35 #include <pthread.h>
36 #endif
38 #if defined(__GNUC__) && __GNUC__ > 2
39 # include <cxxabi.h>
40 #endif
42 #if defined(_WIN32) || defined(WIN32)
43 #ifndef NDEBUG
45 // On windows, replace ANSI assert with our own, for a less annoying
46 // debugging experience.
47 #ifndef __MINGW32__
48 #undef assert
49 #define assert(x) if (!(x)) { __asm { int 3 } }
50 #endif
51 #endif // not NDEBUG
52 #endif // _WIN32
54 #ifdef __amigaos4__
55 #include <stdio.h> //for FILE * in tu_file.h
56 #include <fcntl.h> //for fcntl in Socket.cpp
57 #include <netdb.h> //for hostent in Socket.cpp
58 #include <netinet/tcp.h> //for TCP_NODELAY in Socket.cpp
59 #undef UNUSED //to avoid "already defined" messages
60 #define SHUT_RDWR 0
62 namespace std
64 typedef std::basic_string<wchar_t> wstring;
66 #endif
68 #if defined(__HAIKU__)
69 namespace std {
70 class wstring : public std::basic_string<char>
72 public:
73 wstring(const char *t)
74 : std::basic_string<char>(t)
77 wstring()
80 wstring(const wstring &that)
81 : std::basic_string<char>(that.c_str())
84 wstring(const std::basic_string<char> &that)
85 : std::basic_string<char>(that)
90 #endif
92 namespace gnash {
94 /// Return (unmangled) name of this instance type. Used for
95 /// logging in various places.
96 template <class T>
97 std::string typeName(const T& inst)
99 std::string typeName = typeid(inst).name();
100 #if defined(__GNUC__) && __GNUC__ > 2
101 int status;
102 char* typeNameUnmangled =
103 abi::__cxa_demangle (typeName.c_str(), NULL, NULL,
104 &status);
105 if (status == 0)
107 typeName = typeNameUnmangled;
108 std::free(typeNameUnmangled);
110 #endif // __GNUC__ > 2
111 return typeName;
114 /// Used in logging.
115 #ifdef HAVE_PTHREADS
116 #else
117 # ifdef _WIN32
118 } // end namespace gnash
119 extern "C" unsigned long int /* DWORD WINAPI */ GetCurrentThreadId(void);
120 namespace gnash {
121 # else
122 /* getpid() */
123 #include <sys/types.h>
124 #include <unistd.h>
125 # endif
126 #endif
128 inline unsigned long int /* pthread_t */ get_thread_id(void)
130 #ifdef HAVE_PTHREADS
131 # ifdef __APPLE_CC__
132 return reinterpret_cast<unsigned long int>(pthread_self());
133 # else
134 // This isn't a proper style C++ cast, but FreeBSD has a problem with
135 // static_cast for this as pthread_self() returns a pointer. We can
136 // use that too, this ID is only used for the log file to keep output
137 // from seperare threads clear.
138 # ifdef _WIN32
139 return GetCurrentThreadId();
140 #else
141 return (unsigned long int)pthread_self();
142 #endif
143 # endif
144 #else
145 # ifdef _WIN32
146 return GetCurrentThreadId();
147 # else
148 return static_cast<unsigned long int>(getpid());
149 # endif
150 #endif
153 } // namespace gnash
155 // Handy macro to quiet compiler warnings about unused parameters/variables.
156 #define UNUSED(x) (x) = (x)
158 #endif // _GNASH_UTILITY_H
161 // Local Variables:
162 // mode: C++
163 // c-basic-offset: 8
164 // tab-width: 8
165 // indent-tabs-mode: t
166 // End: