src/main.h

changeset 73
1ee9b312dc18
parent 72
03e4d9db3fd9
child 75
bf8c57437231
equal deleted inserted replaced
72:03e4d9db3fd9 73:1ee9b312dc18
1 /*
2 Copyright (c) 2013-2014, Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 * Neither the name of the <organization> nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
1 #ifndef BOTC_COMMON_H 31 #ifndef BOTC_COMMON_H
2 #define BOTC_COMMON_H 32 #define BOTC_COMMON_H
3 33
4 #if !defined (__cplusplus) || __cplusplus < 201103L 34 #if !defined (__cplusplus) || __cplusplus < 201103L
5 # error botc requires a C++11-compliant compiler to be built 35 # error botc requires a C++11-compliant compiler to be built
11 #include "types.h" 41 #include "types.h"
12 #include "bots.h" 42 #include "bots.h"
13 #include "str.h" 43 #include "str.h"
14 #include "containers.h" 44 #include "containers.h"
15 #include "format.h" 45 #include "format.h"
46 #include "tokens.h"
16 47
17 // Application name and version 48 // Application name and version
18 #define APPNAME "botc" 49 #define APPNAME "botc"
19 #define VERSION_MAJOR 0 50 #define VERSION_MAJOR 0
20 #define VERSION_MINOR 999 51 #define VERSION_MINOR 999
47 if (!pointer) { \ 78 if (!pointer) { \
48 error ("couldn't open %s for %s!\n", path.chars(), action); \ 79 error ("couldn't open %s for %s!\n", path.chars(), action); \
49 exit (1); \ 80 exit (1); \
50 } 81 }
51 82
52 // Plural expression 83 #define types public
53 #define PLURAL(n) (n != 1) ? "s" : "" 84 #define countof(A) ((int) (sizeof A / sizeof *A))
85 #define autocast(A) (decltype(A))
86 #define assign_autocast(A,B) a = autocast(A) b
54 87
55 // Shortcut for zeroing something 88 // Shortcut for zeroing something
56 #define ZERO(obj) memset (&obj, 0, sizeof (obj)); 89 #define ZERO(obj) memset (&obj, 0, sizeof (obj));
57 90
58 void error (const char* text, ...);
59 string ObjectFileName (string s); 91 string ObjectFileName (string s);
60 bool fexists (string path); 92 bool fexists (string path);
61 type_e GetTypeByName (string token); 93 type_e GetTypeByName (string token);
62 string GetTypeName (type_e type); 94 string GetTypeName (type_e type);
63 95
74 #define __attribute__(X) 106 #define __attribute__(X)
75 #endif 107 #endif
76 #define deprecated __attribute__ ((deprecated)) 108 #define deprecated __attribute__ ((deprecated))
77 109
78 // Power function 110 // Power function
79 template<class T> T pow (T a, unsigned int b) { 111 template<class T> T pow (T a, int b) {
80 if (!b) 112 if (!b)
81 return 1; 113 return 1;
82 114
83 T r = a; 115 T r = a;
84 while (b > 1) { 116 while (b > 1) {
95 127
96 // Keywords 128 // Keywords
97 extern const char** g_Keywords; 129 extern const char** g_Keywords;
98 130
99 bool IsKeyword (string s); 131 bool IsKeyword (string s);
100 unsigned int NumKeywords (); 132 int NumKeywords ();
101 133
102 // Script mark and reference 134 // Script mark and reference
103 struct ScriptMark { 135 struct ScriptMark {
104 string name; 136 string name;
105 size_t pos; 137 size_t pos;
106 }; 138 };
107 139
108 struct ScriptMarkReference { 140 struct ScriptMarkReference {
109 unsigned int num; 141 int num;
110 size_t pos; 142 size_t pos;
111 }; 143 };
112 144
113 // ==================================================================== 145 // ====================================================================
114 // Generic union 146 // Generic union
115 template <class T> union union_t { 147 template <class T> union generic_union {
116 T val; 148 T as_t;
117 byte b[sizeof (T)]; 149 byte as_bytes[sizeof (T)];
118 char c[sizeof (T)]; 150 char as_chars[sizeof (T)];
119 double d; 151 double as_double;
120 float f; 152 float as_float;
121 int i; 153 int as_int;
122 word w; 154 word as_word;
123 }; 155 };
124 156
125 // ==================================================================== 157 // ====================================================================
126 // Finds a byte in the given value. 158 // Finds a byte in the given value.
127 template <class T> inline unsigned char GetByteIndex (T a, unsigned int b) { 159 template <class T> inline unsigned char GetByteIndex (T a, int b) {
128 union_t<T> uni; 160 union_t<T> uni;
129 uni.val = a; 161 uni.val = a;
130 return uni.b[b]; 162 return uni.b[b];
131 } 163 }
132 164

mercurial