PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llrun.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 162 lines | 52 code | 18 blank | 92 comment | 0 complexity | ed57cc04d350f293ceaf400519df89d8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llrun.h
  3. * @author Phoenix
  4. * @date 2006-02-16
  5. * @brief Declaration of LLRunner and LLRunnable classes.
  6. *
  7. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #ifndef LL_LLRUN_H
  29. #define LL_LLRUN_H
  30. #include <vector>
  31. #include <boost/shared_ptr.hpp>
  32. class LLRunnable;
  33. /**
  34. * @class LLRunner
  35. * @brief This class manages a set of LLRunnable objects.
  36. *
  37. * An instance of this class has a collection of LLRunnable objects
  38. * which are scheduled to run on a repeating or one time basis.
  39. * @see LLRunnable
  40. */
  41. class LL_COMMON_API LLRunner
  42. {
  43. public:
  44. /**
  45. * @brief The pointer to a runnable.
  46. */
  47. typedef boost::shared_ptr<LLRunnable> run_ptr_t;
  48. /**
  49. * @brief The handle for use in the API.
  50. */
  51. typedef S64 run_handle_t;
  52. /**
  53. * @brief Constructor.
  54. */
  55. LLRunner();
  56. /**
  57. * @brief Destructor.
  58. */
  59. ~LLRunner();
  60. /**
  61. * @brief Enumeration which specifies when to run.
  62. */
  63. enum ERunSchedule
  64. {
  65. // The runnable will run in N seconds
  66. RUN_IN,
  67. // The run every N seconds
  68. RUN_EVERY,
  69. // A count of the run types
  70. RUN_SCHEDULE_COUNT
  71. };
  72. /**
  73. * @brief Run the runnables which are scheduled to run
  74. *
  75. * @return Returns the number of runnables run.
  76. */
  77. S32 run();
  78. /**
  79. * @brief Add a runnable to the run list.
  80. *
  81. * The handle of the runnable is unique to each addition. If the
  82. * same runnable is added a second time with the same or different
  83. * schedule, this method will return a new handle.
  84. * @param runnable The runnable to run() on schedule.
  85. * @param schedule Specifies the run schedule.
  86. * @param seconds When to run the runnable as interpreted by schedule.
  87. * @return Returns the handle to the runnable. handle == 0 means failure.
  88. */
  89. run_handle_t addRunnable(
  90. run_ptr_t runnable,
  91. ERunSchedule schedule,
  92. F64 seconds);
  93. /**
  94. * @brief Remove the specified runnable.
  95. *
  96. * @param handle The handle of the runnable to remove.
  97. * @return Returns the pointer to the runnable removed which may
  98. * be empty.
  99. */
  100. run_ptr_t removeRunnable(run_handle_t handle);
  101. protected:
  102. struct LLRunInfo
  103. {
  104. run_handle_t mHandle;
  105. run_ptr_t mRunnable;
  106. ERunSchedule mSchedule;
  107. F64 mNextRunAt;
  108. F64 mIncrement;
  109. LLRunInfo(
  110. run_handle_t handle,
  111. run_ptr_t runnable,
  112. ERunSchedule schedule,
  113. F64 next_run_at,
  114. F64 increment);
  115. };
  116. typedef std::vector<LLRunInfo> run_list_t;
  117. run_list_t mRunOnce;
  118. run_list_t mRunEvery;
  119. run_handle_t mNextHandle;
  120. };
  121. /**
  122. * @class LLRunnable
  123. * @brief Abstract base class for running some scheduled process.
  124. *
  125. * Users of the LLRunner class are expected to derive a concrete
  126. * implementation of this class which overrides the run() method to do
  127. * something useful.
  128. * @see LLRunner
  129. */
  130. class LL_COMMON_API LLRunnable
  131. {
  132. public:
  133. LLRunnable();
  134. virtual ~LLRunnable();
  135. /**
  136. * @brief Do the process.
  137. *
  138. * This method will be called from the LLRunner according to
  139. * @param runner The Runner which call run().
  140. * @param handle The handle this run instance is run under.
  141. */
  142. virtual void run(LLRunner* runner, S64 handle) = 0;
  143. };
  144. #endif // LL_LLRUN_H