1 /* 2 * 3 * C++ Portable Types Library (PTypes) 4 * Version 2.1.1 Released 27-Jun-2007 5 * 6 * Copyright (C) 2001-2007 Hovik Melikyan 7 * 8 * http://www.melikyan.com/ptypes/ 9 * 10 */ 11 12 #include "ptypes.h" 13 14 15 PTYPES_BEGIN 16 17 component()18component::component() 19 : unknown(), refcount(0), freelist(nil), typeinfo(nil) {} 20 21 ~component()22component::~component() 23 { 24 if (freelist != nil) 25 { 26 for (int i = 0; i < freelist->get_count(); i++) 27 (*freelist)[i]->freenotify(this); 28 delete freelist; 29 freelist = nil; 30 } 31 } 32 33 freenotify(component *)34void component::freenotify(component*) 35 { 36 } 37 38 addnotification(component * obj)39void component::addnotification(component* obj) 40 { 41 if (freelist == nil) 42 freelist = new tobjlist<component>(false); 43 freelist->add(obj); 44 } 45 46 delnotification(component * obj)47void component::delnotification(component* obj) 48 { 49 int i = -1; 50 if (freelist != nil) 51 { 52 i = freelist->indexof(obj); 53 if (i >= 0) { 54 freelist->del(i); 55 if (freelist->get_count() == 0) 56 { 57 delete freelist; 58 freelist = nil; 59 } 60 } 61 } 62 if (i == -1) 63 fatal(CRIT_FIRST + 1, "delnotification() failed: no such object"); 64 } 65 66 classid()67int component::classid() 68 { 69 return CLASS_UNDEFINED; 70 } 71 72 addref(component * c)73component* ptdecl addref(component* c) 74 { 75 if (c != nil) 76 #ifdef PTYPES_ST 77 c->refcount++; 78 #else 79 pincrement(&c->refcount); 80 #endif 81 return c; 82 } 83 84 release(component * c)85bool ptdecl release(component* c) 86 { 87 if (c != nil) 88 { 89 #ifdef PTYPES_ST 90 if (--c->refcount == 0) 91 #else 92 if (pdecrement(&c->refcount) == 0) 93 #endif 94 delete c; 95 else 96 return false; 97 } 98 return true; 99 } 100 101 102 PTYPES_END 103