00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef NMSTL_H_NMSTL
00024 #define NMSTL_H_NMSTL
00025
00026
00027
00028
00033 #define NMSTL_NAMESPACE_BEGIN namespace std {}; namespace NMSTL { using namespace ::std;
00034 #define NMSTL_NAMESPACE_END };
00035
00036 #define NMSTL_ARITH_OPS(T, m) \
00037 bool operator == (const T& x) const { return m == x.m; } \
00038 bool operator != (const T& x) const { return m != x.m; } \
00039 bool operator < (const T& x) const { return m < x.m; } \
00040 bool operator > (const T& x) const { return m > x.m; } \
00041 bool operator <= (const T& x) const { return m <= x.m; } \
00042 bool operator >= (const T& x) const { return m >= x.m; }
00043
00044 #define NMSTL_ARITH_OPS_T1(T, U, m) \
00045 template<typename T> bool operator == (const U& x) const { return m == x.m; } \
00046 template<typename T> bool operator != (const U& x) const { return m != x.m; } \
00047 template<typename T> bool operator < (const U& x) const { return m < x.m; } \
00048 template<typename T> bool operator > (const U& x) const { return m > x.m; } \
00049 template<typename T> bool operator <= (const U& x) const { return m <= x.m; } \
00050 template<typename T> bool operator >= (const U& x) const { return m >= x.m; }
00051
00052 #define NMSTL_TO_STRING_INTL(T) \
00053 inline string to_string(const T& t) { return t.as_string(); } \
00054 inline ostream& operator << (ostream& out, const T& t) { return out << t.as_string(); } \
00055 inline string operator + (const char * in, const T& t) { return string(in) + to_string(t); } \
00056 inline string operator + (const string& in, const T& t) { return in + to_string(t); } \
00057 inline string& operator += (string& in, const T& t) { return in += to_string(t); }
00058
00059 #define NMSTL_TO_STRING(T) NMSTL_TO_STRING_INTL(T) using ::NMSTL::to_string; using ::NMSTL::operator +;
00060
00061 #include <iostream>
00062 #include <iomanip>
00063 #include <string>
00064 #include <sstream>
00065 #include <cstdio>
00066 #include <typeinfo>
00067 #include <map>
00068 #include <vector>
00069
00070 #include <cxxabi.h>
00071
00072 NMSTL_NAMESPACE_BEGIN;
00073
00074
00075 #define NMSTL_TO_STRING_APPEND(A) \
00076 inline string operator + (const string& t, const A& u) { return t + to_string(u); }
00077
00078 #define NMSTL_TO_STRING_APPEND_T1(A) \
00079 template<typename T> inline string operator + (const string& t, const A& u) { return t + to_string(u); }
00080
00081 #define NMSTL_TO_STRING_APPEND_T2(A,B) \
00082 template<typename T, typename U> inline string operator + (const string& t, const A,B& u) { return t + to_string(u); }
00083
00084
00085
00086
00087
00088 #define NMSTL_TO_STRING_STREAM(A) \
00089 NMSTL_TO_STRING_APPEND(A) \
00090 inline ostream& operator << (ostream& out, const A& u) { return out << to_string(u); }
00091
00092 #define NMSTL_TO_STRING_STREAM_T1(A) \
00093 NMSTL_TO_STRING_APPEND_T1(A) \
00094 template<typename T> inline ostream& operator << (ostream& out, const A& u) { return out << to_string(u); }
00095
00096 #define NMSTL_TO_STRING_STREAM_T2(A,B) \
00097 NMSTL_TO_STRING_APPEND_T2(A,B) \
00098 template<typename T, typename U> inline ostream& operator << (ostream& out, const A,B& u) { return out << to_string(u); }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 template<class T>
00109 inline string to_string(const T* t) {
00110 char buf[64];
00111 sprintf(buf, "&(%p)", t);
00112 return buf;
00113 }
00114
00115 inline string to_string(const char* ch) {
00116 return string(ch);
00117 }
00118
00119 inline string to_string(const string& s) {
00120 return s;
00121 }
00122
00123 inline string to_string(const void* t) {
00124 char buf[64];
00125 sprintf(buf, "&%p", t);
00126 return buf;
00127 }
00128 NMSTL_TO_STRING_APPEND(void*);
00129
00130 inline string to_string(const bool& i) {
00131 static string t = "true";
00132 static string f = "false";
00133 return i ? t : f;
00134 }
00135 NMSTL_TO_STRING_APPEND(bool);
00136
00137 template<typename T, typename U>
00138 inline string to_string(const map<T,U>& m) {
00139 string out = "{";
00140
00141 for (typename map<T, U>::const_iterator i = m.begin(); i != m.end(); ++i) {
00142 if (i != m.begin()) out.append("; ");
00143 out.append(to_string(i->first)).append("->").append(to_string(i->second));
00144 }
00145
00146 out.append("}");
00147 return out;
00148 }
00149 NMSTL_TO_STRING_STREAM_T2(map<T,U>);
00150
00151 template<typename T>
00152 inline string to_string(const vector<T>& m) {
00153 string out = "{";
00154
00155 for (unsigned int i = 0; i < m.size(); ++i) {
00156 if (i != 0) out.append("; ");
00157 out.append(to_string(m[i]));
00158 }
00159
00160 out.append("}");
00161 return out;
00162 }
00163 NMSTL_TO_STRING_STREAM_T2(vector<T,U>);
00164
00165 string to_string(const type_info& i);
00166
00167 #define NMSTL_SCALAR_TO_STRING(T) \
00168 inline string to_string(const T& t) { ostringstream o; o << t; return o.str(); } NMSTL_TO_STRING_APPEND(T)
00169
00170 NMSTL_SCALAR_TO_STRING(char);
00171 NMSTL_SCALAR_TO_STRING(unsigned char);
00172 NMSTL_SCALAR_TO_STRING(short);
00173 NMSTL_SCALAR_TO_STRING(unsigned short);
00174 NMSTL_SCALAR_TO_STRING(int);
00175 NMSTL_SCALAR_TO_STRING(unsigned int);
00176 NMSTL_SCALAR_TO_STRING(long);
00177 NMSTL_SCALAR_TO_STRING(unsigned long);
00178 NMSTL_SCALAR_TO_STRING(long long);
00179 NMSTL_SCALAR_TO_STRING(unsigned long long);
00180
00181 inline string to_string(const float& t) { ostringstream o; o << setprecision(99) << t; return o.str(); }
00182 NMSTL_TO_STRING_APPEND(float);
00183 inline string to_string(const double& t) { ostringstream o; o << setprecision(99) << t; return o.str(); }
00184 NMSTL_TO_STRING_APPEND(double);
00185
00186 string to_human_readable(const void *data, int length);
00187 inline string to_human_readable(const string& s) {
00188 return to_human_readable(s.data(), s.length());
00189 }
00190
00191 string to_hex_string(const void *data, int length);
00192 inline string to_hex_string(const string& s) {
00193 return to_hex_string(s.data(), s.length());
00194 }
00195
00196 template <typename Int>
00197 inline string to_hex_string(Int l) {
00198 bool written = false;
00199 string out = "0x";
00200
00201 for (int shift = (sizeof(Int) - 1) * 8; shift >= 0; shift -= 8) {
00202 unsigned char ch = (unsigned char)(l >> shift);
00203 if (ch) written = true;
00204 if (written) {
00205 out += "0123456789ABCDEF"[ch >> 4];
00206 out += "0123456789ABCDEF"[ch & 0xF];
00207 }
00208 }
00209
00210 if (!written) out += "0";
00211 return out;
00212 }
00213
00214 string to_escaped_string(const void *data, int length);
00215 inline string to_escaped_string(const string& s) {
00216 return to_escaped_string(s.data(), s.length());
00217 }
00218
00219 NMSTL_NAMESPACE_END;
00220
00221
00222
00223
00224 #ifdef NMSTL_DMALLOC
00225
00226 #include "dmalloc.h"
00227 #include <new>
00228
00229 inline void* operator new(std::size_t bytes, const char *file, unsigned int line) throw(std::bad_alloc) {
00230 return _malloc_leap(file, line, bytes);
00231 }
00232 inline void* operator new[](std::size_t bytes, const char *file, unsigned int line) throw(std::bad_alloc) {
00233 return _malloc_leap(file, line, bytes);
00234 }
00235
00236 inline void operator delete(void *pnt) { _free_leap(0, 0, pnt); }
00237 inline void operator delete[](void *pnt) { _free_leap(0, 0, pnt); }
00238
00239 #define new new(__FILE__, __LINE__)
00240
00241 #endif
00242
00243 #endif