Signal.h

Go to the documentation of this file.
00001 #ifndef __SIGNAL_H__
00002 #define __SIGNAL_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include "Error.h"
00011 
00012 #include "SignalDeclaration.h"
00013 
00014 // Implementation.
00015 
00016 template<class M>
00017 Signal<M>::Signal(const String &className, const String &signalName) : SignalBase(className, signalName)
00018 {
00019 }
00020 
00021 template<class M>
00022 Signal<M>::~Signal()
00023 {
00024   int res;
00025 
00026   if (_orphanSlotObject) {
00027     if (FAILED(res = unbind((Object *)_orphanSlotObject)))
00028       ERROR_BACKTRACE(res);
00029   } else {
00030       SlotMap::iterator i = _slots.begin();
00031       SlotMap::iterator e = _slots.end();
00032 
00033       while (i != e) {
00034         if (FAILED(res = unbind((Object *)(i->first))))
00035           ERROR_BACKTRACE(res);
00036         ++i;
00037       }
00038     }
00039 }
00040 
00041 template<class M>
00042 int Signal<M>::connect(const Object *slotObject, const M slotMethod)
00043 {
00044   if (!slotObject || !slotMethod)
00045     return ERROR(MSG_NULL_POINTER);
00046 
00047   SlotMap::const_iterator f = _slots.find(slotObject);
00048   int res;
00049 
00050   if (f == _slots.end())
00051     if (FAILED(res = bind((Object *)slotObject)))
00052       return ERROR_BACKTRACE(res);
00053 
00054   if (_slots.empty()) {
00055     _orphanSlotObject = slotObject;
00056     _orphanSlotMethod = (SlotMethod)slotMethod;
00057   } else
00058       _orphanSlotObject = NULL;
00059   _slots[slotObject].push_back((SlotMethod)slotMethod);
00060 
00061   return OK;
00062 }
00063 
00064 template<class M>
00065 int Signal<M>::disconnect(const Object *slotObject)
00066 {
00067   if (!slotObject)
00068     return ERROR(MSG_NULL_POINTER);
00069 
00070   int res;
00071 
00072   if (_orphanSlotObject == slotObject)
00073     _orphanSlotObject = NULL;
00074   else {
00075     SlotMap::iterator f = _slots.find(slotObject);
00076     if (f == _slots.end())
00077       return ERROR(MSG_SIGNAL_SLOT_DOES_NOT_EXISTS, CSTRING(slotObject->className()), slotObject->id());
00078 
00079     _slots.erase(f);
00080   }
00081 
00082   if (FAILED(res = unbind((Object *)slotObject)))
00083     return ERROR_BACKTRACE(res);
00084 
00085   return OK;
00086 }
00087 
00088 template<class M>
00089 int Signal<M>::disconnect(const Object *slotObject, const M slotMethod)
00090 {
00091   if (!slotObject || !slotMethod)
00092     return ERROR(MSG_NULL_POINTER);
00093 
00094   DEBUG("Disconnectiong slot 0x%X->0x%X.", slotObject, slotMethod);
00095 
00096   int res;
00097 
00098   if (_orphanSlotObject == slotObject && _orphanSlotMethod == slotMethod) {
00099     _orphanSlotObject = NULL;
00100     if (FAILED(res = unbind((Object *)slotObject)))
00101       return ERROR_BACKTRACE(res);
00102   } else {
00103       SlotMap::iterator f = _slots.find(slotObject);
00104       if (f == _slots.end())
00105         return ERROR(MSG_SIGNAL_SLOT_DOES_NOT_EXISTS, CSTRING(slotObject->className()), slotObject->id());
00106 
00107       unsigned int i;
00108       bool found = false;
00109 
00110       for (i = 0; i < f->second.size(); ++i)
00111         if (f->second[i] == slotMethod) {
00112           f->second.erase(i);
00113           found = true;
00114         }
00115 
00116       if (!found)
00117         return ERROR(MSG_SIGNAL_SLOT_DOES_NOT_EXISTS, CSTRING(slotObject->className()), slotObject->id());
00118 
00119       if (_slots.empty())
00120         if (FAILED(res = unbind((Object *)slotObject)))
00121           return ERROR_BACKTRACE(res);
00122     }
00123 
00124   return OK;
00125 }
00126 
00127 #endif

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