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 #include "Object.h"
00026
00027 #include "XMLParser.h"
00028 #include "UninstantiatedXMLObject.h"
00029
00030
00031 #include "AutoXML.cheader"
00032
00033 #ifdef WIN32
00034 #else
00035 #include "sysdep.h"
00036 #include "uuid.h"
00037 #include "uuidlib.h"
00038 #endif
00039
00040
00041 namespace cmlabs {
00042
00043 static int processid = 0;
00044
00045
00046
00047
00048
00049 Object::Object()
00050 {
00051 unitTestLog = NULL;
00052 }
00053
00054 Object::~Object()
00055 {
00056 }
00057
00058 bool Object::equals(const Object* o2) const
00059 {
00060 return (this == o2);
00061 }
00062
00063 int Object::compare(const Object* o2) const {
00064 return 0;
00065 }
00066
00067 bool Object::unitTest()
00068 {
00069
00070 return false;
00071 }
00072
00073 JString Object::print() {
00074 return getClass();
00075 }
00076
00077 JString Object::toHTML() {
00078 return print();
00079 }
00080
00081 JString Object::toXML() {
00082 return this->print();
00083 }
00084
00085 bool Object::fromXML(const JString& xml) {
00086
00087 if (xml.length() == 0)
00088 return false;
00089
00090 XMLParser* xmlParser = new XMLParser();
00091 xmlParser->parse(xml);
00092 bool ret = fromXML(xmlParser->getRootNode());
00093 delete(xmlParser);
00094 return ret;
00095 }
00096
00097 bool Object::fromXML(XMLNode* node) {
00098 return true;
00099 }
00100
00101
00102 Object* Object::createObjectFromXML(const JString& xml) {
00103 if (xml.length() == 0)
00104 return NULL;
00105
00106 XMLParser* xmlParser = new XMLParser();
00107 xmlParser->parse(xml);
00108 XMLNode* rootNode = xmlParser->getRootNode();
00109 Object* obj = createObjectFromXML(rootNode);
00110 delete(xmlParser);
00111 return obj;
00112 }
00113
00114 Object* Object::createObjectFromXML(XMLNode* rootNode) {
00115
00116 if (rootNode == NULL) return NULL;
00117
00118 JString tagName = rootNode->getTag();
00119
00120 Object* obj = NULL;
00121
00122 if (tagName.equalsIgnoreCase("UninstantiatedXMLObject")) {
00123
00124 obj = (Object*) new UninstantiatedXMLObject(rootNode);
00125 }
00126
00127 #include "AutoXML.code"
00128
00129 else {
00130 if (DEBUGLEVEL(KITCHENSINK)) {
00131 printf("\n[Object] ################# Object::createObjectFromXML: %s unsupported #################\n\n", (char*) tagName);
00132 }
00133
00134 obj = (Object*) new UninstantiatedXMLObject(rootNode);
00135 }
00136
00137 return obj;
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 }
00190
00191 double Object::getRandomNumber() {
00192
00193 double d = 0;
00194 double t = (1.0/((RAND_MAX + 1.0)*(RAND_MAX + 1.0)*(RAND_MAX + 1.0)));
00195 do {
00196 d = (rand () * ((RAND_MAX + 1.0) * (RAND_MAX + 1.0))
00197 + rand () * (RAND_MAX + 1.0)
00198 + rand ()) * t;
00199 } while (d >= 1);
00200 return d;
00201 }
00202
00203 JString Object::getCoreLibraryVersion() {
00204 return CLVERSION;
00205 }
00206
00207 JString Object::createUniqueID() {
00208 JString str = "";
00209 #ifdef WIN32
00210 #ifdef _WIN32_WCE
00211
00212 SYSTEMTIME systime;
00213 FILETIME filetime;
00214
00215
00216 GetSystemTime(&systime);
00217 SystemTimeToFileTime(&systime,&filetime);
00218 DWORD tick=GetTickCount();
00219 DWORD highWord=filetime.dwHighDateTime+0x146BF4;
00220
00221 str = JString::format("%08.8x-%04.4x-%04.4x-%04.4x-%04.4x%04.4x%04.4x",
00222 filetime.dwLowDateTime,
00223 LOWORD(highWord),
00224 HIWORD(highWord |0x10000000),
00225 LOWORD(rand()),
00226 HIWORD(tick),
00227 LOWORD(tick),
00228 LOWORD(rand()));
00229
00230
00231 #else
00232 unsigned char* chstr;
00233 UUID uuid;
00234
00235 UuidCreate(&uuid);
00236
00237
00238 UuidToString(&uuid, &chstr);
00239 str = (char*) chstr;
00240 RpcStringFree(&chstr);
00241
00242
00243 #endif
00244 #else
00245 uuid__t u;
00246 uuid_create(&u);
00247 char s[40];
00248 sprintf(s, "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
00249 u.time_low, u.time_mid, u.time_hi_and_version, u.clock_seq_hi_and_reserved, u.clock_seq_low,
00250 u.node[0], u.node[1], u.node[2], u.node[3], u.node[4], u.node[5]);
00251 str = s;
00252
00253
00254
00255
00256
00257
00258
00259 #endif // WIN32
00260 return str;
00261 }
00262
00263 JString Object::createUniqueID(const JString& title) {
00264 if (title.length() > 0)
00265 return JString::format("%s:%s", (char*) title, (char*) createUniqueID());
00266 else
00267 return createUniqueID();
00268 }
00269
00270 JString Object::extractIDLabel(const JString& id) {
00271 const char *pos = id, *p;
00272 while ( (p = strstr((const char*)pos, ":")) != NULL)
00273 pos = p+1;
00274 if (pos <= id)
00275 return "";
00276 return id.substring(0, pos-id-1);
00277 }
00278
00279 void Object::addUnitTestLog(const JString& str) {
00280
00281
00282 JString s = JString(unitTestLog) + str + "\n";
00283 delete [] unitTestLog;
00284 int len;
00285 unitTestLog = s.getCharCopy(len);
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 }
00297
00298
00299 JString Object::getUnitTestLog() {
00300 if (unitTestLog != NULL) {
00301 JString str = unitTestLog;
00302 delete [] unitTestLog;
00303 unitTestLog = NULL;
00304 return str;
00305 }
00306 else
00307 return "";
00308 }
00309
00310 Object* Object::clone() const {
00311 return NULL;
00312 }
00313
00314 bool Object::isSameClass(const Object* o2) const {
00315 if (o2 == NULL)
00316 return false;
00317 try {
00318 if ((typeid(*this) == typeid(*o2)) != 0) {
00319 return true;
00320 }
00321 else {
00322
00323 return this->getClass().equals(o2->getClass());
00324 }
00325 }
00326 catch(...) {}
00327 printf("Object::isSameClass Exception\n");
00328
00329 return this->getClass().equals(o2->getClass());
00330
00331
00332 }
00333
00334 JString Object::getClass() const {
00335
00336 const char* str;
00337 try {
00338 str = typeid(*this).name();
00339 }
00340 catch(...) {
00341 printf("Object::getClass Exception reading Object TypeInfo String\n");
00342 return "";
00343 }
00344
00345 try {
00346 const char* str_object;
00347 const char* str_cmlabs = strstr(str, "cmlabs");
00348 if (str_cmlabs == NULL) {
00349
00350 return str;
00351 }
00352
00353
00354 if (strstr(str, "class") == str) {
00355 if (strlen(str_cmlabs) > 8) {
00356 str_object = str_cmlabs + 8;
00357 return str_object;
00358 }
00359 }
00360
00361 else if (strstr(str, "N6cmlabs") == str) {
00362 if (strlen(str_cmlabs) > 7) {
00363 str_object = str_cmlabs + 6;
00364 int count = atoi(str_object);
00365 int len = 1;
00366 if (count > 9)
00367 len = 2;
00368 if (count > 99)
00369 len = 3;
00370
00371 char name[256];
00372 memcpy(name, str_object+len, count);
00373 name[count] = 0;
00374 return name;
00375 }
00376 }
00377
00378 printf("Object::getClass: Unknown Object TypeInfo String: %s\n", (char*)str);
00379 return str;
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 }
00399 catch(...) {
00400 printf("Object::getClass Exception: Unknown Object TypeInfo String: %s\n", (char*)str);
00401 return "Object";
00402 }
00403
00404 }
00405
00406
00407 bool Object::wait(long ms) {
00408
00409 #ifdef WIN32
00410 if (ms <= 0)
00411 Sleep(0);
00412 else
00413 Sleep(ms);
00414 #else
00415 if (ms <= 0)
00416 sched_yield();
00417 else
00418 usleep(ms * 1000);
00419 #endif // WIN32
00420
00421 return true;
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 int Object::getProcID() {
00442
00443 int id;
00444
00445 #ifdef WIN32
00446 id = GetCurrentProcessId();
00447 #else
00448 if (processid == 0)
00449 processid = getpid();
00450 id = processid;
00451 #endif // WIN32
00452
00453 return id;
00454 }
00455
00456
00457 long Object::getBinarySize(int chunk) {
00458 return 0;
00459 }
00460
00461
00462 int Object::getBinaryChunkCount() {
00463 return 0;
00464 }
00465
00466
00467 long Object::toBinaryBuffer(int chunk, char* buffer, int maxlen) {
00468 return 0;
00469 }
00470
00471
00472 bool Object::fromBinaryBuffer(int chunk, char* buffer, long len) {
00473 return false;
00474 }
00475
00476 bool Object::isCollection() const {
00477 return false;
00478 }
00479
00480
00481 unsigned long Object::getPayloadSize() const {
00482 return 0;
00483 }
00484
00485 unsigned long Object::getHash() const {
00486 unsigned long key = (unsigned long) this;
00487 return (key >> 3) * 2654435761u;
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 }
00505
00506
00507 SysInfo Object::getSystemInfo() {
00508 SysInfo info;
00509 info.update();
00510 return info;
00511 }
00512
00513
00514
00515 ObjectLink::ObjectLink() {
00516 object = NULL;
00517 active = NULL;
00518 stopUsing = false;
00519 }
00520
00521 ObjectLink::~ObjectLink() {
00522 *active = false;
00523 }
00524
00525 Object* ObjectLink::clone() const {
00526 return new ObjectLink();
00527 }
00528
00529 JString ObjectLink::toXML() {return "<objectlink />";}
00530
00531 bool ObjectLink::isValid() {
00532 return ((object != NULL) && (!stopUsing));
00533 }
00534
00535
00536 ObjectLinkProvider::ObjectLinkProvider(ObjectLink* link, Object* object) {
00537 active = ( (link != NULL) && (object != NULL) );
00538 this->link = link;
00539 if (link != NULL) {
00540 link->active = &this->active;
00541 link->object = object;
00542 link->stopUsing = false;
00543 }
00544 }
00545
00546 ObjectLinkProvider::~ObjectLinkProvider() {
00547 if (isInUse()) {
00548 link->stopUsing = true;
00549 link->object = NULL;
00550 }
00551 }
00552
00553 Object* ObjectLinkProvider::clone() const {
00554 return new ObjectLinkProvider(NULL, NULL);
00555 }
00556
00557 bool ObjectLinkProvider::isInUse() {
00558 return active;
00559 }
00560
00561 bool ObjectLinkProvider::stopUsingMe() {
00562 link->stopUsing = true;
00563 return true;
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 }