Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
1cdbcb5a
Commit
1cdbcb5a
authored
Dec 23, 2016
by
Davis King
Browse files
merged
parents
9beac18b
bbe8b12d
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
237 additions
and
76 deletions
+237
-76
dlib/matlab/call_matlab.h
dlib/matlab/call_matlab.h
+77
-2
dlib/matlab/example_mex_function.cpp
dlib/matlab/example_mex_function.cpp
+4
-0
dlib/matlab/mex_wrapper.cpp
dlib/matlab/mex_wrapper.cpp
+156
-74
No files found.
dlib/matlab/call_matlab.h
View file @
1cdbcb5a
...
@@ -5,12 +5,29 @@
...
@@ -5,12 +5,29 @@
#define MIT_LL_CALL_MATLAB_H__
#define MIT_LL_CALL_MATLAB_H__
#include <string>
#include <string>
#include <sstream>
#include <dlib/error.h>
#include <dlib/assert.h>
namespace
dlib
namespace
dlib
{
{
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
struct
invalid_args_exception
:
error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception thrown when the mex wrapper tries to convert a matlab
object into a C++ object but for whatever reason can't (usually because the
types don't match).
!*/
invalid_args_exception
(
const
std
::
string
&
msg_
)
:
error
(
msg_
)
{}
invalid_args_exception
(
const
std
::
ostringstream
&
msg_
)
:
error
(
msg_
.
str
())
{}
};
// ----------------------------------------------------------------------------------------
void
check_for_matlab_ctrl_c
();
void
check_for_matlab_ctrl_c
();
/*!
/*!
ensures
ensures
...
@@ -20,6 +37,62 @@ void check_for_matlab_ctrl_c();
...
@@ -20,6 +37,62 @@ void check_for_matlab_ctrl_c();
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class
matlab_object
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is a simple wrapper around matlab's generic mxArray, which is the
thing that is matlab's "anything object". So a matlab_object can be used as an
argument to a mex_function() that can bind to any matlab object at all. It can
also bind to "nothing" and so is inherently also an optional argument when
present in a mex_funciton().
!*/
public:
matlab_object
()
:
handle
(
0
),
should_free
(
false
),
arg_idx
(
0
)
{}
matlab_object
(
const
matlab_object
&
)
=
delete
;
~
matlab_object
();
// Check if a matlab object is bound to this object.
bool
is_empty
()
const
{
return
handle
==
0
;
}
operator
bool
()
const
{
return
handle
!=
0
;
}
// Convert from MATLAB to C++, throw invalid_args_exception if not possible.
template
<
typename
T
>
operator
T
()
const
;
template
<
typename
T
>
void
get
(
T
&
item
)
const
;
// Convert from a C++ object to MATLAB
template
<
typename
T
>
matlab_object
&
operator
=
(
const
T
&
new_val
);
template
<
typename
T
>
bool
try_get
(
T
&
item
)
const
{
try
{
get
(
item
);
return
true
;
}
catch
(
invalid_args_exception
&
)
{
return
false
;
}
}
const
void
*
get_handle
()
const
{
return
handle
;
}
/*!
ensures
- returns a pointer to the mxArray object. Might be NULL.
!*/
matlab_object
&
operator
=
(
const
matlab_object
&
)
=
delete
;
// Users shouldn't call these functions
const
void
*
release_object_to_matlab
()
{
const
void
*
temp
=
handle
;
handle
=
0
;
return
temp
;
}
void
set_object_handle
(
int
arg_idx_
,
const
void
*
sh
)
{
DLIB_CASSERT
(
!
handle
);
handle
=
sh
;
arg_idx
=
arg_idx_
;
}
private:
const
void
*
handle
;
bool
should_free
;
int
arg_idx
;
};
// ----------------------------------------------------------------------------------------
class
matlab_struct
class
matlab_struct
{
{
/*!
/*!
...
@@ -42,7 +115,8 @@ class matlab_struct
...
@@ -42,7 +115,8 @@ class matlab_struct
class
sub
;
class
sub
;
public:
public:
matlab_struct
()
:
struct_handle
(
0
),
should_free
(
false
)
{}
matlab_struct
()
:
struct_handle
(
0
),
should_free
(
false
),
arg_idx
(
0
)
{}
matlab_struct
(
const
matlab_struct
&
)
=
delete
;
~
matlab_struct
();
~
matlab_struct
();
const
sub
operator
[]
(
const
std
::
string
&
name
)
const
;
const
sub
operator
[]
(
const
std
::
string
&
name
)
const
;
...
@@ -50,7 +124,7 @@ public:
...
@@ -50,7 +124,7 @@ public:
bool
has_field
(
const
std
::
string
&
name
)
const
;
bool
has_field
(
const
std
::
string
&
name
)
const
;
const
void
*
release_struct_to_matlab
()
{
const
void
*
temp
=
struct_handle
;
struct_handle
=
0
;
return
temp
;
}
const
void
*
release_struct_to_matlab
()
{
const
void
*
temp
=
struct_handle
;
struct_handle
=
0
;
return
temp
;
}
void
set_struct_handle
(
const
void
*
sh
)
{
struct_handle
=
sh
;
}
void
set_struct_handle
(
int
arg_idx_
,
const
void
*
sh
)
{
DLIB_CASSERT
(
!
struct_handle
);
struct_handle
=
sh
;
arg_idx
=
arg_idx_
;
}
private:
private:
class
sub
class
sub
...
@@ -72,6 +146,7 @@ private:
...
@@ -72,6 +146,7 @@ private:
};
};
const
void
*
struct_handle
;
const
void
*
struct_handle
;
bool
should_free
;
bool
should_free
;
int
arg_idx
;
matlab_struct
&
operator
=
(
const
matlab_struct
&
);
matlab_struct
&
operator
=
(
const
matlab_struct
&
);
};
};
...
...
dlib/matlab/example_mex_function.cpp
View file @
1cdbcb5a
...
@@ -46,6 +46,10 @@ using namespace std;
...
@@ -46,6 +46,10 @@ using namespace std;
- Types corresponding to a MATLAB cell array
- Types corresponding to a MATLAB cell array
- a std::vector or dlib::array containing any of the above
- a std::vector or dlib::array containing any of the above
types of objects or std::vector or dlib::array objects.
types of objects or std::vector or dlib::array objects.
- matlab_struct and matlab_object. These are special types defined in the
call_matlab.h file and correspond to matlab structs and arbitrary matlab
objects respectively.
!*/
!*/
...
...
dlib/matlab/mex_wrapper.cpp
View file @
1cdbcb5a
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment