| 1 |
// $Id: b_id.h,v 1.7 2004/06/27 08:07:24 fukasawa Exp $ |
| 2 |
|
| 3 |
//============================================================================= |
| 4 |
/** |
| 5 |
* @file b_id.h |
| 6 |
* |
| 7 |
* @author Fukasawa Mitsuo |
| 8 |
* |
| 9 |
* |
| 10 |
* Copyright (C) 1998-2004 BEE Co.,Ltd. All rights reserved. |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or |
| 13 |
* modify it under the terms of the GNU General Public License |
| 14 |
* as published by the Free Software Foundation; either version 2 |
| 15 |
* of the License, or (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 |
*/ |
| 26 |
//============================================================================= |
| 27 |
|
| 28 |
#ifndef B_ID_H |
| 29 |
#define B_ID_H |
| 30 |
|
| 31 |
#include "b_value.h" |
| 32 |
|
| 33 |
class b_id : public b_value |
| 34 |
{ |
| 35 |
public: |
| 36 |
b_id(): b_value() { m_t = ATOM_INT4; m._ui = 0; } |
| 37 |
b_id(int id): b_value(id) {} |
| 38 |
b_id(UINT id): b_value(id) {} |
| 39 |
b_id(LONGLONG id): b_value(id) {} |
| 40 |
b_id(ULONGLONG id): b_value(id) {} |
| 41 |
b_id(const string& s): b_value(s) {} |
| 42 |
b_id(const BCHAR * id): b_value(id) {} |
| 43 |
b_id(const b_id& rhs): b_value((b_value&)rhs) {} |
| 44 |
~b_id() {} |
| 45 |
|
| 46 |
bool get(int& rid) { |
| 47 |
if (m_t == ATOM_ASCII) { |
| 48 |
rid = strtoul(m._str->c_str(), NULL, 0); |
| 49 |
return false; |
| 50 |
} else if (m_t == ATOM_INT4) { |
| 51 |
rid = m._in; |
| 52 |
} else if (m_t == ATOM_UINT4) { |
| 53 |
rid = (int)m._ui; |
| 54 |
} else { |
| 55 |
rid = 0; |
| 56 |
return false; |
| 57 |
} |
| 58 |
return true; |
| 59 |
} |
| 60 |
bool get(UINT& rid) { |
| 61 |
if (m_t == ATOM_ASCII) { |
| 62 |
rid = strtoul(m._str->c_str(), NULL, 0); |
| 63 |
return false; |
| 64 |
} else if (m_t == ATOM_INT4) { |
| 65 |
rid = (UINT)m._in; |
| 66 |
} else if (m_t == ATOM_UINT4) { |
| 67 |
rid = m._ui; |
| 68 |
} else { |
| 69 |
rid = 0; |
| 70 |
return false; |
| 71 |
} |
| 72 |
return true; |
| 73 |
} |
| 74 |
bool get(LONGLONG& rid) { |
| 75 |
if (m_t == ATOM_ASCII) { |
| 76 |
#ifdef WIN32 |
| 77 |
rid = _atoi64(m._str->c_str()); |
| 78 |
#else |
| 79 |
rid = strtoll(m._str->c_str(), NULL, 0); |
| 80 |
#endif |
| 81 |
return false; |
| 82 |
} else if (m_t == ATOM_INT8) { |
| 83 |
rid = m._llo; |
| 84 |
} else if (m_t == ATOM_UINT8) { |
| 85 |
rid = (LONGLONG)m._ull; |
| 86 |
} else if (m_t == ATOM_INT4) { |
| 87 |
rid = (LONGLONG)m._in; |
| 88 |
} else if (m_t == ATOM_UINT4) { |
| 89 |
rid = (LONGLONG)m._ui; |
| 90 |
} else { |
| 91 |
rid = 0; |
| 92 |
return false; |
| 93 |
} |
| 94 |
return true; |
| 95 |
} |
| 96 |
bool get(ULONGLONG& rid) { |
| 97 |
if (m_t == ATOM_ASCII) { |
| 98 |
#ifdef WIN32 |
| 99 |
rid = _strtoui64(m._str->c_str(), NULL, 0); |
| 100 |
#else |
| 101 |
rid = strtoull(m._str->c_str(), NULL, 0); |
| 102 |
#endif |
| 103 |
return false; |
| 104 |
} else if (m_t == ATOM_INT8) { |
| 105 |
rid = (ULONGLONG)m._llo; |
| 106 |
} else if (m_t == ATOM_UINT8) { |
| 107 |
rid = m._ull; |
| 108 |
} else if (m_t == ATOM_INT4) { |
| 109 |
rid = (ULONGLONG)m._in; |
| 110 |
} else if (m_t == ATOM_UINT4) { |
| 111 |
rid = (ULONGLONG)m._ui; |
| 112 |
} else { |
| 113 |
rid = 0; |
| 114 |
return false; |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
bool get(string& rid) const { |
| 120 |
if (m_t == ATOM_ASCII) { |
| 121 |
rid = (m._str == NULL) ? _TX("") : *(m._str) ; |
| 122 |
} else { |
| 123 |
BCHAR buf[64]; |
| 124 |
if (m_t == ATOM_INT4) { |
| 125 |
_stprintf(buf, _TX("%d"), m._in); |
| 126 |
} else { |
| 127 |
_stprintf(buf, _TX("%u"), m._ui); |
| 128 |
} |
| 129 |
rid = buf; |
| 130 |
} |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
b_id& operator=(int id) { init(id); return *this; } |
| 135 |
b_id& operator=(UINT id) { init(id); return *this; } |
| 136 |
b_id& operator=(LONGLONG id) { init(id); return *this; } |
| 137 |
b_id& operator=(ULONGLONG id) { init(id); return *this; } |
| 138 |
b_id& operator=(string& id) { init(&id); return *this; } |
| 139 |
b_id& operator=(const BCHAR * id) { |
| 140 |
init(id); |
| 141 |
return *this; |
| 142 |
} |
| 143 |
b_id& operator=(const b_id& rhs) { |
| 144 |
if (this == &rhs) |
| 145 |
return *this; |
| 146 |
if (rhs.m_t == ATOM_ASCII) |
| 147 |
init(rhs.m._str); |
| 148 |
else if (rhs.m_t == ATOM_INT4) |
| 149 |
init(rhs.m._in); |
| 150 |
else if (rhs.m_t == ATOM_UINT4) |
| 151 |
init(rhs.m._ui); |
| 152 |
else if (rhs.m_t == ATOM_INT8) |
| 153 |
init(rhs.m._llo); |
| 154 |
else // if (rhs.m_t == ATOM_UINT8) |
| 155 |
init(rhs.m._ull); |
| 156 |
return *this; |
| 157 |
} |
| 158 |
|
| 159 |
friend bool operator==(const b_id& lhs, const b_id& rhs); |
| 160 |
friend bool operator!=(const b_id& lhs, const b_id& rhs) { |
| 161 |
return (! (lhs == rhs)); |
| 162 |
} |
| 163 |
friend bool operator<(const b_id& lhs, const b_id& rhs); |
| 164 |
|
| 165 |
// |
| 166 |
protected: |
| 167 |
inline void init(const string * v) { |
| 168 |
clean(); |
| 169 |
m_t = ATOM_ASCII; |
| 170 |
m_sz = sizeof(BCHAR); |
| 171 |
if (v == NULL) { |
| 172 |
m_q = 0; m._str = NULL; |
| 173 |
} else { |
| 174 |
m_q = v->size(); |
| 175 |
if (m_q > 0) |
| 176 |
m._str = new string(*v); |
| 177 |
else |
| 178 |
m._str = NULL; |
| 179 |
} |
| 180 |
} |
| 181 |
inline void init(const BCHAR * v) { |
| 182 |
clean(); |
| 183 |
m_t = ATOM_ASCII; |
| 184 |
m_sz = sizeof(BCHAR); |
| 185 |
if (v != NULL) |
| 186 |
m._str = new string(v); |
| 187 |
else |
| 188 |
m._str = NULL; |
| 189 |
m_q = m._str->size(); |
| 190 |
} |
| 191 |
inline void init(int v) { |
| 192 |
clean(); |
| 193 |
m_t = ATOM_INT4; |
| 194 |
m_q = 1; |
| 195 |
m_sz = sizeof(int); |
| 196 |
m._in = v; |
| 197 |
} |
| 198 |
inline void init(UINT v) { |
| 199 |
clean(); |
| 200 |
m_t = ATOM_UINT4; |
| 201 |
m_q = 1; |
| 202 |
m_sz = sizeof(UINT); |
| 203 |
m._ui = v; |
| 204 |
} |
| 205 |
inline void init(LONGLONG v) { |
| 206 |
clean(); |
| 207 |
m_t = ATOM_INT8; |
| 208 |
m_q = 1; |
| 209 |
m_sz = sizeof(LONGLONG); |
| 210 |
m._llo = v; |
| 211 |
} |
| 212 |
inline void init(ULONGLONG v) { |
| 213 |
clean(); |
| 214 |
m_t = ATOM_UINT8; |
| 215 |
m_q = 1; |
| 216 |
m_sz = sizeof(ULONGLONG); |
| 217 |
m._ull = v; |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
inline bool operator==(const b_id& lhs, const b_id& rhs) |
| 222 |
{ |
| 223 |
if (lhs.m_t == rhs.m_t) { |
| 224 |
if (lhs.m_t == ATOM_UINT4) { |
| 225 |
return (lhs.m._ui == rhs.m._ui); |
| 226 |
} else if (lhs.m_t == ATOM_INT4) { |
| 227 |
return (lhs.m._in == rhs.m._in); |
| 228 |
} else if (lhs.m_t == ATOM_INT8) { |
| 229 |
return (lhs.m._llo == rhs.m._llo); |
| 230 |
} else if (lhs.m_t == ATOM_UINT8) { |
| 231 |
return (lhs.m._ull == rhs.m._ull); |
| 232 |
} else { |
| 233 |
return (*lhs.m._str == *rhs.m._str); |
| 234 |
} |
| 235 |
} |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
inline bool operator<(const b_id& lhs, const b_id& rhs) |
| 240 |
{ |
| 241 |
if (lhs.m_t == rhs.m_t) { |
| 242 |
if (lhs.m_t == ATOM_UINT4) { |
| 243 |
return (lhs.m._ui < rhs.m._ui); |
| 244 |
} else if (lhs.m_t == ATOM_INT4) { |
| 245 |
return (lhs.m._in < rhs.m._in); |
| 246 |
} else if (lhs.m_t == ATOM_INT8) { |
| 247 |
return (lhs.m._llo < rhs.m._llo); |
| 248 |
} else if (lhs.m_t == ATOM_UINT8) { |
| 249 |
return (lhs.m._ull < rhs.m._ull); |
| 250 |
} else { |
| 251 |
return (*lhs.m._str < *rhs.m._str); |
| 252 |
} |
| 253 |
} |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
typedef vector<b_id> b_ids; |
| 258 |
|
| 259 |
struct id_less |
| 260 |
{ |
| 261 |
bool operator() (const b_id& x, const b_id& y) const { return (x < y); } |
| 262 |
}; |
| 263 |
|
| 264 |
#endif /* B_ID_H */ |