File: LuaThread.h

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (77 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download | duplicates (4)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once

#include "LuaConvert.h"
#include "LuaTypes.h"
#include "LuaValue.h"
#include "LuaFunction.h"

#include <iterator>

namespace luacpp {

/**
 * @brief Class to improve handling of lua threads (aka coroutines).
 *
 * This class provides a high-level interface to lua threads.
 *
 * @see LuaConvert
 */
class LuaThread : public LuaValue {

	LuaReference deleterUserdata;
	LuaTable deleterTable;
	LuaFunction deleterFunc;
  public:
	using ErrorCallback = std::function<bool(lua_State* mainState, lua_State* thread)>;
	struct ResumeState
	{
		bool completed = false;
		LuaValueList returnVals;
	};

	/**
	 * @brief Creates a new empty thread.
	 */
	static LuaThread create(lua_State* L, const LuaFunction& func);

	/**
	 * @brief Default constructor
	 */
	LuaThread();

	//Copying threads is VERY illegal
	LuaThread(const LuaThread&) = delete;
	LuaThread& operator=(const LuaThread&) = delete;

	LuaThread(LuaThread&&); 
	LuaThread& operator=(LuaThread&&);

	~LuaThread() override;

	/**
	 * @brief Sets a new reference.
	 * This overload checks if the passed reference is a thread
	 *
	 * @param ref The new reference
	 * @return void
	 */
	void setReference(const LuaReference& ref) override;

	void setErrorCallback(ErrorCallback errorCallback);

	ResumeState resume(const LuaValueList& params) const;

	lua_State* getThreadHandle() const;
  private:
	LuaThread(lua_State* luaState, lua_State* threadHandle);

	lua_State* _thread = nullptr;
	ErrorCallback _errorCallback;
};

namespace convert {

bool popValue(lua_State* luaState, LuaThread& target, int stackposition = -1, bool remove = true);

}
} // namespace luacpp