PageRenderTime 251ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/ocaml/director.swg

#
Unknown | 102 lines | 87 code | 15 blank | 0 comment | 0 complexity | 054b955954a881738436fa27052b47d7 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * director.swg
  3. *
  4. * This file contains support for director classes that proxy
  5. * method calls from C++ to Ocaml extensions.
  6. *
  7. * ----------------------------------------------------------------------------- */
  8. #ifdef __cplusplus
  9. #include <string>
  10. # define SWIG_DIRECTOR_CAST(ARG) dynamic_cast<Swig::Director *>(ARG)
  11. namespace Swig {
  12. /* base class for director exceptions */
  13. class DirectorException {
  14. protected:
  15. std::string swig_msg;
  16. public:
  17. DirectorException(const char* msg="") {
  18. }
  19. const char *getMessage() const {
  20. return swig_msg.c_str();
  21. }
  22. virtual ~DirectorException() {}
  23. };
  24. /* type mismatch in the return value from a python method call */
  25. class DirectorTypeMismatchException : public Swig::DirectorException {
  26. public:
  27. DirectorTypeMismatchException(const char* msg="") {
  28. }
  29. };
  30. /* any python exception that occurs during a director method call */
  31. class DirectorMethodException : public Swig::DirectorException {};
  32. /* attempt to call a pure virtual method via a director method */
  33. class DirectorPureVirtualException : public Swig::DirectorException {
  34. public:
  35. DirectorPureVirtualException(const char* msg="") {
  36. }
  37. static void raise(const char *msg) {
  38. throw DirectorPureVirtualException(msg);
  39. }
  40. };
  41. /* simple thread abstraction for pthreads on win32 */
  42. #ifdef __THREAD__
  43. #define __PTHREAD__
  44. #if defined(_WIN32) || defined(__WIN32__)
  45. #define pthread_mutex_lock EnterCriticalSection
  46. #define pthread_mutex_unlock LeaveCriticalSection
  47. #define pthread_mutex_t CRITICAL_SECTION
  48. #define MUTEX_INIT(var) CRITICAL_SECTION var
  49. #else
  50. #include <pthread.h>
  51. #define MUTEX_INIT(var) pthread_mutex_t var = PTHREAD_MUTEX_INITIALIZER
  52. #endif
  53. #endif
  54. /* director base class */
  55. class Director {
  56. private:
  57. /* pointer to the wrapped ocaml object */
  58. CAML_VALUE swig_self;
  59. /* flag indicating whether the object is owned by ocaml or c++ */
  60. mutable bool swig_disown_flag;
  61. public:
  62. /* wrap a ocaml object, optionally taking ownership */
  63. Director(CAML_VALUE self) : swig_self(self), swig_disown_flag(false) {
  64. register_global_root(&swig_self);
  65. }
  66. /* discard our reference at destruction */
  67. virtual ~Director() {
  68. remove_global_root(&swig_self);
  69. swig_disown();
  70. // Disown is safe here because we're just divorcing a reference that
  71. // points to us.
  72. }
  73. /* return a pointer to the wrapped ocaml object */
  74. CAML_VALUE swig_get_self() const {
  75. return swig_self;
  76. }
  77. /* acquire ownership of the wrapped ocaml object (the sense of "disown"
  78. * is from ocaml) */
  79. void swig_disown() const {
  80. if (!swig_disown_flag) {
  81. swig_disown_flag=true;
  82. callback(*caml_named_value("caml_obj_disown"),swig_self);
  83. }
  84. }
  85. };
  86. }
  87. #endif /* __cplusplus */