classHandleMtoC.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file classHandleMtoC.h
  3. * @brief Generic class to pass C++ objects to matlab (Interface and inline implementations)
  4. * @author Alexander Freytag
  5. * @date 19-12-2013 (dd-mm-yyyy)
  6. */
  7. #ifndef _NICE_CLASSHANDLEMTOCINCLUDE
  8. #define _NICE_CLASSHANDLEMTOCINCLUDE
  9. #include "mex.h"
  10. #include <stdint.h>
  11. #include <iostream>
  12. #include <string>
  13. #include <cstring>
  14. #include <typeinfo>
  15. #define CLASS_HANDLE_SIGNATURE 0xFF00F0A3
  16. /**
  17. * @class FMKGPHyperparameterOptimization
  18. * @brief Generic class to pass C++ objects to matlab
  19. * @author Alexander Freytag
  20. */
  21. template<class objectClass> class ClassHandle
  22. {
  23. private:
  24. //!
  25. uint32_t i_mySignature;
  26. //! typeid.name of object class we refere to
  27. std::string s_myName;
  28. //! the actual pointer to our C++ object
  29. objectClass* p_myPtr;
  30. public:
  31. /**
  32. * @brief standard constructor
  33. *
  34. * @param ptr pointer to the c++ object
  35. */
  36. ClassHandle ( objectClass* p_ptr ) : p_myPtr(p_ptr), s_myName( typeid(objectClass).name() )
  37. {
  38. i_mySignature = CLASS_HANDLE_SIGNATURE;
  39. }
  40. /**
  41. * @brief standard destructor
  42. */
  43. ~ClassHandle()
  44. {
  45. // reset internal variables
  46. i_mySignature = 0;
  47. // clearn up data
  48. delete p_myPtr;
  49. }
  50. /**
  51. * @brief check whether the class handle was initialized properly, i.e., we point to an actual object
  52. */
  53. bool isValid()
  54. {
  55. return ( (i_mySignature == CLASS_HANDLE_SIGNATURE) && !strcmp( s_myName.c_str(), typeid(objectClass).name() ) );
  56. }
  57. /**
  58. * @brief get the pointer to the actual object
  59. */
  60. objectClass * getPtrToObject()
  61. {
  62. return p_myPtr;
  63. }
  64. };
  65. ////////////////////////////////////////////
  66. // conversion methods //
  67. ////////////////////////////////////////////
  68. /**
  69. * @brief convert handle to C++ object into matlab usable data
  70. */
  71. template<class objectClass> inline mxArray *convertPtr2Mat(objectClass *ptr)
  72. {
  73. // prevent user from clearing the mex file! Otherwise, storage leaks might be caused
  74. mexLock();
  75. // allocate memory
  76. mxArray *out = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
  77. // convert handle do matlab usable data
  78. *((uint64_t *)mxGetData(out)) = reinterpret_cast<uint64_t>(new ClassHandle<objectClass>(ptr));
  79. return out;
  80. }
  81. /**
  82. * @brief convert matlab usable data referring to an object into handle to C++ object
  83. */
  84. template<class objectClass> inline ClassHandle<objectClass> *convertMat2HandlePtr(const mxArray *in)
  85. {
  86. // check that the given pointer actually points to a real object
  87. if ( ( mxGetNumberOfElements(in) != 1 ) ||
  88. ( mxGetClassID(in) != mxUINT64_CLASS ) ||
  89. mxIsComplex(in)
  90. )
  91. mexErrMsgTxt("Input must be a real uint64 scalar.");
  92. ClassHandle<objectClass> *ptr = reinterpret_cast<ClassHandle<objectClass> *>(*((uint64_t *)mxGetData(in)));
  93. if (!ptr->isValid())
  94. mexErrMsgTxt("Handle not valid.");
  95. return ptr;
  96. }
  97. /**
  98. * @brief convert matlab usable data referring to an object into direct pointer to the underlying C++ object
  99. */
  100. template<class objectClass> inline objectClass *convertMat2Ptr(const mxArray *in)
  101. {
  102. return convertMat2HandlePtr<objectClass>(in)->getPtrToObject();
  103. }
  104. /**
  105. * @brief convert matlab usable data referring to an object into direct pointer to the underlying C++ object
  106. */
  107. template<class objectClass> inline void destroyObject(const mxArray *in)
  108. {
  109. // clean up
  110. delete convertMat2HandlePtr<objectClass>(in);
  111. // storage is freed, so users can savely clear the mex file again at any time...
  112. mexUnlock();
  113. }
  114. #endif // _NICE_CLASSHANDLEMTOCINCLUDE