File: IExecutionContext.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (47 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include "Executor.h"

namespace executor {

/**
 * @brief Abstract class representing the context of an execution.
 *
 * This is intended to be used with executors to ensure that work items are only executed in the same context as they
 * were started. See runInContext() for how to use this with executors.
 */
class IExecutionContext {
  public:
	enum class State {
		Valid,     //! We are currently in the state captured in this context
		Suspended, //! We are in a state that is not valid but may return to the captured state
		Invalid,   //! We are in an unrelated state
	};

	using Callback = std::function<Executor::CallbackResult(State contextState)>;

	virtual ~IExecutionContext() = default;

	/**
	 * @brief Determines the state of the context
	 * @return The context state
	 */
	virtual State determineContextState() const = 0;
};

/**
 * @brief Wraps the specified callback in an execution context
 *
 * The return value is a work item for an executor that will only call the passed function when the context is valid.
 * The function will not be called when the context is suspended. Should the context become invalid, the function will
 * be called one last time with State::Invalid to indicate to the function that the context became invalid. This is
 * useful to do cleanup work (e.g. setting an error state in created Lua Promises).
 *
 * @param context The execution context
 * @param func The function to wrap
 * @return An executor work item
 */
Executor::Callback runInContext(const std::shared_ptr<IExecutionContext>& context,
	const IExecutionContext::Callback& func);

} // namespace executor