Subversion Repositories seema-scanner

Rev

Rev 1 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 205
1
/*
1
/*
2
 Copyright (c) 2012, Oliver Woodford
2
 Copyright (c) 2012, Oliver Woodford
3
 All rights reserved.
3
 All rights reserved.
4
 
4
 
5
 Redistribution and use in source and binary forms, with or without
5
 Redistribution and use in source and binary forms, with or without
6
 modification, are permitted provided that the following conditions are
6
 modification, are permitted provided that the following conditions are
7
 met:
7
 met:
8
 
8
 
9
 * Redistributions of source code must retain the above copyright
9
 * Redistributions of source code must retain the above copyright
10
 notice, this list of conditions and the following disclaimer.
10
 notice, this list of conditions and the following disclaimer.
11
 * Redistributions in binary form must reproduce the above copyright
11
 * Redistributions in binary form must reproduce the above copyright
12
 notice, this list of conditions and the following disclaimer in
12
 notice, this list of conditions and the following disclaimer in
13
 the documentation and/or other materials provided with the distribution
13
 the documentation and/or other materials provided with the distribution
14
 
14
 
15
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
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
16
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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
22
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
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
24
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
 POSSIBILITY OF SUCH DAMAGE.
25
 POSSIBILITY OF SUCH DAMAGE.
26

26

27
 */
27
 */
28
 
28
 
29
 
29
 
30
#ifndef __CLASS_HANDLE_HPP__
30
#ifndef __CLASS_HANDLE_HPP__
31
#define __CLASS_HANDLE_HPP__
31
#define __CLASS_HANDLE_HPP__
32
#include "mex.h"
32
#include "mex.h"
33
#include <stdint.h>
33
#include <stdint.h>
34
#include <string>
34
#include <string>
35
#include <cstring>
35
#include <cstring>
36
#include <typeinfo>
36
#include <typeinfo>
37
 
37
 
38
#define CLASS_HANDLE_SIGNATURE 0xFF00F0A5
38
#define CLASS_HANDLE_SIGNATURE 0xFF00F0A5
39
template<class base> class class_handle
39
template<class base> class class_handle
40
{
40
{
41
public:
41
public:
42
    class_handle(base *ptr) : ptr_m(ptr), name_m(typeid(base).name()) { signature_m = CLASS_HANDLE_SIGNATURE; }
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; }
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())); }
44
    bool isValid() { return ((signature_m == CLASS_HANDLE_SIGNATURE) && !strcmp(name_m.c_str(), typeid(base).name())); }
45
    base *ptr() { return ptr_m; }
45
    base *ptr() { return ptr_m; }
46
 
46
 
47
private:
47
private:
48
    uint32_t signature_m;
48
    uint32_t signature_m;
49
    std::string name_m;
49
    std::string name_m;
50
    base *ptr_m;
50
    base *ptr_m;
51
};
51
};
52
 
52
 
53
template<class base> inline mxArray *convertPtr2Mat(base *ptr)
53
template<class base> inline mxArray *convertPtr2Mat(base *ptr)
54
{
54
{
55
    mexLock();
55
    mexLock();
56
    mxArray *out = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
56
    mxArray *out = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
57
    *((uint64_t *)mxGetData(out)) = reinterpret_cast<uint64_t>(new class_handle<base>(ptr));
57
    *((uint64_t *)mxGetData(out)) = reinterpret_cast<uint64_t>(new class_handle<base>(ptr));
58
    return out;
58
    return out;
59
}
59
}
60
 
60
 
61
template<class base> inline class_handle<base> *convertMat2HandlePtr(const mxArray *in)
61
template<class base> inline class_handle<base> *convertMat2HandlePtr(const mxArray *in)
62
{
62
{
63
    if (mxGetNumberOfElements(in) != 1 || mxGetClassID(in) != mxUINT64_CLASS || mxIsComplex(in))
63
    if (mxGetNumberOfElements(in) != 1 || mxGetClassID(in) != mxUINT64_CLASS || mxIsComplex(in))
64
        mexErrMsgTxt("Input must be a real uint64 scalar.");
64
        mexErrMsgTxt("Input must be a real uint64 scalar.");
65
    class_handle<base> *ptr = reinterpret_cast<class_handle<base> *>(*((uint64_t *)mxGetData(in)));
65
    class_handle<base> *ptr = reinterpret_cast<class_handle<base> *>(*((uint64_t *)mxGetData(in)));
66
    if (!ptr->isValid())
66
    if (!ptr->isValid())
67
        mexErrMsgTxt("Handle not valid.");
67
        mexErrMsgTxt("Handle not valid.");
68
    return ptr;
68
    return ptr;
69
}
69
}
70
 
70
 
71
template<class base> inline base *convertMat2Ptr(const mxArray *in)
71
template<class base> inline base *convertMat2Ptr(const mxArray *in)
72
{
72
{
73
    return convertMat2HandlePtr<base>(in)->ptr();
73
    return convertMat2HandlePtr<base>(in)->ptr();
74
}
74
}
75
 
75
 
76
template<class base> inline void destroyObject(const mxArray *in)
76
template<class base> inline void destroyObject(const mxArray *in)
77
{
77
{
78
    delete convertMat2HandlePtr<base>(in);
78
    delete convertMat2HandlePtr<base>(in);
79
    mexUnlock();
79
    mexUnlock();
80
}
80
}
81
 
81
 
82
#endif // __CLASS_HANDLE_HPP__
82
#endif // __CLASS_HANDLE_HPP__
83
 
83