llcallbacklist.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llcallbacklist.h"
00035 
00036 // Library includes
00037 #include "llerror.h"
00038 
00039 
00040 //
00041 // Globals
00042 //
00043 LLCallbackList gIdleCallbacks;
00044 
00045 //
00046 // Member functions
00047 //
00048 
00049 LLCallbackList::LLCallbackList()
00050 {
00051         // nothing
00052 }
00053 
00054 LLCallbackList::~LLCallbackList()
00055 {
00056 }
00057 
00058 
00059 void LLCallbackList::addFunction( callback_t func, void *data)
00060 {
00061         if (!func)
00062         {
00063                 llerrs << "LLCallbackList::addFunction - function is NULL" << llendl;
00064                 return;
00065         }
00066 
00067         // only add one callback per func/data pair
00068         callback_pair_t t(func, data);
00069         callback_list_t::iterator iter = std::find(mCallbackList.begin(), mCallbackList.end(), t);
00070         if (iter == mCallbackList.end())
00071         {
00072                 mCallbackList.push_back(t);
00073         }
00074 }
00075 
00076 
00077 BOOL LLCallbackList::containsFunction( callback_t func, void *data)
00078 {
00079         callback_pair_t t(func, data);
00080         callback_list_t::iterator iter = std::find(mCallbackList.begin(), mCallbackList.end(), t);
00081         if (iter != mCallbackList.end())
00082         {
00083                 return TRUE;
00084         }
00085         else
00086         {
00087                 return FALSE;
00088         }
00089 }
00090 
00091 
00092 BOOL LLCallbackList::deleteFunction( callback_t func, void *data)
00093 {
00094         callback_pair_t t(func, data);
00095         callback_list_t::iterator iter = std::find(mCallbackList.begin(), mCallbackList.end(), t);
00096         if (iter != mCallbackList.end())
00097         {
00098                 mCallbackList.erase(iter);
00099                 return TRUE;
00100         }
00101         else
00102         {
00103                 return FALSE;
00104         }
00105 }
00106 
00107 
00108 void LLCallbackList::deleteAllFunctions()
00109 {
00110         mCallbackList.clear();
00111 }
00112 
00113 
00114 void LLCallbackList::callFunctions()
00115 {
00116         for (callback_list_t::iterator iter = mCallbackList.begin(); iter != mCallbackList.end();  )
00117         {
00118                 callback_list_t::iterator curiter = iter++;
00119                 curiter->first(curiter->second);
00120         }
00121 }
00122 
00123 #ifdef _DEBUG
00124 
00125 void test1(void *data)
00126 {
00127         S32 *s32_data = (S32 *)data;
00128         llinfos << "testfunc1 " << *s32_data << llendl;
00129 }
00130 
00131 
00132 void test2(void *data)
00133 {
00134         S32 *s32_data = (S32 *)data;
00135         llinfos << "testfunc2 " << *s32_data << llendl;
00136 }
00137 
00138 
00139 void
00140 LLCallbackList::test()
00141 {
00142         S32 a = 1;
00143         S32 b = 2;
00144         LLCallbackList *list = new LLCallbackList;
00145 
00146         llinfos << "Testing LLCallbackList" << llendl;
00147 
00148         if (!list->deleteFunction(NULL))
00149         {
00150                 llinfos << "passed 1" << llendl;
00151         }
00152         else
00153         {
00154                 llinfos << "error, removed function from empty list" << llendl;
00155         }
00156 
00157         // llinfos << "This should crash" << llendl;
00158         // list->addFunction(NULL);
00159 
00160         list->addFunction(&test1, &a);
00161         list->addFunction(&test1, &a);
00162 
00163         llinfos << "Expect: test1 1, test1 1" << llendl;
00164         list->callFunctions();
00165 
00166         list->addFunction(&test1, &b);
00167         list->addFunction(&test2, &b);
00168 
00169         llinfos << "Expect: test1 1, test1 1, test1 2, test2 2" << llendl;
00170         list->callFunctions();
00171 
00172         if (list->deleteFunction(&test1, &b))
00173         {
00174                 llinfos << "passed 3" << llendl;
00175         }
00176         else
00177         {
00178                 llinfos << "error removing function" << llendl;
00179         }
00180 
00181         llinfos << "Expect: test1 1, test1 1, test2 2" << llendl;
00182         list->callFunctions();
00183 
00184         list->deleteAllFunctions();
00185 
00186         llinfos << "Expect nothing" << llendl;
00187         list->callFunctions();
00188 
00189         llinfos << "nothing :-)" << llendl;
00190 
00191         delete list;
00192 
00193         llinfos << "test complete" << llendl;
00194 }
00195 
00196 #endif  // _DEBUG

Generated on Thu Jul 1 06:08:20 2010 for Second Life Viewer by  doxygen 1.4.7