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