PageRenderTime 37ms CodeModel.GetById 30ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/visualizations/Vortex/angelscript/add_on/contextmgr/contextmgr.h

http://github.com/xbmc/xbmc
C++ Header | 81 lines | 30 code | 22 blank | 29 comment | 0 complexity | 746a70b09cd594152d12f4728fb92874 MD5 | raw file
 1#ifndef CONTEXTMGR_H
 2#define CONTEXTMGR_H
 3
 4// The context manager simplifies the management of multiple concurrent scripts
 5
 6// More than one context manager can be used, if you wish to control different
 7// groups of scripts separately, e.g. game object scripts, and GUI scripts.
 8
 9// OBSERVATION: This class is currently not thread safe.
10
11#include <angelscript.h>
12#include <vector>
13
14BEGIN_AS_NAMESPACE
15
16class CScriptAny;
17
18// The internal structure for holding contexts
19struct SContextInfo;
20
21// The signature of the get time callback function
22typedef asUINT (*TIMEFUNC_t)();
23
24class CContextMgr
25{
26public:
27    CContextMgr();
28    ~CContextMgr();
29
30	// Set the function that the manager will use to obtain the time in milliseconds
31    void SetGetTimeCallback(TIMEFUNC_t func);
32
33    // Registers the script function
34	//
35	//  void sleep(uint milliseconds)
36	//
37    // The application must set the get time callback for this to work
38    void RegisterThreadSupport(asIScriptEngine *engine);
39
40	// Registers the script functions
41	//
42	//  void createCoRoutine(const string &in functionName, any @arg)
43	//  void yield()
44	void RegisterCoRoutineSupport(asIScriptEngine *engine);
45
46	// Create a new context, prepare it with the function id, then return 
47	// it so that the application can pass the argument values. The context
48	// will be released by the manager after the execution has completed.
49    asIScriptContext *AddContext(asIScriptEngine *engine, int funcId);
50
51	// Create a new context, prepare it with the function id, then return
52	// it so that the application can pass the argument values. The context
53	// will be added as a co-routine in the same thread as the currCtx.
54	asIScriptContext *AddContextForCoRoutine(asIScriptContext *currCtx, int funcId);
55
56	// Execute each script that is not currently sleeping. The function returns after 
57	// each script has been executed once. The application should call this function
58	// for each iteration of the message pump, or game loop, or whatever.
59    void ExecuteScripts();
60
61	// Put a script to sleep for a while
62    void SetSleeping(asIScriptContext *ctx, asUINT milliSeconds);
63
64	// Switch the execution to the next co-routine in the group.
65	// Returns true if the switch was successful.
66	void NextCoRoutine();
67
68	// Abort all scripts
69    void AbortAll();
70
71protected:
72	std::vector<SContextInfo*> threads;
73	std::vector<SContextInfo*> freeThreads;
74	asUINT                     currentThread;
75    TIMEFUNC_t                 getTimeFunc;
76};
77
78
79END_AS_NAMESPACE
80
81#endif