File: contextmgr.h

package info (click to toggle)
angelscript 2.35.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,388 kB
  • sloc: cpp: 71,969; asm: 1,558; makefile: 665; xml: 214; javascript: 42; python: 22; ansic: 22; sh: 7
file content (99 lines) | stat: -rw-r--r-- 3,325 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef CONTEXTMGR_H
#define CONTEXTMGR_H

// The context manager simplifies the management of multiple concurrent scripts

// More than one context manager can be used, if you wish to control different
// groups of scripts separately, e.g. game object scripts, and GUI scripts.

// OBSERVATION: This class is currently not thread safe.

#ifndef ANGELSCRIPT_H 
// Avoid having to inform include path if header is already include before
#include <angelscript.h>
#endif

#include <vector>

BEGIN_AS_NAMESPACE

class CScriptDictionary;

// The internal structure for holding contexts
struct SContextInfo;

// The signature of the get time callback function
typedef asUINT (*TIMEFUNC_t)();

class CContextMgr
{
public:
	CContextMgr();
	~CContextMgr();

	// Set the function that the manager will use to obtain the time in milliseconds
	void SetGetTimeCallback(TIMEFUNC_t func);

	// Registers the following:
	//
	//  void sleep(uint milliseconds)
	//
	// The application must set the get time callback for this to work
	void RegisterThreadSupport(asIScriptEngine *engine);

	// Registers the following:
	//
	//  funcdef void coroutine(dictionary@)
	//  void createCoRoutine(coroutine @func, dictionary @args)
	//  void yield()
	void RegisterCoRoutineSupport(asIScriptEngine *engine);

	// Create a new context, prepare it with the function id, then return 
	// it so that the application can pass the argument values. The context
	// will be released by the manager after the execution has completed.
	// Set keepCtxAfterExecution to true if the application needs to retrieve
	// information from the context after it the script has finished. 
	asIScriptContext *AddContext(asIScriptEngine *engine, asIScriptFunction *func, bool keepCtxAfterExecution = false);

	// If the context was kept after the execution, this method must be 
	// called when the application is done with the context so it can be
	// returned to the pool for reuse.
	void DoneWithContext(asIScriptContext *ctx);

	// Create a new context, prepare it with the function id, then return
	// it so that the application can pass the argument values. The context
	// will be added as a co-routine in the same thread as the currCtx.
	asIScriptContext *AddContextForCoRoutine(asIScriptContext *currCtx, asIScriptFunction *func);

	// Execute each script that is not currently sleeping. The function returns after 
	// each script has been executed once. The application should call this function
	// for each iteration of the message pump, or game loop, or whatever.
	// Returns the number of scripts still in execution.
	int ExecuteScripts();

	// Put a script to sleep for a while
	void SetSleeping(asIScriptContext *ctx, asUINT milliSeconds);

	// Switch the execution to the next co-routine in the group.
	// Returns true if the switch was successful.
	void NextCoRoutine();

	// Abort all scripts
	void AbortAll();

protected:
	std::vector<SContextInfo*> m_threads;
	std::vector<SContextInfo*> m_freeThreads;
	asUINT                     m_currentThread;
	TIMEFUNC_t                 m_getTimeFunc;

	// Statistics for Garbage Collection
	asUINT   m_numExecutions;
	asUINT   m_numGCObjectsCreated;
	asUINT   m_numGCObjectsDestroyed;
};


END_AS_NAMESPACE

#endif