Object.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Header$
00004 
00005 */
00006 
00007 #include <string.h>
00008 
00009 #include "Memory.h"
00010 #include "Config.h"
00011 #include "Error.h"
00012 #include "SignalBase.h"
00013 #include "RegistryBase.h"
00014 #include "ServiceBase.h"
00015 #include "Utils.h"
00016 
00017 #include "Object.h"
00018 
00019 // Public methods.
00020 
00021 Object::ObjectList Object::_objects;
00022 Object::DefferedDeletionObjectList Object::_deferredDeletionObjects;
00023 Object::RegistryList Object::_registryStatic;
00024 
00025 unsigned int Object::_idCounter(0);
00026 
00027 Object::Object() : Support(),
00028   _signals(),
00029   _registryPrivate(),
00030   _suspended(false),
00031   _cpuTimestamp(0),
00032   _cpuTime(0)
00033 {
00034   _id = ++_idCounter;
00035   _objects[_id] = this;
00036 }
00037 
00038 Object::~Object()
00039 {
00040   if (!_signals.empty()) {
00041     int res;
00042 
00043     SignalList::const_iterator i;
00044     SignalBase *sgn;
00045     while ((i = _signals.begin()) != _signals.end())
00046       if ((sgn = *i))
00047         if (FAILED(res = sgn->disconnect(this)))
00048           ERROR_BACKTRACE(res);
00049   }
00050 
00051   ObjectList::iterator f = _objects.find(_id);
00052   if (f != _objects.end())
00053     _objects.erase(f);
00054 }
00055 
00056 ObjectId Object::id() const
00057 {
00058   return _id;
00059 }
00060 
00061 int Object::get(const ObjectId id, Object **obj)
00062 {
00063   if (!obj)
00064     return ERROR(MSG_NULL_POINTER);
00065 
00066   if (id == nid) {
00067     *obj = NULL;
00068     return ERROR(MSG_OBJECT_INVALID_ID, id);
00069   }
00070 
00071   ObjectList::const_iterator f = _objects.find(id);
00072 
00073   if (f == _objects.end()) {
00074     *obj = NULL;
00075     return ERROR(MSG_OBJECT_NOT_FOUND, id);
00076   }
00077 
00078   *obj = f->second;
00079 
00080   return OK;
00081 }
00082 
00083 int Object::suspend()
00084 {
00085   _suspended = true;
00086   return OK;
00087 }
00088 
00089 bool Object::suspended() const
00090 {
00091   return _suspended;
00092 }
00093 
00094 int Object::resume()
00095 {
00096   _suspended = false;
00097   return OK;
00098 }
00099 
00100 int Object::listObjects(String &text)
00101 {
00102   ObjectList::const_iterator i = _objects.begin();
00103   ObjectList::const_iterator e = _objects.end();
00104   Object *obj;
00105   String line;
00106   unsigned int n = 1;
00107 
00108   line.sprintf("%s%6s\t%10s\t%9s\t%10s\t%-32s%s", String::eol(), "#", "ID", "SUSPENDED", "CPU", "CLASS", String::eol());
00109   text = OBJECT_COLOR_TITLE(line);
00110   while (i != e) {
00111     if ((obj = i->second)) {
00112       line.sprintf("%6u\t%10llu\t%9s\t%10llu\t%-32s%s", n, obj->id(), obj->suspended() ? "Yes" : "No", obj->cpuTime(), CSTRING(obj->className()), String::eol());
00113       text += line;
00114       ++n;
00115     }
00116     ++i;
00117   }
00118   return OK;
00119 }
00120 
00121 int Object::listSignals(String &text)
00122 {
00123   ObjectList::const_iterator i = _objects.begin();
00124   ObjectList::const_iterator e = _objects.end();
00125   SignalBase *signal;
00126   String line;
00127   unsigned int n = 1;
00128 
00129   line.sprintf("%s%6s\t%6s\t%-32s%s", String::eol(), "#", "ID", "NAME", String::eol());
00130   text = OBJECT_COLOR_TITLE(line);
00131   while (i != e) {
00132     if ((signal = SIGNAL_BASE(i->second))) {
00133       line.sprintf("%6u\t%6llu\t%-32s%s", n, signal->id(), CSTRING(signal->name()), String::eol());
00134       text += line;
00135       ++n;
00136     }
00137     ++i;
00138   }
00139   return OK;
00140 }
00141 
00142 int Object::listMySignals(String &text)
00143 {
00144   SignalList::const_iterator i = _signals.begin();
00145   SignalList::const_iterator e = _signals.end();
00146   SignalBase *signal;
00147   String line;
00148   unsigned int n = 1;
00149 
00150   line.sprintf("%s%6s\t%6s\t%-32s%s", String::eol(), "#", "ID", "NAME", String::eol());
00151   text = OBJECT_COLOR_TITLE(line);
00152   while (i != e) {
00153     if ((signal = *i)) {
00154       line.sprintf("%6u\t%6llu\t%-32s%s", n, signal->id(), CSTRING(signal->name()), String::eol());
00155       text += line;
00156       ++n;
00157     }
00158     ++i;
00159   }
00160   return OK;
00161 }
00162 
00163 int Object::listRegistry(String &text)
00164 {
00165   RegistryList::const_iterator i = _registryStatic.begin();
00166   RegistryList::const_iterator e = _registryStatic.end();
00167   RegistryBase *reg;
00168   String line;
00169   String value;
00170   unsigned int n = 1;
00171   int res;
00172 
00173   line.sprintf("%s%6s\t%6s\t%4s\t%3s\t%3s\t%6s\t%6s\t%-32s\t%-32s%s", String::eol(),
00174     "#", "ID", "PERS", "ACC", "VIS", "GETTER", "SETTER", "NAME", "VALUE", String::eol());
00175   text = OBJECT_COLOR_TITLE(line);
00176   while (i != e) {
00177     if ((reg = i->second)) {
00178       if (FAILED(res = reg->toString(value)))
00179         ERROR_BACKTRACE(res);
00180       line.sprintf("%6u\t%6llu\t%4c\t%3c\t%3c\t%6s\t%6s\t%-32s\t%-32s%s",
00181                n, reg->id(), reg->persistency() >> REG_PERSISTENCY_SHIFT,
00182                reg->accessMode() >> REG_ACCESS_MODE_SHIFT, reg->visibility() >> REG_VISIBILITY_SHIFT,
00183                reg->hasGetter() ? "Yes" : "No", reg->hasSetter() ? "Yes" : "No",
00184                CSTRING(reg->name()), CSTRING(value), String::eol());
00185       text += line;
00186       ++n;
00187     }
00188     ++i;
00189   }
00190   return OK;
00191 }
00192 
00193 int Object::listMyRegistry(String &text)
00194 {
00195   RegistryList::const_iterator i = _registryPrivate.begin();
00196   RegistryList::const_iterator e = _registryPrivate.end();
00197   RegistryBase *reg;
00198   String line;
00199   String value;
00200   unsigned int n = 1;
00201   int res;
00202 
00203   line.sprintf("%s%6s\t%6s\t%4s\t%3s\t%3s\t%6s\t%6s\t%-32s\t%-32s%s", String::eol(),
00204     "#", "ID", "PERS", "ACC", "VIS", "GETTER", "SETTER", "NAME", "VALUE", String::eol());
00205   text = OBJECT_COLOR_TITLE(line);
00206   while (i != e) {
00207     if ((reg = i->second)) {
00208       if (FAILED(res = reg->toString(value)))
00209         ERROR_BACKTRACE(res);
00210       line.sprintf("%6u\t%6llu\t%4c\t%3c\t%3c\t%6s\t%6s\t%-32s\t%-32s%s",
00211                n, reg->id(), reg->persistency() >> REG_PERSISTENCY_SHIFT,
00212                reg->accessMode() >> REG_ACCESS_MODE_SHIFT, reg->visibility() >> REG_VISIBILITY_SHIFT,
00213                reg->hasGetter() ? "Yes" : "No", reg->hasSetter() ? "Yes" : "No",
00214                CSTRING(reg->name()), CSTRING(value), String::eol());
00215       text += line;
00216       ++n;
00217     }
00218     ++i;
00219   }
00220   return OK;
00221 }
00222 
00223 int Object::listInstances(String &text)
00224 {
00225   ObjectList::const_iterator i = _objects.begin();
00226   ObjectList::const_iterator e = _objects.end();
00227   ServiceBase *service;
00228   String line;
00229   unsigned int n = 1;
00230 
00231   line.sprintf("%s%6s\t%6s\t%4s\t%10s\t%-16s%s", String::eol(), "#", "ID", "SUSP", "CPU", "NAME", String::eol());
00232   text = OBJECT_COLOR_TITLE(line);
00233   while (i != e) {
00234     if ((service = queryInterface<ServiceBase>(i->second))) {
00235       line.sprintf("%6u\t%6llu\t%4s\t%10llu\t%-16s%s",
00236         n, service->id(), service->suspended() ? "Yes" : "No", service->cpuTime(), CSTRING(service->name()), String::eol());
00237       text += line;
00238       ++n;
00239     }
00240     ++i;
00241   }
00242 
00243   return OK;
00244 }
00245 
00246 int Object::listTop(String &text, const unsigned int itemsNum)
00247 {
00248   vector<Object *> objects;
00249   ObjectList::const_iterator i = _objects.begin();
00250   ObjectList::const_iterator e = _objects.end();
00251   String line;
00252   unsigned int j, size;
00253 
00254   line.sprintf("%s%6s\t%10s\t%10s\t%-32s%s", String::eol(), "#", "ID", "CPU", "CLASS", String::eol());
00255   text = OBJECT_COLOR_TITLE(line);
00256   while (i != e) {
00257     if (i->second)
00258       objects.push_back(i->second);
00259     ++i;
00260   }
00261 
00262   sort(objects.begin(), objects.end(), compareCpuTime);
00263 
00264   size = itemsNum != 0 ? MIN(objects.size(), itemsNum) : objects.size();
00265 
00266   for (j = 0; j < size; ++j) {
00267     line.sprintf("%6u\t%10llu\t%10llu\t%-32s%s", j + 1, objects[j]->id(), objects[j]->cpuTime(), CSTRING(objects[j]->className()), String::eol());
00268     text += line;
00269   }
00270 
00271   return OK;
00272 }
00273 
00274 void Object::profBegin()
00275 {
00276   _cpuTimestamp = clock();
00277 }
00278 
00279 void Object::profEnd()
00280 {
00281   _cpuTime += (clock() - _cpuTimestamp) / CLOCKS_PER_MSEC;
00282 }
00283 
00284 unsigned long long Object::cpuTime() const
00285 {
00286   return _cpuTime;
00287 }
00288 
00289 // Protected methods.
00290 
00291 void Object::deleteLater(Object *obj)
00292 {
00293   DefferedDeletionObjectList::const_iterator f = _deferredDeletionObjects.find(obj);
00294 
00295   if (f != _deferredDeletionObjects.end())
00296     ERROR(MSG_OBJECT_ALREADY_REGISTERED_FOR_DEFERRED_DELETION, obj->id());
00297 
00298   _deferredDeletionObjects.insert(obj);
00299 }
00300 
00301 // Private methods.
00302 
00303 int Object::bindSignal(SignalBase *sgn)
00304 {
00305   if (!sgn)
00306     return ERROR(MSG_NULL_POINTER);
00307 
00308   SignalList::const_iterator f = _signals.find(sgn);
00309 
00310   if (f == _signals.end())
00311     _signals.insert(sgn);
00312   else
00313     return ERROR(MSG_OBJECT_SIGNAL_ALREADY_EXISTS, CSTRING(sgn->name()));
00314 
00315   return OK;
00316 }
00317 
00318 int Object::unbindSignal(SignalBase *sgn)
00319 {
00320   if (!sgn)
00321     return ERROR(MSG_NULL_POINTER);
00322 
00323   SignalList::iterator f = _signals.find(sgn);
00324 
00325   if (f == _signals.end())
00326     return ERROR(MSG_OBJECT_SIGNAL_IS_NOT_CONNECTED, CSTRING(sgn->name()), _id);
00327   else
00328     _signals.erase(f);
00329 
00330   return OK;
00331 }
00332 
00333 int Object::bindRegistry(Object *owner, RegistryBase *reg)
00334 {
00335  if (!reg)
00336     return ERROR(MSG_NULL_POINTER);
00337 
00338   RegistryList *list = owner ? &(owner->_registryPrivate) : &(Object::_registryStatic);
00339   RegistryList::const_iterator f = list->find(reg->name());
00340 
00341   if (f == list->end())
00342     (*list)[reg->name()] = reg;
00343   else
00344     ERROR(MSG_OBJECT_REGISTRY_ALREADY_EXISTS, CSTRING(reg->name()));
00345 
00346   return OK;
00347 }
00348 
00349 int Object::deletePostponed()
00350 {
00351   if (!_deferredDeletionObjects.empty()) {
00352     DefferedDeletionObjectList objectsToBeDeleted(_deferredDeletionObjects);
00353 
00354     _deferredDeletionObjects.clear();
00355 
00356     DefferedDeletionObjectList::iterator i = objectsToBeDeleted.begin();
00357     DefferedDeletionObjectList::iterator e = objectsToBeDeleted.end();
00358 
00359     while (i != e) {
00360       DEBUG("Deleting object of class '%s'...", CSTRING((*i)->className()));
00361       delete *i;
00362       DEBUG("Deleted.");
00363       ++i;
00364     }
00365   }
00366   return OK;
00367 }
00368 
00369 bool Object::compareCpuTime(const Object *o1, const Object *o2)
00370 {
00371   return o1->cpuTime() > o2->cpuTime();
00372 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1