GeoGen Development snapshot a3

ggen_support.h

Go to the documentation of this file.
00001 /*
00002 
00003     This file is part of GeoGen.
00004 
00005     GeoGen is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010           GeoGen is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with GeoGen.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 */
00019 
00020 #ifndef GGEN_SUPPORT
00021 #define GGEN_SUPPORT
00022 
00023 #include "math.h"
00024 #include <string>
00025 #include <cstdlib>
00026 #include <limits.h>
00027 
00032 // hide stupid "sprintf is deprecated function, use our better alternative" MSVS warnings
00033 #define _CRT_SECURE_NO_WARNINGS
00034 #pragma warning(disable:4996)
00035 
00036 using namespace std;
00037 
00038 /* Define DLL export macro */
00039 #ifdef _USRDLL
00040           #define GGEN_EXPORT __declspec(dllexport)
00041 #else
00042           #define GGEN_EXPORT  
00043 #endif
00044 
00045 typedef signed char int8;
00046 typedef unsigned char uint8;
00047 typedef signed short int16;
00048 typedef unsigned short uint16;
00049 typedef signed int int32;
00050 typedef unsigned int uint32;
00051 typedef signed long long int64;
00052 typedef unsigned long long uint64;
00053 
00054 /* Type definitions. */
00055 
00059 typedef int16 GGen_Height;
00060 
00064 typedef int32 GGen_ExtHeight;
00065 
00069 typedef int64 GGen_ExtExtHeight;
00070 
00075 typedef uint16 GGen_Size;
00076 
00080 typedef uint32 GGen_TotalSize; /* Total count of elements in the data array, must be able to hold at least GGen_Size * GGen_Size. Must not allow negative values. */
00081 
00086 typedef uint16 GGen_Coord;
00087 
00092 typedef int32 GGen_CoordOffset;
00093 
00097 typedef GGen_TotalSize GGen_Index;
00098 
00103 typedef uint32 GGen_Distance;
00104 
00108 #define GGEN_INVALID_HEIGHT -32768
00109 
00113 #define GGEN_MIN_HEIGHT -32767
00114 
00118 #define GGEN_MAX_HEIGHT 32767
00119 
00123 #define GGEN_MIN_MAP_SIZE 2
00124 
00128 #define GGEN_MAX_PATH_LENGTH 5000
00129 
00130 #define GGEN_UNRELATIVE_CAP GGEN_MAX_HEIGHT
00131 
00132 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 
00133 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 
00134 #define ABS(a) ((a) < 0 ? -(a): (a)) 
00135 
00136 #ifdef _UNICODE
00137           #ifndef GGEN_UNICODE 
00138                     #define GGEN_UNICODE
00139           #endif
00140 #endif
00141 
00142 #ifdef GGEN_UNICODE
00143           typedef wchar_t GGen_Char;
00144           #define WIDEN(x) L ## x
00145           #define GGen_Const_String(a) WIDEN(a)
00146           #define GGen_Cin              wcin
00147           #define GGen_Cout             wcout
00148           #define   GGen_Strcmp                   wcscmp
00149           #define GGen_Sprintf          swprintf
00150           #define GGen_Strlen           wcslen
00151           #define GGen_Strtod           wcstod
00152           #define GGen_Strtol           wcstol
00153           #define GGen_Atoi             _wtoi
00154           #define GGen_Strtoul          wcstoul
00155           #define GGen_Vsprintf         vswprintf
00156           #define GGen_Strstr           wcsstr
00157           #define GGen_Isspace          iswspace
00158           #define GGen_Isdigit          iswdigit
00159           #define GGen_Isxdigit         iswxdigit
00160           #define GGen_Isalpha          iswalpha
00161           #define GGen_Iscntrl          iswcntrl
00162           #define GGen_Isalnum          iswalnum
00163           #define GGen_Printf           wprintf
00164 #else
00165           typedef char GGen_Char;
00166           #define GGen_Const_String(a) a
00167           #define GGen_Cin              cin       
00168           #define GGen_Cout             cout
00169           #define   GGen_Strcmp                   strcmp
00170           #define GGen_Sprintf          sprintf
00171           #define GGen_Strlen           strlen
00172           #define GGen_Strtod           strtod
00173           #define GGen_Strtol           strtol
00174           #define GGen_Atoi             atoi
00175           #define GGen_Strtoul          strtoul
00176           #define GGen_Vsprintf         vsprintf
00177           #define GGen_Strstr           strstr
00178           #define GGen_Isspace          isspace
00179           #define GGen_Isdigit          isdigit
00180           #define GGen_Isxdigit         isxdigit
00181           #define GGen_Isalpha          iscntrl
00182           #define GGen_Iscntrl          isalpha
00183           #define GGen_Isalnum          isalnum
00184           #define GGen_Printf           printf
00185 #endif 
00186 
00187 typedef GGEN_EXPORT basic_string<GGen_Char> GGen_String;
00188 
00189 struct GGen_ScriptAssertException{};
00190 
00191 // Custom assertion handler. Invoke messaage callback and shut down the script execution.
00192 // GCC can't convert the __FUNCTION__ macro to unicode property using the L## syntax -> it has to be done on runtime
00193 #ifdef GGEN_UNICODE
00194           #define GGen_Script_Assert(_Expression) {if(!(_Expression)) {\
00195                     GGen_String as_buf; \
00196                     as_buf += GGen_Const_String("Assertion in function "); \
00197                     int as_len = strlen(__FUNCTION__);\
00198                     GGen_Char* as_buf2 = new GGen_Char[as_len + 1];\
00199                     mbstowcs(as_buf2, __FUNCTION__, as_len);\
00200                     as_buf2[as_len] = GGen_Const_String('\0');\
00201                     as_buf += as_buf2;\
00202                     delete [] as_buf2;\
00203                     as_buf += GGen_Const_String(" failed: "); \
00204                     as_buf += GGen_Const_String(#_Expression); \
00205                     GGen::GetInstance()->ThrowMessage(as_buf, GGEN_ERROR, -1); \
00206                     throw GGen_ScriptAssertException(); \
00207                     }}
00208 #else
00209           #define GGen_Script_Assert(_Expression) {if(!(_Expression)) {\
00210                     GGen_String as_buf; \
00211                     as_buf += GGen_Const_String("Assertion in function "); \
00212                     as_buf += GGen_Const_String(__FUNCTION__); \
00213                     as_buf += GGen_Const_String(" failed: "); \
00214                     as_buf += GGen_Const_String(#_Expression); \
00215                     GGen::GetInstance()->ThrowMessage(as_buf, GGEN_ERROR, -1);\
00216                     throw GGen_ScriptAssertException(); \
00217                     }}
00218 #endif
00219 
00223 enum GGen_Normalization_Mode{
00224           GGEN_ADDITIVE, 
00225           GGEN_SUBSTRACTIVE 
00226 };
00227 
00231 enum GGen_Overflow_Mode{
00232           GGEN_CYCLE, 
00233           GGEN_DISCARD,  
00234           GGEN_DISCARD_AND_FILL  
00235 };
00236 
00240 enum GGen_Direction{
00241           GGEN_HORIZONTAL, 
00242           GGEN_VERTICAL, 
00243 };
00244 
00245 enum GGen_Message_Level{
00246           GGEN_MESSAGE = 0,
00247           GGEN_NOTICE = 1,
00248           GGEN_WARNING = 2,
00249           GGEN_ERROR = 3
00250 };
00251 
00255 enum GGen_Arg_Type{
00256           GGEN_BOOL, 
00257           GGEN_INT, 
00258           GGEN_ENUM 
00259 };
00260 
00264 enum GGen_Voronoi_Noise_Mode{
00265           GGEN_RIDGES, 
00266           GGEN_BUBBLES 
00267 };
00268 
00272 enum GGen_Comparison_Mode{
00273           GGEN_EQUAL_TO, 
00274           GGEN_NOT_EQUAL_TO, 
00275           GGEN_LESS_THAN, 
00276           GGEN_GREATER_THAN, 
00277           GGEN_LESS_THAN_OR_EQUAL_TO, 
00278           GGEN_GREATER_THAN_OR_EQUAL_TO, 
00279 };
00280 
00284 enum GGen_Outline_Mode{
00285           GGEN_INSIDE, 
00286           GGEN_OUTSIDE 
00287 };
00288 
00292 enum GGen_Status{ 
00293           GGEN_NO_SCRIPT, 
00294           GGEN_SCRIPT_LOADED, 
00295           GGEN_READY_TO_GENERATE, 
00296           GGEN_LOADING_MAP_INFO, 
00297           GGEN_GENERATING, 
00298 };
00299 
00300 template <class T>
00301 T GGen_Random(T min, T max){
00302           double random = (double)rand() / (double) RAND_MAX;
00303           T output = min + (T) (random * (double)(max - min));
00304           return output;
00305 }
00306 
00307 inline int GGen_log2(int x){
00308           static double base = log10((double) 2);
00309           return (int16) (log10((double) x)/ base);
00310 }
00311 
00312 #endif