123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #ifndef _NICE_CLASSHANDLEMTOCINCLUDE
- #define _NICE_CLASSHANDLEMTOCINCLUDE
- #include <mex.h>
- #include <stdint.h>
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <typeinfo>
- #define CLASS_HANDLE_SIGNATURE 0xFF00F0A3
-
- template<class objectClass> class ClassHandle
- {
- private:
-
- uint32_t i_mySignature;
-
-
- std::string s_myName;
-
-
- objectClass* p_myPtr;
-
- public:
-
-
-
- ClassHandle ( objectClass* p_ptr ) : p_myPtr(p_ptr), s_myName( typeid(objectClass).name() )
- {
- i_mySignature = CLASS_HANDLE_SIGNATURE;
- }
-
-
-
- ~ClassHandle()
- {
-
- i_mySignature = 0;
-
-
- delete p_myPtr;
- }
-
-
-
- bool isValid()
- {
- return ( (i_mySignature == CLASS_HANDLE_SIGNATURE) && !strcmp( s_myName.c_str(), typeid(objectClass).name() ) );
- }
-
-
-
- objectClass * getPtrToObject()
- {
- return p_myPtr;
- }
- };
-
- template<class objectClass> inline mxArray *convertPtr2Mat(objectClass *ptr)
- {
-
- mexLock();
-
-
- mxArray *out = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
-
-
- *((uint64_t *)mxGetData(out)) = reinterpret_cast<uint64_t>(new ClassHandle<objectClass>(ptr));
-
- return out;
- }
-
- template<class objectClass> inline ClassHandle<objectClass> *convertMat2HandlePtr(const mxArray *in)
- {
-
- if ( ( mxGetNumberOfElements(in) != 1 ) ||
- ( mxGetClassID(in) != mxUINT64_CLASS ) ||
- mxIsComplex(in)
- )
- mexErrMsgTxt("Input must be a real uint64 scalar.");
-
- ClassHandle<objectClass> *ptr = reinterpret_cast<ClassHandle<objectClass> *>(*((uint64_t *)mxGetData(in)));
-
- if (!ptr->isValid())
- mexErrMsgTxt("Handle not valid.");
-
- return ptr;
- }
-
- template<class objectClass> inline objectClass *convertMat2Ptr(const mxArray *in)
- {
- return convertMat2HandlePtr<objectClass>(in)->getPtrToObject();
- }
-
- template<class objectClass> inline void destroyObject(const mxArray *in)
- {
-
- delete convertMat2HandlePtr<objectClass>(in);
-
-
- mexUnlock();
- }
- #endif
|