llcallbacklist.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @file llcallbacklist.cpp
  3. * @brief A simple list of callback functions to call.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llcallbacklist.h"
  34. #include "lleventtimer.h"
  35. LLCallbackList gIdleCallbacks;
  36. void LLCallbackList::addFunction(callback_t func, void* data)
  37. {
  38. if (!func)
  39. {
  40. llwarns << "Function is NULL" << llendl;
  41. llassert(false);
  42. return;
  43. }
  44. // only add one callback per func/data pair
  45. if (!containsFunction(func, data))
  46. {
  47. callback_pair_t t(func, data);
  48. mCallbackList.push_back(t);
  49. }
  50. }
  51. bool LLCallbackList::deleteFunction(callback_t func, void* data)
  52. {
  53. callback_list_t::iterator iter = find(func, data);
  54. if (iter != mCallbackList.end())
  55. {
  56. mCallbackList.erase(iter);
  57. return true;
  58. }
  59. else
  60. {
  61. return false;
  62. }
  63. }
  64. void LLCallbackList::deleteAllFunctions()
  65. {
  66. mCallbackList.clear();
  67. }
  68. void LLCallbackList::callFunctions()
  69. {
  70. for (callback_list_t::iterator iter = mCallbackList.begin();
  71. iter != mCallbackList.end(); )
  72. {
  73. callback_list_t::iterator curiter = iter++;
  74. curiter->first(curiter->second);
  75. }
  76. }
  77. // Shim class to allow arbitrary boost::bind expressions to be run as one-time
  78. // idle callbacks.
  79. class OnIdleCallbackOneTime
  80. {
  81. public:
  82. OnIdleCallbackOneTime(nullary_func_t callable)
  83. : mCallable(callable)
  84. {
  85. }
  86. static void onIdle(void* data)
  87. {
  88. gIdleCallbacks.deleteFunction(onIdle, data);
  89. OnIdleCallbackOneTime* self = reinterpret_cast<OnIdleCallbackOneTime*>(data);
  90. self->call();
  91. delete self;
  92. }
  93. LL_INLINE void call()
  94. {
  95. mCallable();
  96. }
  97. private:
  98. nullary_func_t mCallable;
  99. };
  100. void doOnIdleOneTime(nullary_func_t callable)
  101. {
  102. OnIdleCallbackOneTime* cb_functor = new OnIdleCallbackOneTime(callable);
  103. gIdleCallbacks.addFunction(&OnIdleCallbackOneTime::onIdle, cb_functor);
  104. }
  105. // Shim class to allow generic boost functions to be run as recurring idle
  106. // callbacks. Callable should return true when done, false to continue getting
  107. // called.
  108. class OnIdleCallbackRepeating
  109. {
  110. public:
  111. OnIdleCallbackRepeating(bool_func_t callable)
  112. : mCallable(callable)
  113. {
  114. }
  115. // Will keep getting called until the callable returns true.
  116. static void onIdle(void* data)
  117. {
  118. OnIdleCallbackRepeating* self = reinterpret_cast<OnIdleCallbackRepeating*>(data);
  119. bool done = self->call();
  120. if (done)
  121. {
  122. gIdleCallbacks.deleteFunction(onIdle, data);
  123. delete self;
  124. }
  125. }
  126. LL_INLINE bool call()
  127. {
  128. return mCallable();
  129. }
  130. private:
  131. bool_func_t mCallable;
  132. };
  133. void doOnIdleRepeating(bool_func_t callable)
  134. {
  135. OnIdleCallbackRepeating* cb_functor = new OnIdleCallbackRepeating(callable);
  136. gIdleCallbacks.addFunction(&OnIdleCallbackRepeating::onIdle, cb_functor);
  137. }
  138. class NullaryFuncEventTimer : public LLEventTimer
  139. {
  140. public:
  141. NullaryFuncEventTimer(nullary_func_t callable, F32 seconds)
  142. : LLEventTimer(seconds),
  143. mCallable(callable)
  144. {
  145. }
  146. private:
  147. LL_INLINE bool tick()
  148. {
  149. mCallable();
  150. return true;
  151. }
  152. private:
  153. nullary_func_t mCallable;
  154. };
  155. // Call a given callable once after specified interval.
  156. void doAfterInterval(nullary_func_t callable, F32 seconds)
  157. {
  158. new NullaryFuncEventTimer(callable, seconds);
  159. }
  160. class BoolFuncEventTimer : public LLEventTimer
  161. {
  162. public:
  163. BoolFuncEventTimer(bool_func_t callable, F32 seconds)
  164. : LLEventTimer(seconds),
  165. mCallable(callable)
  166. {
  167. }
  168. private:
  169. LL_INLINE bool tick()
  170. {
  171. return mCallable();
  172. }
  173. private:
  174. bool_func_t mCallable;
  175. };
  176. // Call a given callable every specified number of seconds, until it returns true.
  177. void doPeriodically(bool_func_t callable, F32 seconds)
  178. {
  179. new BoolFuncEventTimer(callable, seconds);
  180. }