mksqlite  2.5
A MATLAB interface to SQLite
serialize.hpp
Go to the documentation of this file.
1 
19 #pragma once
20 
21 //#include "config.h"
22 //#include "global.hpp"
23 #include "utils.hpp"
24 #include "value.hpp"
25 
26 
27 // (de-)serializing functions
28 // References:
29 // https://www.mathworks.com/matlabcentral/fileexchange/29457-serializedeserialize (Tim Hutt)
30 // https://www.mathworks.com/matlabcentral/fileexchange/34564-fast-serializedeserialize (Christian Kothe)
31 // http://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data (Christian Kothe)
32 // getByteStreamFromArray(), getArrayFromByteStream() (undocumented Matlab functions)
33 
34 bool have_serialize ();
35 bool can_serialize ();
36 bool serialize ( const mxArray* pItem, mxArray*& pByteStream );
37 bool deserialize ( const mxArray* pByteStream, mxArray*& pItem );
38 
39 
40 #ifdef MAIN_MODULE
41 
43 bool serialize( const mxArray* pItem, mxArray*& pByteStream )
44 {
45  assert( NULL == pByteStream && NULL != pItem );
46 
47 #if CONFIG_EARLY_BIND_SERIALIZE
48  pByteStream = mxSerialize( pItem );
49  return NULL != pByteStream;
50 #endif
51 
52  if( have_serialize() )
53  {
54  mexCallMATLAB( 1, &pByteStream, 1, const_cast<mxArray**>( &pItem ), "getByteStreamFromArray" ) ;
55  }
56 
57  return NULL != pByteStream;
58 }
59 
60 
62 bool deserialize( const mxArray* pByteStream, mxArray*& pItem )
63 {
64  assert( NULL != pByteStream && NULL == pItem );
65 
66 #if CONFIG_EARLY_BIND_SERIALIZE
67  pItem = mxDeserialize( mxGetData( pByteStream ), mxGetNumberOfElements( pByteStream ) );
68  return NULL != pItem;
69 #endif
70 
71  if( have_serialize() )
72  {
73  mexCallMATLAB( 1, &pItem, 1, const_cast<mxArray**>( &pByteStream ), "getArrayFromByteStream" );
74  return NULL != pItem;
75  }
76 
77  return NULL != pItem;
78 }
79 
80 
83 {
84 #if CONFIG_EARLY_BIND_SERIALIZE
85  static int flagHaveSerialize = 1;
86 #else
87  static int flagHaveSerialize = -1;
88 #endif
89 
90  if( flagHaveSerialize < 0 )
91  {
92  mxArray* pResult = NULL;
93  mxArray* pFuncName = mxCreateString( "getByteStreamFromArray" );
94 
95  flagHaveSerialize = pFuncName
96  && 0 == mexCallMATLAB( 1, &pResult, 1, &pFuncName, "exist" )
97  && pResult
98  && 5 == ValueMex( pResult ).GetInt(); // getByteStreamFromArray must be a build-in function
99 
100  ::utils_destroy_array( pFuncName );
101  ::utils_destroy_array( pResult );
102  }
103 
104  return flagHaveSerialize > 0;
105 }
106 
109 {
110  return g_streaming && have_serialize();
111 }
112 
113 
114 #endif
Encapsulating a MATLAB mxArray.
Definition: value.hpp:178
bool deserialize(const mxArray *pByteStream, mxArray *&pItem)
Converts byte stream back into originally MATLAB variable.
Definition: serialize.hpp:62
mxArray * mxSerialize(const mxArray *)
Serialize a MATLAB array.
mxArray * mxDeserialize(const void *, size_t)
Deserialize a MATLAB array.
int GetInt(int errval=0) const
Get integer value from item.
Definition: value.hpp:677
Utilities used in all files.
void utils_destroy_array(mxArray *&pmxarr)
Freeing memory allocated by mxCreateNumericMatrix() or mxCreateNumericArray().
Definition: utils.hpp:302
int g_streaming
Flag: Allow streaming.
Definition: global.hpp:269
bool can_serialize()
Returns true, if streaming is switched on (user setting) and serialization is accessible.
Definition: serialize.hpp:108
bool serialize(const mxArray *pItem, mxArray *&pByteStream)
Converts MATLAB variable of any complexity into byte stream.
Definition: serialize.hpp:43
bool have_serialize()
Returns true, if current MATLAB version supports serialization.
Definition: serialize.hpp:82
Value container for MATLAB/SQL data.