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 #include "w_defines.h"
00054
00055
00056
00057 #define W_SOURCE
00058
00059 #ifdef __GNUG__
00060 #pragma implementation "w_base.h"
00061 #endif
00062
00063 #include <w_base.h>
00064 #include <sstream>
00065
00066
00067
00068
00069 const w_base_t::int1_t w_base_t::int1_max = 0x7f;
00070 const w_base_t::int1_t w_base_t::int1_min = (w_base_t::int1_t) 0x80u;
00071 const w_base_t::uint1_t w_base_t::uint1_max = 0xff;
00072 const w_base_t::uint1_t w_base_t::uint1_min = 0x0;
00073 const w_base_t::int2_t w_base_t::int2_max = 0x7fff;
00074 const w_base_t::int2_t w_base_t::int2_min = (w_base_t::int2_t) 0x8000u;
00075 const w_base_t::uint2_t w_base_t::uint2_max = 0xffff;
00076 const w_base_t::uint2_t w_base_t::uint2_min = 0x0;
00077 const w_base_t::int4_t w_base_t::int4_max = 0x7fffffff;
00078 const w_base_t::int4_t w_base_t::int4_min = 0x80000000;
00079 const w_base_t::uint4_t w_base_t::uint4_max = 0xffffffff;
00080 const w_base_t::uint4_t w_base_t::uint4_min = 0x0;
00081
00082 # define LONGLONGCONSTANT(i) i##LL
00083 # define ULONGLONGCONSTANT(i) i##ULL
00084
00085 #ifdef ARCH_LP64
00086
00087 const w_base_t::uint8_t w_base_t::uint8_max =
00088 ULONGLONGCONSTANT(0xffffffffffffffff);
00089 const w_base_t::uint8_t w_base_t::uint8_min =
00090 ULONGLONGCONSTANT(0x0);
00091 const w_base_t::int8_t w_base_t::int8_max =
00092 LONGLONGCONSTANT(0x7fffffffffffffff);
00093 const w_base_t::int8_t w_base_t::int8_min =
00094 LONGLONGCONSTANT(0x8000000000000000);
00095 #else
00096
00097 const w_base_t::uint8_t w_base_t::uint8_max =
00098 ULONGLONGCONSTANT(0xffffffff);
00099 const w_base_t::uint8_t w_base_t::uint8_min =
00100 ULONGLONGCONSTANT(0x0);
00101 const w_base_t::int8_t w_base_t::int8_max =
00102 LONGLONGCONSTANT(0x7fffffff);
00103 const w_base_t::int8_t w_base_t::int8_min =
00104 LONGLONGCONSTANT(0x80000000);
00105 #endif
00106
00107 ostream&
00108 operator<<(ostream& o, const w_base_t&)
00109 {
00110 w_base_t::assert_failed("w_base::operator<<() called", __FILE__, __LINE__);
00111 return o;
00112 }
00113
00114 void
00115 w_base_t::assert_failed(
00116 const char* desc,
00117 const char* file,
00118 uint4_t line)
00119 {
00120 stringstream os;
00121
00122 os << "assertion failure: " << desc << endl
00123 << "1. error in "
00124 << file << ':' << line
00125 << " Assertion failed" << endl
00126 << "\tcalled from:" << endl
00127 << "\t0) " << file << ':' << line
00128 << endl << ends;
00129 fprintf(stderr, "%s", os.str().c_str());
00130 abort();
00131 }
00132
00133
00134 typedef ios::fmtflags fmtflags;
00135
00136 #include <w_strstream.h>
00137
00138
00139 static w_base_t::uint8_t
00140 __strtou8(
00141 const char *str,
00142 char **endptr,
00143 int base,
00144 bool is_signed
00145 )
00146 {
00147 #if defined(ARCH_LP64)
00148 return is_signed? strtol(str, endptr, base): strtoul(str, endptr, base);
00149 #else
00150 return is_signed? strtoll(str, endptr, base): strtoull(str, endptr, base);
00151 #endif
00152 }
00153
00154 w_base_t::int8_t
00155 w_base_t::strtoi8(
00156 const char *str,
00157 char **endptr,
00158 int base
00159 )
00160 {
00161 w_base_t::int8_t i8;
00162 w_base_t::int8_t u8 =
00163 __strtou8(str, endptr, base, true);
00164 i8 = w_base_t::int8_t(u8);
00165 return i8;
00166 }
00167
00168 w_base_t::uint8_t
00169 w_base_t::strtou8(
00170 const char *str,
00171 char **endptr,
00172 int base
00173 )
00174 {
00175 return __strtou8(str, endptr, base, false);
00176 }
00177
00178 #if defined(SOLARIS2)
00179 #include <ieeefp.h>
00180 #else
00181 #include <cmath>
00182 #endif
00183
00184 bool
00185 w_base_t::is_finite(const f8_t x)
00186 {
00187 bool value = false;
00188 value = finite(x);
00189 return value;
00190 }
00191
00192 bool
00193 w_base_t::is_infinite(const f8_t x)
00194 {
00195 bool value = false;
00196 #if defined(SOLARIS2)
00197 value = !finite(x) && !isnand(x);
00198 #elif defined(MacOSX) && W_GCC_THIS_VER >= W_GCC_VER(3,0)
00199 value = !finite(x) && !__isnand(x);
00200 #else
00201 value = !finite(x) && !isnan(x);
00202 #endif
00203 return value;
00204 }
00205
00206 bool
00207 w_base_t::is_nan(const f8_t x)
00208 {
00209 bool value = false;
00210 #if defined(SOLARIS2)
00211 value = isnand(x);
00212 #elif defined(MacOSX) && W_GCC_THIS_VER >= W_GCC_VER(3,0)
00213 value = __isnand(x);
00214 #else
00215 value = isnan(x);
00216 #endif
00217 return value;
00218 }
00219
00220 bool
00221 w_base_t::is_infinite_or_nan(const f8_t x)
00222 {
00223 bool value = false;
00224 value = !finite(x);
00225 return value;
00226 }
00227
00228
00229 void w_base_t::abort()
00230 {
00231 cout.flush();
00232 cerr.flush();
00233 ::abort();
00234 }
00235
00236
00237
00238
00239
00240
00241 #ifdef __GNUG__
00242 #define PURE_VIRTUAL extern "C" void __pure_virtual()
00243 #else
00244 #define PURE_VIRTUAL void pure_virtual()
00245 #endif
00246
00247 PURE_VIRTUAL
00248 {
00249
00250 static bool called = false;
00251 if (!called)
00252 cerr << "** Pure virtual function called" << endl;
00253 called = true;
00254
00255 w_base_t::abort();
00256
00257 }
00258
00259
00260 #include <netinet/in.h>
00261 w_base_t::uint2_t w_base_t::w_ntohs(w_base_t::uint2_t net)
00262 {
00263 return ntohs(net);
00264 }
00265
00266 w_base_t::uint2_t w_base_t::w_htons(w_base_t::uint2_t host)
00267 {
00268 return htons(host);
00269 }
00270
00271 w_base_t::uint4_t w_base_t::w_ntohl(w_base_t::uint4_t net)
00272 {
00273 return ntohl(net);
00274 }
00275
00276 w_base_t::uint4_t w_base_t::w_htonl(w_base_t::uint4_t host)
00277 {
00278 return htonl(host);
00279 }