Subversion Repositories seema-scanner

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jakw 1
/*
2
 Copyright (c) 2012, Oliver Woodford
3
 All rights reserved.
4
 
5
 Redistribution and use in source and binary forms, with or without
6
 modification, are permitted provided that the following conditions are
7
 met:
8
 
9
 * Redistributions of source code must retain the above copyright
10
 notice, this list of conditions and the following disclaimer.
11
 * Redistributions in binary form must reproduce the above copyright
12
 notice, this list of conditions and the following disclaimer in
13
 the documentation and/or other materials provided with the distribution
14
 
15
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
 POSSIBILITY OF SUCH DAMAGE.
26
 
27
 */
28
 
29
 
30
#ifndef __CLASS_HANDLE_HPP__
31
#define __CLASS_HANDLE_HPP__
32
#include "mex.h"
33
#include <stdint.h>
34
#include <string>
35
#include <cstring>
36
#include <typeinfo>
37
 
38
#define CLASS_HANDLE_SIGNATURE 0xFF00F0A5
39
template<class base> class class_handle
40
{
41
public:
42
    class_handle(base *ptr) : ptr_m(ptr), name_m(typeid(base).name()) { signature_m = CLASS_HANDLE_SIGNATURE; }
43
    ~class_handle() { signature_m = 0; delete ptr_m; }
44
    bool isValid() { return ((signature_m == CLASS_HANDLE_SIGNATURE) && !strcmp(name_m.c_str(), typeid(base).name())); }
45
    base *ptr() { return ptr_m; }
46
 
47
private:
48
    uint32_t signature_m;
49
    std::string name_m;
50
    base *ptr_m;
51
};
52
 
53
template<class base> inline mxArray *convertPtr2Mat(base *ptr)
54
{
55
    mexLock();
56
    mxArray *out = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
57
    *((uint64_t *)mxGetData(out)) = reinterpret_cast<uint64_t>(new class_handle<base>(ptr));
58
    return out;
59
}
60
 
61
template<class base> inline class_handle<base> *convertMat2HandlePtr(const mxArray *in)
62
{
63
    if (mxGetNumberOfElements(in) != 1 || mxGetClassID(in) != mxUINT64_CLASS || mxIsComplex(in))
64
        mexErrMsgTxt("Input must be a real uint64 scalar.");
65
    class_handle<base> *ptr = reinterpret_cast<class_handle<base> *>(*((uint64_t *)mxGetData(in)));
66
    if (!ptr->isValid())
67
        mexErrMsgTxt("Handle not valid.");
68
    return ptr;
69
}
70
 
71
template<class base> inline base *convertMat2Ptr(const mxArray *in)
72
{
73
    return convertMat2HandlePtr<base>(in)->ptr();
74
}
75
 
76
template<class base> inline void destroyObject(const mxArray *in)
77
{
78
    delete convertMat2HandlePtr<base>(in);
79
    mexUnlock();
80
}
81
 
82
#endif // __CLASS_HANDLE_HPP__