-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserialize.hpp
114 lines (89 loc) · 3.48 KB
/
serialize.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* <!-- mksqlite: A MATLAB Interface to SQLite -->
*
* @file serialize.hpp
* @brief MATLAB hidden (officially undocumented) feature of serializing data
* @details MATLAB arrays can be serialized into a byte stream for "streaming".
* With this feature any variable type can be stored as BLOB into a SQL database.
* @see http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data
* @authors Martin Kortmann <mail@kortmann.de>,
* Andreas Martin <andimartin@users.sourceforge.net>
* @version 2.14
* @date 2008-2024
* @copyright Distributed under BSD-2
* @pre
* @warning
* @bug
*/
#pragma once
//#include "config.h"
//#include "global.hpp"
#include "utils.hpp"
#include "value.hpp"
// (de-)serializing functions
// References:
// https://www.mathworks.com/matlabcentral/fileexchange/29457-serializedeserialize (Tim Hutt)
// https://www.mathworks.com/matlabcentral/fileexchange/34564-fast-serializedeserialize (Christian Kothe)
// http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data (Christian Kothe)
// getByteStreamFromArray(), getArrayFromByteStream() (undocumented Matlab functions)
bool have_serialize ();
bool can_serialize ();
bool serialize ( const mxArray* pItem, mxArray*& pByteStream );
bool deserialize ( const mxArray* pByteStream, mxArray*& pItem );
#ifdef MAIN_MODULE
/// Converts MATLAB variable of any complexity into byte stream
bool serialize( const mxArray* pItem, mxArray*& pByteStream )
{
assert( NULL == pByteStream && NULL != pItem );
#if CONFIG_EARLY_BIND_SERIALIZE
pByteStream = mxSerialize( pItem );
return NULL != pByteStream;
#endif
if( have_serialize() )
{
mexCallMATLAB( 1, &pByteStream, 1, const_cast<mxArray**>( &pItem ), "getByteStreamFromArray" ) ;
}
return NULL != pByteStream;
}
/// Converts byte stream back into originally MATLAB variable
bool deserialize( const mxArray* pByteStream, mxArray*& pItem )
{
assert( NULL != pByteStream && NULL == pItem );
#if CONFIG_EARLY_BIND_SERIALIZE
pItem = mxDeserialize( mxGetData( pByteStream ), mxGetNumberOfElements( pByteStream ) );
return NULL != pItem;
#endif
if( have_serialize() )
{
mexCallMATLAB( 1, &pItem, 1, const_cast<mxArray**>( &pByteStream ), "getArrayFromByteStream" );
return NULL != pItem;
}
return NULL != pItem;
}
/// Returns true, if current MATLAB version supports serialization
bool have_serialize()
{
#if CONFIG_EARLY_BIND_SERIALIZE
static int flagHaveSerialize = 1;
#else
static int flagHaveSerialize = -1;
#endif
if( flagHaveSerialize < 0 )
{
mxArray* pResult = NULL;
mxArray* pFuncName = mxCreateString( "getByteStreamFromArray" );
flagHaveSerialize = pFuncName
&& 0 == mexCallMATLAB( 1, &pResult, 1, &pFuncName, "exist" )
&& pResult
&& 5 == ValueMex( pResult ).GetInt(); // getByteStreamFromArray must be a build-in function
::utils_destroy_array( pFuncName );
::utils_destroy_array( pResult );
}
return flagHaveSerialize > 0;
}
/// Returns true, if streaming is switched on (user setting) and serialization is accessible.
bool can_serialize()
{
return g_streaming && have_serialize();
}
#endif