00001
00002
00005
00014
00016
00048
00051
00052
00053
00054
00055
00056 #ifdef UTILITYMACROS_H
00057 # error Multiple #included file!
00058 #endif
00059 #define UTILITYMACROS_H
00060
00061
00062
00063
00064
00070 #define REPEAT_2(x) x x
00071 #define REPEAT_4(x) REPEAT_2 (x) REPEAT_2 (x)
00072 #define REPEAT_8(x) REPEAT_4 (x) REPEAT_4 (x)
00073 #define REPEAT_16(x) REPEAT_8 (x) REPEAT_8 (x)
00074 #define REPEAT_32(x) REPEAT_16 (x) REPEAT_16 (x)
00075 #define REPEAT_64(x) REPEAT_32 (x) REPEAT_32 (x)
00076 #define REPEAT_128(x) REPEAT_64 (x) REPEAT_64 (x)
00077 #define REPEAT_256(x) REPEAT_128 (x) REPEAT_128 (x)
00078 #define REPEAT_512(x) REPEAT_256 (x) REPEAT_256 (x)
00079 #define REPEAT_1024(x) REPEAT_512 (x) REPEAT_512 (x)
00080
00081
00087 #define REPEAT(count, what) REPEAT_##count (what)
00088
00089
00095 #define REPEAT_WC_2(x) x, x
00096 #define REPEAT_WC_4(x) REPEAT_WC_2 (x), REPEAT_WC_2 (x)
00097 #define REPEAT_WC_8(x) REPEAT_WC_4 (x), REPEAT_WC_4 (x)
00098 #define REPEAT_WC_16(x) REPEAT_WC_8 (x), REPEAT_WC_8 (x)
00099 #define REPEAT_WC_32(x) REPEAT_WC_16 (x), REPEAT_WC_16 (x)
00100 #define REPEAT_WC_64(x) REPEAT_WC_32 (x), REPEAT_WC_32 (x)
00101 #define REPEAT_WC_128(x) REPEAT_WC_64 (x), REPEAT_WC_64 (x)
00102 #define REPEAT_WC_256(x) REPEAT_WC_128 (x), REPEAT_WC_128 (x)
00103 #define REPEAT_WC_512(x) REPEAT_WC_256 (x), REPEAT_WC_256 (x)
00104 #define REPEAT_WC_1024(x) REPEAT_WC_512 (x), REPEAT_WC_512 (x)
00105
00106
00112 #define REPEAT_WC(count, text) REPEAT_WC_##count (text)
00113
00114
00121 #define UNIQUE_NAME_1(prefix, x) prefix##x
00122 #define UNIQUE_NAME_2(prefix, x) UNIQUE_NAME_1 (prefix, x)
00123
00124 #define UNIQUE_NAME_WP(prefix) UNIQUE_NAME_2 (prefix, __LINE__)
00125
00126 #define UNIQUE_NAME UNIQUE_NAME_WP (uniqueNameOnLine_)
00127
00128
00134 #define LINE_STRING_1(x) #x
00135 #define LINE_STRING_2(x) LINE_STRING_1 (x)
00136
00137 #define LINE_STRING LINE_STRING_2 (__LINE__)
00138
00139
00148 #define HERE __FILE__ "(" LINE_STRING "):"
00149
00150
00157 #define ONCE(execution_code) \
00158 { \
00159 static int UNIQUE_NAME = 0; \
00160 if( !UNIQUE_NAME ) \
00161 { \
00162 UNIQUE_NAME = 1; \
00163 { execution_code } \
00164 } \
00165 }
00166
00167
00173 #define SKIP(skip_count, execution_code) \
00174 { \
00175 static int UNIQUE_NAME = 0; \
00176 if( ++UNIQUE_NAME >= (skip_count) ) \
00177 { \
00178 UNIQUE_NAME = 0; \
00179 { execution_code } \
00180 } \
00181 }
00182
00183
00189 #define REV_SKIP(skip_count, execution_code) \
00190 { \
00191 static int UNIQUE_NAME = 0; \
00192 if( ++UNIQUE_NAME >= (skip_count) ) \
00193 UNIQUE_NAME = 0; \
00194 else \
00195 { \
00196 execution_code \
00197 } \
00198 }
00199
00200
00206 #define LOOP_C(count, loop_code) \
00207 \
00208 { \
00209 int UNIQUE_NAME = (count); \
00210 for(; UNIQUE_NAME > 0; --UNIQUE_NAME) \
00211 { loop_code } \
00212 }
00213
00214
00224 #define LOOP(count) for(int UNIQUE_NAME = (count); \
00225 UNIQUE_NAME > 0; \
00226 --UNIQUE_NAME)
00227
00228
00229
00236 #define AT_START(execution_code) \
00237 \
00238 static struct t_UNIQUE_NAME \
00239 { \
00240 UNIQUE_NAME() \
00241 { \
00242 execution_code \
00243 } \
00244 } \
00245 UNIQUE_NAME_WP (atStartVarOnLine_);
00246
00247
00254 #define AT_END(execution_code) \
00255 \
00256 static struct t_UNIQUE_NAME \
00257 { \
00258 ~UNIQUE_NAME() \
00259 { \
00260 execution_code \
00261 } \
00262 } \
00263 UNIQUE_NAME_WP (atStartVarOnLine_);
00264
00265
00270 class DelayedAssigner_T_Base
00271 {
00272 public:
00273 virtual ~DelayedAssigner_T_Base()
00274 {}
00275 };
00276
00277
00284 template <class Type>
00285 class DelayedAssigner_T : public DelayedAssigner_T_Base
00286 {
00287 Type& itsVarRef;
00288 Type itsValue;
00289
00290 public:
00291 DelayedAssigner_T (Type &var_ref)
00292 : itsVarRef (var_ref)
00293 , itsValue (var_ref)
00294 {}
00295
00296 DelayedAssigner_T (Type &var_ref, const Type &value)
00297 : itsVarRef (var_ref)
00298 , itsValue (value)
00299 {}
00300
00301 ~DelayedAssigner_T()
00302 {
00303 itsVarRef = itsValue;
00304 }
00305 };
00306
00307
00313 #define DELAYED_ASSIGN_T(Type, var_ref, value) \
00314 \
00315 DelayedAssigner_T<Type> UNIQUE_NAME (var_ref, value);
00316
00317
00323 #define SAVE_T(Type, var_ref) \
00324 \
00325 DelayedAssigner_T<Type> UNIQUE_NAME (var_ref);
00326
00327
00334 template <class Type> inline
00335 DelayedAssigner_T<Type> makeDelayedAssigner_T (Type &var)
00336 {
00337 return DelayedAssigner_T<Type> (var);
00338 }
00339
00346 template <class Type> inline
00347 DelayedAssigner_T <Type>
00348 makeDelayedAssigner_T (Type &var, const Type& value)
00349 {
00350 return DelayedAssigner_T<Type> (var, value);
00351 }
00352
00353
00361 template <class Type> inline
00362 DelayedAssigner_T_Base* newDelayedAssigner_T (Type &var)
00363 {
00364 return new DelayedAssigner_T<Type> (var);
00365 }
00366
00367
00376 template <class Base, size_t SIZE>
00377 class UnivesalStorage_T
00378 {
00379 char itsStorage [SIZE];
00380 public:
00381
00382 template <class From>
00383 UnivesalStorage_T (const From& from)
00384 {
00385
00386 typedef char assertSize [sizeof (from) == SIZE];
00387
00388 if(const Base* assertInheritence = &from)
00389 (void)0;
00390
00391 new (itsStorage) From (from);
00392 }
00393
00394 ~UnivesalStorage_T()
00395 {
00396 ((Base*)itsStorage)->~Base();
00397 }
00398 };
00399
00400
00406 #define SAVE(var_ref) \
00407 \
00408 UnivesalStorage_T \
00409 < DelayedAssigner_T_Base, \
00410 sizeof (makeDelayedAssigner_T (var_ref)) > \
00411 UNIQUE_NAME (makeDelayedAssigner_T (var_ref));
00412
00413
00419 #define DELAYED_ASSIGN(var_ref, value) \
00420 \
00421 UnivesalStorage_T \
00422 < DelayedAssigner_T_Base, \
00423 sizeof (makeDelayedAssigner_T (var_ref)) > \
00424 UNIQUE_NAME (makeDelayedAssigner_T ((var_ref), (value)));
00425
00426
00427