00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #ifndef W_BASE_H
00054 #define W_BASE_H
00055
00056 #include "w_defines.h"
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #ifdef ON
00073 #undef ON
00074 #endif
00075
00076 #ifdef OFF
00077 #undef OFF
00078 #endif
00079
00080
00081
00082 #ifdef __GNUG__
00083 #pragma interface
00084 #endif
00085
00086 #include <w_stream.h>
00087
00088 #ifndef W_WORKAROUND_H
00089 #include "w_workaround.h"
00090 #endif
00091
00092 #define NORET
00093 #define CAST(t,o) ((t)(o))
00094 #define W_UNUSED(x)
00095
00096
00097 #if W_DEBUG_LEVEL>0
00098 #define W_IFDEBUG1(x) x
00099 #define W_IFNDEBUG1(x)
00100 #else
00101 #define W_IFDEBUG1(x)
00102 #define W_IFNDEBUG1(x) x
00103 #endif
00104
00105 #if W_DEBUG_LEVEL>1
00106 #define W_IFDEBUG2(x) x
00107 #define W_IFNDEBUG2(x)
00108 #else
00109 #define W_IFDEBUG2(x)
00110 #define W_IFNDEBUG2(x) x
00111 #endif
00112
00113 #if W_DEBUG_LEVEL>2
00114 #define W_IFDEBUG3(x) x
00115 #define W_IFNDEBUG3(x)
00116 #else
00117 #define W_IFDEBUG3(x)
00118 #define W_IFNDEBUG3(x) x
00119 #endif
00120
00121 #if W_DEBUG_LEVEL>3
00122 #define W_IFDEBUG4(x) x
00123 #define W_IFNDEBUG4(x)
00124 #else
00125 #define W_IFDEBUG4(x)
00126 #define W_IFNDEBUG4(x) x
00127 #endif
00128
00129 #define W_IFDEBUG9(x)
00130 #define W_IFNDEBUG9(x) x
00131
00132
00133 #undef W_IFDEBUG
00134 #undef W_IFNDEBUG
00135 #if W_DEBUG_LEVEL==1
00136 #define W_IFDEBUG(x) W_IFDEBUG1(x)
00137 #define W_IFNDEBUG(x) W_IFNDEBUG1(x)
00138 #endif
00139
00140 #if W_DEBUG_LEVEL==2
00141 #define W_IFDEBUG(x) W_IFDEBUG2(x)
00142 #define W_IFNDEBUG(x) W_IFNDEBUG2(x)
00143 #endif
00144
00145 #if W_DEBUG_LEVEL==3
00146 #define W_IFDEBUG(x) W_IFDEBUG3(x)
00147 #define W_IFNDEBUG(x) W_IFNDEBUG3(x)
00148 #endif
00149
00150 #if W_DEBUG_LEVEL==4
00151 #define W_IFDEBUG(x) W_IFDEBUG4(x)
00152 #define W_IFNDEBUG(x) W_IFNDEBUG4(x)
00153 #endif
00154
00155 #ifndef W_IFDEBUG
00156 #define W_IFDEBUG(x)
00157 #endif
00158 #ifndef W_IFNDEBUG
00159 #define W_IFNDEBUG(x) x
00160 #endif
00161
00162
00163
00164 #ifdef W_TRACE
00165 #define W_IFTRACE(x) x
00166 #define W_IFNTRACE(x)
00167 #else
00168 #define W_IFTRACE(x)
00169 #define W_IFNTRACE(x) x
00170 #endif
00171
00172
00173 #define w_assert0(x) do { \
00174 if (!(x)) w_base_t::assert_failed(#x, __FILE__, __LINE__); \
00175 } while(0)
00176
00177 #ifndef W_DEBUG_LEVEL
00178 #define W_DEBUG_LEVEL 0
00179 #endif
00180
00181
00182 #if W_DEBUG_LEVEL>=1
00183 #define w_assert1(x) w_assert0(x)
00184 #else
00185 #define w_assert1(x)
00186 #endif
00187
00188
00189 #if W_DEBUG_LEVEL>=2
00190 #define w_assert2(x) w_assert1(x)
00191 #else
00192 #define w_assert2(x)
00193 #endif
00194
00195
00196 #if W_DEBUG_LEVEL>=3
00197 #define w_assert3(x) w_assert1(x)
00198 #else
00199 #define w_assert3(x)
00200 #endif
00201
00202
00203 #if W_DEBUG_LEVEL>=4
00204 #define w_assert4 w_assert1(x)
00205 #else
00206 #define w_assert4(x)
00207 #endif
00208
00209
00210 #if W_DEBUG_LEVEL>=5
00211 #define w_assert5 w_assert1(x)
00212 #else
00213 #define w_assert5(x)
00214 #endif
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 #define w_assert9(x)
00226
00227
00228
00229
00230
00231
00232
00233
00234 #define W_ENUM(x) ((int)(x))
00235
00236
00237
00238
00239
00240
00241
00242
00243 #define W_ADDR(x) ((void *)(x))
00244
00245 class w_rc_t;
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 class w_base_t {
00256 public:
00257
00258
00259
00260 typedef unsigned char u_char;
00261 typedef unsigned short u_short;
00262 typedef unsigned long u_long;
00263
00264
00265
00266
00267
00268 typedef char int1_t;
00269 typedef u_char uint1_t;
00270 typedef short int2_t;
00271 typedef u_short uint2_t;
00272 typedef int int4_t;
00273 typedef u_int uint4_t;
00274
00275 #if defined(ARCH_LP64)
00276 typedef long int8_t;
00277 typedef unsigned long uint8_t;
00278 #elif defined(__GNUG__)
00279 typedef long long int8_t;
00280 typedef unsigned long long uint8_t;
00281 #else
00282 #error int8_t Not supported for this compiler.
00283 #endif
00284
00285
00286
00287
00288 typedef uint8_t large_stat_t;
00289
00290
00291
00292
00293
00294
00295
00296 #if defined(LARGEFILE_AWARE) || defined(ARCH_LP64)
00297 typedef int8_t base_stat_t;
00298 typedef double base_float_t;
00299 #else
00300 typedef int4_t base_stat_t;
00301 typedef float base_float_t;
00302 #endif
00303
00304 typedef float f4_t;
00305 typedef double f8_t;
00306
00307 static const int1_t int1_max, int1_min;
00308 static const int2_t int2_max, int2_min;
00309 static const int4_t int4_max, int4_min;
00310 static const int8_t int8_max, int8_min;
00311
00312 static const uint1_t uint1_max, uint1_min;
00313 static const uint2_t uint2_max, uint2_min;
00314 static const uint4_t uint4_max, uint4_min;
00315 static const uint8_t uint8_max, uint8_min;
00316
00317
00318
00319
00320
00321
00322 #define alignonarg(a) (((ptrdiff_t)(a))-1)
00323
00324 #define alignon(p,a) (((ptrdiff_t)((ptrdiff_t)(p) + alignonarg(a))) & ~alignonarg(a))
00325
00326
00327
00328
00329
00330
00331
00332
00333 # ifndef align
00334 # define ALIGNON 0x8
00335 # define ALIGNON1 (ALIGNON-1)
00336 # define align(sz) ((size_t)((sz + ALIGNON1) & ~ALIGNON1))
00337 # endif
00338 static bool is_aligned(size_t sz);
00339 static bool is_aligned(const void* s);
00340
00341 static bool is_big_endian();
00342 static bool is_little_endian();
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 static int8_t strtoi8(const char *, char ** end=0 , int base=0);
00356
00357
00358
00359
00360
00361
00362 static uint8_t strtou8(const char *, char ** end=0, int base=0);
00363
00364
00365 static istream& _scan_uint8(istream& i, uint8_t &,
00366 bool chew_white,
00367 bool is_signed,
00368 bool& rangerr);
00369
00370 static bool is_finite(const f8_t x);
00371 static bool is_infinite(const f8_t x);
00372 static bool is_nan(const f8_t x);
00373 static bool is_infinite_or_nan(const f8_t x);
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 static uint2_t w_ntohs(uint2_t);
00384 static uint2_t w_htons(uint2_t);
00385 static uint4_t w_ntohl(uint4_t);
00386 static uint4_t w_htonl(uint4_t);
00387
00388
00389 friend ostream& operator<<(
00390 ostream& o,
00391 const w_base_t& obj);
00392
00393
00394 static void assert_failed(
00395 const char* desc,
00396 const char* file,
00397 uint4_t line);
00398
00399
00400 static void abort();
00401
00402
00403
00404
00405 enum CompareOp {
00406 badOp=0x0, eqOp=0x1, gtOp=0x2, geOp=0x3, ltOp=0x4, leOp=0x5,
00407
00408 NegInf=0x100, eqNegInf, gtNegInf, geNegInf, ltNegInf, leNegInf,
00409 PosInf=0x400, eqPosInf, gtPosInf, gePosInf, ltPosInf, lePosInf
00410 };
00411
00412
00413
00414
00415
00416
00417
00418 enum lock_mode_t {
00419 NL = 0,
00420 IS,
00421 IX,
00422 SH,
00423 SIX,
00424 UD,
00425 EX
00426 };
00427
00428
00429
00430
00431
00432 enum lock_duration_t {
00433 t_instant = 0,
00434 t_short = 1,
00435 t_medium = 2,
00436 t_long = 3,
00437 t_very_long = 4,
00438 t_num_durations = 5
00439 };
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 enum vote_t {
00451 vote_bad,
00452 vote_readonly,
00453 vote_abort,
00454 vote_commit
00455 };
00456 };
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 #if defined (__SUNPRO_CC)
00472 #define w_reset_strstream(s) \
00473 do { \
00474 s.clear(); \
00475 s.seekp(0); \
00476 } while(0)
00477 #else
00478 #define w_reset_strstream(s) \
00479 do { \
00480 s.clear(); \
00481 s.seekp(ios::beg); \
00482 } while (0)
00483 #endif
00484
00485
00486
00487
00488
00489 inline bool
00490 w_base_t::is_aligned(size_t sz)
00491 {
00492 return (align(sz) == sz);
00493 }
00494
00495 inline bool
00496 w_base_t::is_aligned(const void* s)
00497 {
00498
00499
00500 return is_aligned((ptrdiff_t)(s));
00501 }
00502
00503
00504
00505
00506 inline bool w_base_t::is_big_endian()
00507 {
00508 #ifdef WORDS_BIGENDIAN
00509 return true;
00510 #else
00511 return false;
00512 #endif
00513 }
00514
00515
00516
00517
00518 inline bool
00519 w_base_t::is_little_endian()
00520 {
00521 return ! is_big_endian();
00522 }
00523
00524
00525
00526 class w_vbase_t : public w_base_t {
00527 public:
00528 NORET w_vbase_t() {};
00529 virtual NORET ~w_vbase_t() {};
00530 };
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 struct fill1 {
00543 w_base_t::uint1_t u1;
00544 fill1() : u1(0) {}
00545 };
00546
00547
00548 struct fill2 {
00549 w_base_t::uint2_t u2;
00550 fill2() : u2(0) {}
00551 };
00552
00553
00554 struct fill3 {
00555 w_base_t::uint1_t u1[3];
00556 fill3() { u1[0] = u1[1] = u1[2] = 0; }
00557 };
00558
00559
00560 struct fill4 {
00561 w_base_t::uint4_t u4;
00562 fill4() : u4(0) {}
00563 };
00564
00565
00566 #include <w_autodel.h>
00567 #include <w_error.h>
00568 #include <w_rc.h>
00569
00570 template<bool B> struct CompileTimeAssertion;
00571
00572
00573
00574
00575 template<> struct CompileTimeAssertion<true> { void reference() {} };
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594 template<typename T> struct compile_time_assert
00595 {
00596 compile_time_assert() {
00597 CompileTimeAssertion<sizeof(long) == 8> assert_8byte_long;
00598 CompileTimeAssertion<sizeof(long) >= sizeof(T)> assert_long_holds_T;
00599 }
00600 };
00601
00602 #if W_DEBUG_LEVEL > 4
00603 #define ASSERT_FITS_IN_LONGLONG(T) { \
00604 CompileTimeAssertion<sizeof(long long) >= sizeof(T)> assert__##T##__fits_in_longlong; \
00605 assert__##T##__fits_in_longlong.reference(); \
00606 }
00607 #define ASSERT_FITS_IN_POINTER(T) { \
00608 CompileTimeAssertion<sizeof(void*) >= sizeof(T)> assert__##T##__fits_in_pointer; \
00609 assert__##T##__fits_in_pointer.reference(); \
00610 }
00611 #else
00612
00613 #define ASSERT_FITS_IN_POINTER(T)
00614 #define ASSERT_FITS_IN_LONGLONG(T)
00615 #endif
00616
00617
00618 #endif