PageRenderTime 44ms CodeModel.GetById 34ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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
 29#ifndef LL_LLRUN_H
 30#define LL_LLRUN_H
 31
 32#include <vector>
 33#include <boost/shared_ptr.hpp>
 34
 35class LLRunnable;
 36
 37/** 
 38 * @class LLRunner
 39 * @brief This class manages a set of LLRunnable objects.
 40 *
 41 * An instance of this class has a collection of LLRunnable objects
 42 * which are scheduled to run on a repeating or one time basis.
 43 * @see LLRunnable
 44 */
 45class LL_COMMON_API LLRunner
 46{
 47public:
 48	/**
 49	 * @brief The pointer to a runnable.
 50	 */
 51	typedef boost::shared_ptr<LLRunnable> run_ptr_t;
 52
 53	/**
 54	 * @brief The handle for use in the API.
 55	 */
 56	typedef S64 run_handle_t;
 57
 58	/**
 59	 * @brief Constructor.
 60	 */
 61	LLRunner();
 62
 63	/**
 64	 * @brief Destructor.
 65	 */
 66	~LLRunner();
 67
 68	/** 
 69	 * @brief Enumeration which specifies when to run.
 70	 */
 71	enum ERunSchedule
 72	{
 73		// The runnable will run in N seconds
 74		RUN_IN,
 75
 76		// The run every N seconds
 77		RUN_EVERY,
 78
 79		// A count of the run types
 80		RUN_SCHEDULE_COUNT
 81	};
 82
 83	/**
 84	 * @brief Run the runnables which are scheduled to run
 85	 *
 86	 * @return Returns the number of runnables run.
 87	 */
 88	S32 run();
 89
 90	/** 
 91	 * @brief Add a runnable to the run list.
 92	 *
 93	 * The handle of the runnable is unique to each addition. If the
 94	 * same runnable is added a second time with the same or different
 95	 * schedule, this method will return a new handle.
 96	 * @param runnable The runnable to run() on schedule.
 97	 * @param schedule Specifies the run schedule.
 98	 * @param seconds When to run the runnable as interpreted by schedule.
 99	 * @return Returns the handle to the runnable. handle == 0 means failure.
100	 */
101	run_handle_t addRunnable(
102		run_ptr_t runnable,
103		ERunSchedule schedule,
104		F64 seconds);
105	
106	/**
107	 * @brief Remove the specified runnable.
108	 *
109	 * @param handle The handle of the runnable to remove.
110	 * @return Returns the pointer to the runnable removed which may
111	 * be empty.
112	 */
113	run_ptr_t removeRunnable(run_handle_t handle);
114
115protected:
116	struct LLRunInfo
117	{
118		run_handle_t mHandle;
119		run_ptr_t mRunnable;
120		ERunSchedule mSchedule;
121		F64 mNextRunAt;
122		F64 mIncrement;
123		LLRunInfo(
124			run_handle_t handle,
125			run_ptr_t runnable,
126			ERunSchedule schedule,
127			F64 next_run_at,
128			F64 increment);
129	};
130	typedef std::vector<LLRunInfo> run_list_t;
131	run_list_t mRunOnce;
132	run_list_t mRunEvery;
133	run_handle_t mNextHandle;
134};
135
136
137/** 
138 * @class LLRunnable
139 * @brief Abstract base class for running some scheduled process.
140 *
141 * Users of the LLRunner class are expected to derive a concrete
142 * implementation of this class which overrides the run() method to do
143 * something useful.
144 * @see LLRunner
145 */
146class LL_COMMON_API LLRunnable
147{
148public:
149	LLRunnable();
150	virtual ~LLRunnable();
151
152	/** 
153	 * @brief Do the process.
154	 *
155	 * This method will be called from the LLRunner according to 
156	 * @param runner The Runner which call run().
157	 * @param handle The handle this run instance is run under.
158	 */
159	virtual void run(LLRunner* runner, S64 handle) = 0;
160};
161
162#endif // LL_LLRUN_H