PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llcallbacklist.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 256 lines | 170 code | 44 blank | 42 comment | 12 complexity | 92823f076c4c35e1b353c17fc25f031e MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llcallbacklist.cpp
  3. * @brief A simple list of callback functions to call.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llcallbacklist.h"
  28. // Library includes
  29. #include "llerror.h"
  30. //
  31. // Globals
  32. //
  33. LLCallbackList gIdleCallbacks;
  34. //
  35. // Member functions
  36. //
  37. LLCallbackList::LLCallbackList()
  38. {
  39. // nothing
  40. }
  41. LLCallbackList::~LLCallbackList()
  42. {
  43. }
  44. void LLCallbackList::addFunction( callback_t func, void *data)
  45. {
  46. if (!func)
  47. {
  48. llerrs << "LLCallbackList::addFunction - function is NULL" << llendl;
  49. return;
  50. }
  51. // only add one callback per func/data pair
  52. callback_pair_t t(func, data);
  53. callback_list_t::iterator iter = std::find(mCallbackList.begin(), mCallbackList.end(), t);
  54. if (iter == mCallbackList.end())
  55. {
  56. mCallbackList.push_back(t);
  57. }
  58. }
  59. BOOL LLCallbackList::containsFunction( callback_t func, void *data)
  60. {
  61. callback_pair_t t(func, data);
  62. callback_list_t::iterator iter = std::find(mCallbackList.begin(), mCallbackList.end(), t);
  63. if (iter != mCallbackList.end())
  64. {
  65. return TRUE;
  66. }
  67. else
  68. {
  69. return FALSE;
  70. }
  71. }
  72. BOOL LLCallbackList::deleteFunction( callback_t func, void *data)
  73. {
  74. callback_pair_t t(func, data);
  75. callback_list_t::iterator iter = std::find(mCallbackList.begin(), mCallbackList.end(), t);
  76. if (iter != mCallbackList.end())
  77. {
  78. mCallbackList.erase(iter);
  79. return TRUE;
  80. }
  81. else
  82. {
  83. return FALSE;
  84. }
  85. }
  86. void LLCallbackList::deleteAllFunctions()
  87. {
  88. mCallbackList.clear();
  89. }
  90. void LLCallbackList::callFunctions()
  91. {
  92. for (callback_list_t::iterator iter = mCallbackList.begin(); iter != mCallbackList.end(); )
  93. {
  94. callback_list_t::iterator curiter = iter++;
  95. curiter->first(curiter->second);
  96. }
  97. }
  98. // Shim class to allow arbitrary boost::bind
  99. // expressions to be run as one-time idle callbacks.
  100. class OnIdleCallbackOneTime
  101. {
  102. public:
  103. OnIdleCallbackOneTime(nullary_func_t callable):
  104. mCallable(callable)
  105. {
  106. }
  107. static void onIdle(void *data)
  108. {
  109. gIdleCallbacks.deleteFunction(onIdle, data);
  110. OnIdleCallbackOneTime* self = reinterpret_cast<OnIdleCallbackOneTime*>(data);
  111. self->call();
  112. delete self;
  113. }
  114. void call()
  115. {
  116. mCallable();
  117. }
  118. private:
  119. nullary_func_t mCallable;
  120. };
  121. void doOnIdleOneTime(nullary_func_t callable)
  122. {
  123. OnIdleCallbackOneTime* cb_functor = new OnIdleCallbackOneTime(callable);
  124. gIdleCallbacks.addFunction(&OnIdleCallbackOneTime::onIdle,cb_functor);
  125. }
  126. // Shim class to allow generic boost functions to be run as
  127. // recurring idle callbacks. Callable should return true when done,
  128. // false to continue getting called.
  129. class OnIdleCallbackRepeating
  130. {
  131. public:
  132. OnIdleCallbackRepeating(bool_func_t callable):
  133. mCallable(callable)
  134. {
  135. }
  136. // Will keep getting called until the callable returns true.
  137. static void onIdle(void *data)
  138. {
  139. OnIdleCallbackRepeating* self = reinterpret_cast<OnIdleCallbackRepeating*>(data);
  140. bool done = self->call();
  141. if (done)
  142. {
  143. gIdleCallbacks.deleteFunction(onIdle, data);
  144. delete self;
  145. }
  146. }
  147. bool call()
  148. {
  149. return mCallable();
  150. }
  151. private:
  152. bool_func_t mCallable;
  153. };
  154. void doOnIdleRepeating(bool_func_t callable)
  155. {
  156. OnIdleCallbackRepeating* cb_functor = new OnIdleCallbackRepeating(callable);
  157. gIdleCallbacks.addFunction(&OnIdleCallbackRepeating::onIdle,cb_functor);
  158. }
  159. #ifdef _DEBUG
  160. void test1(void *data)
  161. {
  162. S32 *s32_data = (S32 *)data;
  163. llinfos << "testfunc1 " << *s32_data << llendl;
  164. }
  165. void test2(void *data)
  166. {
  167. S32 *s32_data = (S32 *)data;
  168. llinfos << "testfunc2 " << *s32_data << llendl;
  169. }
  170. void
  171. LLCallbackList::test()
  172. {
  173. S32 a = 1;
  174. S32 b = 2;
  175. LLCallbackList *list = new LLCallbackList;
  176. llinfos << "Testing LLCallbackList" << llendl;
  177. if (!list->deleteFunction(NULL))
  178. {
  179. llinfos << "passed 1" << llendl;
  180. }
  181. else
  182. {
  183. llinfos << "error, removed function from empty list" << llendl;
  184. }
  185. // llinfos << "This should crash" << llendl;
  186. // list->addFunction(NULL);
  187. list->addFunction(&test1, &a);
  188. list->addFunction(&test1, &a);
  189. llinfos << "Expect: test1 1, test1 1" << llendl;
  190. list->callFunctions();
  191. list->addFunction(&test1, &b);
  192. list->addFunction(&test2, &b);
  193. llinfos << "Expect: test1 1, test1 1, test1 2, test2 2" << llendl;
  194. list->callFunctions();
  195. if (list->deleteFunction(&test1, &b))
  196. {
  197. llinfos << "passed 3" << llendl;
  198. }
  199. else
  200. {
  201. llinfos << "error removing function" << llendl;
  202. }
  203. llinfos << "Expect: test1 1, test1 1, test2 2" << llendl;
  204. list->callFunctions();
  205. list->deleteAllFunctions();
  206. llinfos << "Expect nothing" << llendl;
  207. list->callFunctions();
  208. llinfos << "nothing :-)" << llendl;
  209. delete list;
  210. llinfos << "test complete" << llendl;
  211. }
  212. #endif // _DEBUG