llcallbacklist.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @file llcallbacklist.h
  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. #ifndef LL_LLCALLBACKLIST_H
  33. #define LL_LLCALLBACKLIST_H
  34. #include <list>
  35. #include "boost/function.hpp"
  36. #include "llerror.h"
  37. #include "llstl.h"
  38. class LLCallbackList
  39. {
  40. protected:
  41. LOG_CLASS(LLCallbackList);
  42. public:
  43. typedef void (*callback_t)(void*);
  44. typedef std::pair<callback_t, void*> callback_pair_t;
  45. typedef std::list<callback_pair_t> callback_list_t;
  46. LLCallbackList() = default;
  47. // Registers a callback, which will be called as func(data)
  48. void addFunction(callback_t func, void* data = NULL);
  49. // true if list already contains the function/data pair
  50. LL_INLINE bool containsFunction(callback_t func, void* data = NULL)
  51. {
  52. return find(func, data) != mCallbackList.end();
  53. }
  54. // Removes the first instance of this function/data pair from the list,
  55. // false if not found
  56. bool deleteFunction(callback_t func, void* data = NULL);
  57. void callFunctions(); // Calls all functions
  58. void deleteAllFunctions();
  59. static void test();
  60. protected:
  61. LL_INLINE callback_list_t::iterator find(callback_t func, void* data)
  62. {
  63. callback_pair_t t(func, data);
  64. return std::find(mCallbackList.begin(), mCallbackList.end(), t);
  65. }
  66. protected:
  67. // Use a list so that the callbacks are ordered in case that matters
  68. callback_list_t mCallbackList;
  69. };
  70. typedef boost::function<void()> nullary_func_t;
  71. typedef boost::function<bool()> bool_func_t;
  72. // Call a given callable once in idle loop.
  73. void doOnIdleOneTime(nullary_func_t callable);
  74. // Repeatedly call a callable in idle loop until it returns true.
  75. void doOnIdleRepeating(bool_func_t callable);
  76. // Call a given callable once after specified interval.
  77. void doAfterInterval(nullary_func_t callable, F32 seconds);
  78. // Call a given callable every specified number of seconds, until it returns
  79. // true.
  80. void doPeriodically(bool_func_t callable, F32 seconds);
  81. extern LLCallbackList gIdleCallbacks;
  82. #endif