osdep.h

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /** @file osdep.h Various os/compiler specific stuff */
00020 
00021 #ifndef __OSDEP_H__
00022 #define __OSDEP_H__
00023 
00024 #include <boost/format.hpp>
00025 
00026 #ifdef HAVE_CONFIG_H
00027         #include <config.h>
00028 #endif
00029 
00030 //! Ensure that both _WIN32 and WIN32 defined, if one of them is defined
00031 //! (needed when building on mingw32, where _WIN32 is defined, but WIN32 isn't)
00032 #if defined(_WIN32) && !defined(WIN32)
00033         #define WIN32
00034 #elif defined(WIN32) && !defined(_WIN32)
00035         #define WIN32
00036 #endif
00037 
00038 //! Client version information
00039 enum VersionInfo {
00040         APPVER_MAJOR = 0,
00041         APPVER_MINOR = 1,
00042         APPVER_PATCH = 0
00043 };
00044 
00045 // Exports/imports
00046 #ifdef WIN32
00047         #define EXPORT __declspec(dllexport)
00048         #define IMPORT __declspec(dllimport)
00049         #ifdef __MODULE__
00050                 #define DLLEXPORT IMPORT
00051                 #define DLLIMPORT EXPORT
00052         #else
00053                 #define DLLEXPORT EXPORT
00054                 #define DLLIMPORT IMPORT
00055         #endif
00056 #else
00057         #define EXPORT
00058         #define IMPORT
00059         #define DLLEXPORT
00060         #define DLLIMPORT
00061 #endif
00062 
00063 // Useless MSVC warnings
00064 #ifdef _MSC_VER
00065         // boost::thread exceptions don't compile with this enabled
00066         #pragma warning(disable:4275)
00067         // Another warning from boost::threads - we don't care about it
00068         #pragma warning(disable:4251)
00069         // Forcing int to bool
00070         #pragma warning(disable:4800)
00071         // conversion warnings - just too many of them
00072         #pragma warning(disable:4244)
00073         #pragma warning(disable:4267)
00074 #endif
00075 
00076 // integer types
00077 #ifdef _MSC_VER
00078         typedef unsigned char      uint8_t;
00079         typedef unsigned short     uint16_t;
00080         typedef unsigned int       uint32_t;
00081         typedef unsigned long long uint64_t;
00082         typedef signed char        int8_t;
00083         typedef signed short       int16_t;
00084         typedef signed int         int32_t;
00085         typedef signed long long   int64_t;
00086 #else
00087         #include <stdint.h>
00088 #endif
00089 
00090 // IO-related functions; slightly different on MSVC
00091 #ifdef WIN32
00092         #include <io.h>
00093         #define lseek64(fd, pos, dir) _lseeki64(fd, pos, dir)
00094         #define O_LARGEFILE 0
00095 #elif defined __MAC__ // Mac lacks lseek64, and O_LARGEFILE
00096         #define lseek64(fd, pos, dir) lseek(fd, pos, dir)
00097         #define O_LARGEFILE 0
00098 #endif
00099 
00100 #ifndef WIN32 // POSIX lacks O_BINARY
00101         #define O_BINARY 0
00102 #endif
00103 
00104 #ifndef __PRETTY_FUNCTION__ // MSVC doesn't have this
00105         #define __PRETTY_FUNCTION__ __FUNCTION__
00106 #endif
00107 
00108 #ifndef NDEBUG
00109 //! Debugging macro which logs an error if condition fails.
00110 #define CHECK(Cond)                                                       \
00111         if (!(Cond)) {                                                    \
00112                 logError(                                                 \
00113                         boost::format("%1%:%2%: Check `%3%' failed.")     \
00114                         % __FILE__ % __LINE__ % #Cond                     \
00115                 );                                                        \
00116         }
00117 //! Debugging macro which logs an error and message if condition fails
00118 #define CHECK_MSG(Cond, Message)                                          \
00119         if (!(Cond)) {                                                    \
00120                 logError(                                                 \
00121                         boost::format("%1%:%2%: Check `%3%' failed: %4%") \
00122                         % __FILE__ % __LINE__ % #Cond % Message           \
00123                 );                                                        \
00124         }
00125 #else // NDEBUG
00126         #define CHECK(Cond)
00127         #define CHECK_MSG(Cond, Message)
00128 #endif // NDEBUG/!NDEBUG
00129 
00130 //! Aborts the application with message if condition fails
00131 #ifdef __GNUC__
00132 #define CHECK_FAIL(Cond)                                                  \
00133         if (!(Cond)) {                                                    \
00134                 logFatalError(                                            \
00135                         boost::format("\n"                                \
00136                                 "who():   %1%\n"                          \
00137                                 "where(): %2%:%3%\n"                      \
00138                                 "what():  Check `%4%' failed."            \
00139                         )                                                 \
00140                         % __PRETTY_FUNCTION__ % __FILE__ % __LINE__ %#Cond\
00141                 );                                                        \
00142         }
00143 #else
00144 #define CHECK_FAIL(Cond)                                                  \
00145         if (!(Cond)) {                                                    \
00146                 logFatalError(                                            \
00147                         boost::format("\n"                                \
00148                                 "where(): %1%:%2%\n"                      \
00149                                 "what():  Check `%3%' failed."            \
00150                         )                                                 \
00151                         % __FILE__ % __LINE__ % #Cond                     \
00152                 );                                                        \
00153         }
00154 #endif
00155 
00156 //! Throws std::runtime_error if condition is true
00157 //! This is our replacement for assert() for situations where assert() is not
00158 //! appropriate.
00159 #define CHECK_THROW_MSG(Cond, Message)                                   \
00160         if (!(Cond)) {                                                   \
00161                 throw std::runtime_error(                                \
00162                         (boost::format("%1%:%2%: %3%")                   \
00163                         % __FILE__ % __LINE__ % (Message)).str()         \
00164                 );                                                       \
00165         }
00166 
00167 #define CHECK_THROW(Cond)                                                \
00168         if (!(Cond)) {                                                   \
00169                 throw std::runtime_error(                                \
00170                         (boost::format("%1%:%2%: Check `%3%' failed")    \
00171                         % __FILE__ % __LINE__ % #Cond).str()             \
00172                 );                                                       \
00173         }
00174 
00175 //! Checks condition and logs and error + returns from function if it fails.
00176 //! Second version of this macro returns a specific value from the function
00177 //! (for usage in functions that need to return a value)
00178 #ifndef NDEBUG
00179 #define CHECK_RET(Cond)                                                  \
00180         if (!(Cond)) {                                                   \
00181                 logError(                                                \
00182                         boost::format("%1%:%2%: Check `%3%' failed")     \
00183                         % __FILE__ % __LINE__ % #Cond                    \
00184                 );                                                       \
00185                 return;                                                  \
00186         }
00187 #define CHECK_RETVAL(Cond, RetVal)                                       \
00188         if (!(Cond)) {                                                   \
00189                 logError(                                                \
00190                         boost::format("%1%:%2%: Check `%3%' failed")     \
00191                         % __FILE__ % __LINE__ % #Cond                    \
00192                 );                                                       \
00193                 return (RetVal);                                         \
00194         }
00195 #else
00196 #define CHECK_RET(Cond)
00197 #define CHECK_RETVAL(Cond, RetVal)
00198 #endif
00199 
00200 
00201 /**
00202  * Ansi color codes. These can be used to colourize text output to terminal
00203  * or socket. However, since not all platforms, shells and so on support these,
00204  * they are currently disabled when NDEBUG is defined. In the future, the
00205  * check's whether to enable colors or not should be done on runtime (perhaps
00206  * using a commandline argument?).
00207  */
00208 #if defined(__ANSI_COLORS__)
00209         #define COL_NONE     "\33[0;0m"
00210         #define COL_BLACK    "\33[0;30m"
00211         #define COL_RED      "\33[0;31m"
00212         #define COL_BRED     "\33[1;31m"
00213         #define COL_GREEN    "\33[0;32m"
00214         #define COL_BGREEN   "\33[1;32m"
00215         #define COL_YELLOW   "\33[0;33m"
00216         #define COL_BYELLOW  "\33[1;33m"
00217         #define COL_BLUE     "\33[0;34m"
00218         #define COL_BBLUE    "\33[1;34m"
00219         #define COL_MAGENTA  "\33[0;35m"
00220         #define COL_BMAGENTA "\33[1;35m"
00221         #define COL_CYAN     "\33[0;36m"
00222         #define COL_BCYAN    "\33[1;36m"
00223         #define COL_WHITE    "\33[0;37m"
00224         #define COL_BWHITE   "\33[1;37m"
00225 #else
00226         #define COL_NONE
00227         #define COL_BLACK
00228         #define COL_RED
00229         #define COL_BRED
00230         #define COL_GREEN
00231         #define COL_BGREEN
00232         #define COL_YELLOW
00233         #define COL_BYELLOW
00234         #define COL_BLUE
00235         #define COL_BBLUE
00236         #define COL_MAGENTA
00237         #define COL_BMAGENTA
00238         #define COL_CYAN
00239         #define COL_BCYAN
00240         #define COL_WHITE
00241         #define COL_BWHITE
00242 #endif
00243 
00244 //! Compile code only when compiling with GNU GCC
00245 #ifdef __GNUC__
00246         #define GCC_ONLY(x) x
00247 #else
00248         #define GCC_ONLY(x)
00249 #endif
00250 
00251 //! Compile code only when compiling with Microsoft Visual C++
00252 #ifdef _MSC_VER
00253         #define MSVC_ONLY(x) x
00254 #else
00255         #define MSVC_ONLY(x)
00256 #endif
00257 
00258 #endif // __OSDEP_H__