-
Notifications
You must be signed in to change notification settings - Fork 7
/
value.hpp
1331 lines (1147 loc) · 35.2 KB
/
value.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* <!-- mksqlite: A MATLAB Interface to SQLite -->
*
* @file value.hpp
* @brief Value container for MATLAB/SQL data
* @details Classes for value interchange between MATLAB and SQL.
* - ValueBase as common root
* - ValueMex holding MATLAB arrays
* - ValueSQL holding SQL values (single table element)
* - ValueSQLCol holding a complete table column
* @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 "sqlite/sqlite3.h"
#include <string>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <memory>
using namespace std;
#ifdef _MSC_VER
#pragma warning( disable: 4521 4522 ) // multiple copy constructors and assignment operators specified
#endif
class ValueBase;
class ValueMex;
class ValueSQL;
class ValueSQLCol;
struct tagNativeArray;
#define SQLITE_BLOBX 20 ///< Identifier to flag another allocator as used for SQLITE_BLOB
/// Asserting size of SQLite 64-bit integer type at least size of long long type
HC_ASSERT( sizeof( sqlite3_int64 ) <= sizeof( long long ) );
/**
* \brief Base class for ValueMex and ValueSQL
*
* This class doesn't free objects' memory nor manage its lifetime!
* Member \p m_largest_field is only used to copy the content of the union.
*
*/
class ValueBase
{
public:
bool m_isConst; ///< if flagged as non-const, class may swap memory ownership (custody)
union
{
double m_float; ///< floating point representation
sqlite3_int64 m_integer; ///< integer representation
char* m_text; ///< text representation or allocated memory ()
mxArray* m_blob; ///< binary large object representation
mxArray* m_pcItem; ///< MATLAB variable representation
tagNativeArray* m_array; ///< self allocated variable representation
long long m_largest_field; ///< largest member used to copy entire union (dummy field)
};
/// Dtor
~ValueBase()
{
// Class ValueBase manages no memory, so when object runs out of scope memory must have been freed
// already or be const either.
assert( m_isConst || !m_largest_field );
}
// Only derived classes may create class instances
protected:
/// Standard ctor
ValueBase() NOEXCEPT
: m_isConst(true),
m_largest_field(0)
{
}
/// Copy ctor for constant objects
ValueBase( const ValueBase& other ) NOEXCEPT
{
m_isConst = true;
*this = other;
}
/// Move ctor for lvalues
ValueBase( ValueBase& other ) NOEXCEPT
{
m_isConst = true;
*this = other;
other.m_isConst = true; // taking ownership
}
/// Move ctor for rvalues (temporary objects)
ValueBase( ValueBase&& other ) NOEXCEPT
{
m_isConst = true;
*this = other;
other.m_isConst = true; // taking ownership
}
/// Assignment operator
ValueBase& operator=( const ValueBase& other ) NOEXCEPT
{
// checking self assignment
if( this != &other )
{
assert( m_isConst || !m_largest_field );
m_isConst = true;
m_largest_field = other.m_largest_field;
}
return *this;
}
/// Move assignment operator for lvalues
ValueBase& operator=( ValueBase& other ) NOEXCEPT
{
// checking self assignment
if( this != &other )
{
assert( m_isConst || !m_largest_field );
m_isConst = other.m_isConst;
m_largest_field = other.m_largest_field;
other.m_isConst = true; // taking ownership
}
return *this;
}
/// Move assignment operator for rvalues (temporary objects)
ValueBase& operator=( ValueBase&& other ) NOEXCEPT
{
// checking self assignment
if( this != &other )
{
assert( m_isConst || !m_largest_field );
m_isConst = other.m_isConst;
m_largest_field = other.m_largest_field;
other.m_isConst = true; // taking ownership
}
return *this;
}
};
/**
* \brief Encapsulating a MATLAB mxArray.
*
* Class ValueMex never takes custody of a MATLAB memory object! \n
* Even though there is a function Destroy() which frees the MATLAB object,
* this class has no destructor which automatically does.
*
* It's intended that this class allocates and tends memory space
* through other functions than mxCreate*() functions, since these are quite
* slow. (see \ref tagNativeArray)
*
* If a dtor is declared, we have to fulfil the "rule of three"
* \sa <a href="http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29">Wikipedia</a>
*/
class ValueMex : public ValueBase
{
public:
/**
* Complexity information about a MATLAB variable.
* For testing if a variable is able to be packed or not.
*/
typedef enum {
TC_EMPTY = 0, ///< Empty
TC_SIMPLE, ///< single non-complex value, char or simple string (SQLite simple types)
TC_SIMPLE_VECTOR, ///< non-complex numeric vectors (SQLite BLOB)
TC_SIMPLE_ARRAY, ///< multidimensional non-complex numeric or char arrays (SQLite typed BLOB)
TC_COMPLEX, ///< structs, cells, complex data (SQLite typed ByteStream BLOB)
TC_UNSUPP = -1 ///< all other (unsuppored types)
} type_complexity_e;
enum {
LOGICAL_CLASS = mxLOGICAL_CLASS,
INT8_CLASS = mxINT8_CLASS,
UINT8_CLASS = mxUINT8_CLASS,
INT16_CLASS = mxINT16_CLASS,
INT32_CLASS = mxINT32_CLASS,
UINT16_CLASS = mxUINT16_CLASS,
UINT32_CLASS = mxUINT32_CLASS,
INT64_CLASS = mxINT64_CLASS,
DOUBLE_CLASS = mxDOUBLE_CLASS,
SINGLE_CLASS = mxSINGLE_CLASS,
CHAR_CLASS = mxCHAR_CLASS,
};
/// Standard ctor
ValueMex() NOEXCEPT : ValueBase()
{
}
/// Copy ctor for const objects
ValueMex( const ValueMex& other ) NOEXCEPT : ValueBase( other )
{
}
/// Move ctor for lvalues
ValueMex( ValueMex& other ) NOEXCEPT : ValueBase( other )
{
}
/// Move ctor for rvalues (temporary objects)
ValueMex( ValueMex&& other ) NOEXCEPT : ValueBase( other )
{
}
/// Assignment operator for const objects
ValueMex& operator=( const ValueMex& other ) NOEXCEPT
{
ValueBase::operator=( other );
return *this;
}
/// Move assignment operator for lvalues
ValueMex& operator=( ValueMex& other ) NOEXCEPT
{
ValueBase::operator=( other );
return *this;
}
/// Move assignment operator for rvalues (temporary objects)
ValueMex& operator=( ValueMex&& other ) NOEXCEPT
{
ValueBase::operator=( other );
return *this;
}
/**
* \brief Copy ctor for mxArrays
*
* \param[in] pcItem MATLAB array
*/
explicit
ValueMex( const mxArray* pcItem )
{
m_isConst = true;
m_pcItem = const_cast<mxArray*>(pcItem);
}
/**
* \brief Ctor allocating new MATLAB matrix object
*
* \param[in] m Number of rows
* \param[in] n Number of columns
* \param[in] clsid Class ID of value type (refer MATLAB manual)
*/
ValueMex( mwIndex m, mwIndex n, int clsid = mxDOUBLE_CLASS )
{
m_pcItem = mxCreateNumericMatrix( m, n, (mxClassID)clsid, mxREAL );
m_isConst = false;
}
/**
* @brief Take ownership (custody) of a MEX array
*
* @param doAdopt true to adopt, false to make it const
*/
ValueMex& Adopt( bool doAdopt = true )
{
m_isConst = !doAdopt;
return *this;
}
/**
* @brief Create a cell array
*
* @param m Number of rows
* @param n Number of cols
*
* @return Cell array
*/
static
ValueMex CreateCellMatrix( int m, int n )
{
return ValueMex( mxCreateCellMatrix( m, n ) ).Adopt();
}
/**
* @brief Create a double scalar
*
* @param value Scalar value
* @return Scalar value matrix
*/
static
ValueMex CreateDoubleScalar( double value )
{
return ValueMex( mxCreateDoubleScalar( value ) ).Adopt();
}
/**
* @brief Create a string
*
* @param str String value
* @return String as char array
*/
static
ValueMex CreateString( const char* str )
{
return ValueMex( mxCreateString( str ) ).Adopt();
}
/**
* \brief Dtor
*
* If this class has its custody, memory is freed.
*/
void Destroy()
{
if( !m_isConst && m_pcItem )
{
// Workaround to avoid MATLAB crash with persistent arrays ("Case 02098404", Lucas Lebert, MathWorks Technical Support Department
mxArray* tmp = m_pcItem;
m_pcItem = NULL;
mxDestroyArray( m_pcItem );
}
}
/**
* \brief Returns hosted MATLAB array
*/
inline
const mxArray* Item() const
{
return const_cast<const mxArray*>( m_pcItem );
}
/**
* \brief Returns hosted MATLAB array
*/
inline
mxArray* Item()
{
return m_pcItem;
}
/**
* \brief Returns a duplictae of the hosted MATLAB array
*/
inline
ValueMex Duplicate() const
{
return ValueMex( m_pcItem ? mxDuplicateArray( m_pcItem ) : NULL ).Adopt();
}
/**
* \brief Detach hosted MATLAB array
*/
inline
mxArray* Detach()
{
assert( !m_isConst );
mxArray* pcItem = m_pcItem;
m_largest_field = 0;
return pcItem;
}
/**
* \brief Returns row count (1st dimension)
*/
inline
size_t GetM() const
{
return mxGetM(m_pcItem);
}
/**
* \brief Returns col count (2nd dimension)
*/
inline
size_t GetN() const
{
return mxGetN(m_pcItem);
}
/**
* \brief Returns true if item is NULL or empty ([])
*/
inline
bool IsEmpty() const
{
return !m_pcItem || mxIsEmpty( m_pcItem );
}
/**
* \brief Returns true if item is a cell array
*/
inline
bool IsCell() const
{
return m_pcItem ? mxIsCell( m_pcItem ) : false;
}
/**
* \brief Returns true if item is not NULL and complex
*/
inline
bool IsComplex() const
{
return m_pcItem ? mxIsComplex( m_pcItem ) : false;
}
/**
* \brief Returns true if item consists of exact 1 element
*/
inline
bool IsScalar() const
{
return NumElements() == 1;
}
/**
* \brief Returns true if item is a struct array
*/
inline
bool IsStruct() const
{
return mxIsStruct( m_pcItem );
}
/**
* \brief Returns true if m_pcItem is of size 1xN or Mx1
*/
inline
bool IsVector() const
{
return NumDims() == 2 && min( GetM(), GetN() ) == 1;
}
/**
* \brief Returns true if m_pcItem is of type mxDOUBLE_CLASS
*/
inline
bool IsDoubleClass() const
{
return mxDOUBLE_CLASS == ClassID();
}
/**
* \brief Returns true if m_pcItem is of type mxFUNCTION_CLASS
*/
inline
bool IsFunctionHandle() const
{
return mxFUNCTION_CLASS == ClassID();
}
/**
* \brief Returns number of elements
*/
inline
size_t NumElements() const
{
return m_pcItem ? mxGetNumberOfElements( m_pcItem ) : 0;
}
/**
* \brief Returns size in bytes of one element
*/
inline
size_t ByElement() const
{
return m_pcItem ? mxGetElementSize( m_pcItem ) : 0;
}
/**
* \brief Returns number of dimensions
*/
inline
int NumDims() const
{
return m_pcItem ? (int)mxGetNumberOfDimensions( m_pcItem ) : 0;
}
/**
* \brief Returns data size in bytes
*/
inline
size_t ByData() const
{
return NumElements() * ByElement();
}
/**
* \brief Returns item class ID or mxUNKNOWN_CLASS if item is NULL
*/
inline
mxClassID ClassID() const
{
return m_pcItem ? mxGetClassID( m_pcItem ) : mxUNKNOWN_CLASS;
}
/**
* \brief Get complexity information. Which storage level is necessary (scalar, vector, matrix, text, blob)
*
* \param[in] bCanSerialize true if serialization of item is enabled
* \returns The item complexity (for storage issues)
*/
type_complexity_e Complexity( bool bCanSerialize = false ) const
{
if( IsEmpty() ) return TC_EMPTY;
switch( ClassID() )
{
case mxDOUBLE_CLASS:
case mxSINGLE_CLASS:
if( mxIsComplex( m_pcItem ) )
{
return TC_COMPLEX;
}
/* fallthrough */
case mxLOGICAL_CLASS:
case mxINT8_CLASS:
case mxUINT8_CLASS:
case mxINT16_CLASS:
case mxUINT16_CLASS:
case mxINT32_CLASS:
case mxUINT32_CLASS:
case mxINT64_CLASS:
case mxUINT64_CLASS:
if( IsScalar() ) return TC_SIMPLE;
return IsVector() ? TC_SIMPLE_VECTOR : TC_SIMPLE_ARRAY;
case mxCHAR_CLASS:
return ( IsScalar() || IsVector() ) ? TC_SIMPLE : TC_SIMPLE_ARRAY;
case mxUNKNOWN_CLASS:
// serialized data is marked as "unknown" type by mksqlite
return bCanSerialize ? TC_COMPLEX : TC_UNSUPP;
case mxSTRUCT_CLASS:
case mxCELL_CLASS:
return TC_COMPLEX;
default:
return TC_UNSUPP;
}
}
/**
* \brief Returns pointer to raw data
*/
inline
void* Data() const
{
return !IsEmpty() ? mxGetData( m_pcItem ) : NULL;
}
/**
* \brief Convert a string to char, due flagUTF converted to utf8
*
* \param[in] flagUTF if true, string will be converted to UTF8
* \param[out] format optional format string (see fprintf())
* \returns created string (allocator \ref MEM_ALLOC)
*/
char *GetString( bool flagUTF = false, const char* format = NULL ) const
{
size_t count;
char* result = NULL;
mxArray* new_string = NULL;
const mxArray* org_string = m_pcItem;
// reformat original string with MATLAB function "sprintf" into new string
if( format )
{
mxArray* args[2] = { mxCreateString( format ), const_cast<mxArray*>(org_string) };
mexCallMATLAB( 1, &new_string, 2, args, "sprintf" );
mxDestroyArray( args[0] ); // destroy format string
org_string = new_string; // Override (but don't free!) org_string
}
// get character stream from original string (MATLAB array)
if( org_string )
{
char* temp = mxArrayToString( org_string ); // Handles multibyte strings, too
// Copy string with own memory management
if( temp )
{
count = strlen( temp ) + 1;
result = (char*) MEM_ALLOC( count, sizeof(char) );
if( result )
{
memcpy( result, temp, count );
}
mxFree( temp );
}
}
// reformatted string is no longer needed
::utils_destroy_array( new_string );
// try to retrieve the character stream
if( !result )
{
// free memory and return with error
mexErrMsgTxt( getLocaleMsg( MSG_CANTCOPYSTRING ) );
}
// convert to UFT
if( flagUTF )
{
char *buffer = NULL;
int buflen;
/* get only the buffer size needed */
buflen = utils_latin2utf( (unsigned char*)result, (unsigned char*)buffer );
buffer = (char*) MEM_ALLOC( buflen, sizeof(char) );
if( !buffer )
{
::utils_free_ptr( result ); // Needless due to mexErrMsgTxt(), but clean
mexErrMsgTxt( getLocaleMsg( MSG_CANTCOPYSTRING ) );
}
/* encode string to utf now */
::utils_latin2utf( (unsigned char*)result, (unsigned char*)buffer );
::utils_free_ptr( result );
result = buffer;
}
return result;
}
/**
* \brief Returns allocated memory with items test, due to global flag converted to UTF
*
* \returns created string (allocator \ref MEM_ALLOC)
*/
char* GetEncString() const
{
return GetString( g_convertUTF8 ? true : false );
}
/**
* \brief Get integer value from item
*
* \param errval Value to return in case of non-convertible variable data type
* \returns Item value converted to integer type.
*/
int GetInt( int errval = 0 ) const
{
if( !IsEmpty() )
{
switch( ClassID() )
{
case mxINT8_CLASS : return (int) *( (int8_t*) Data() );
case mxUINT8_CLASS : return (int) *( (uint8_t*) Data() );
case mxINT16_CLASS : return (int) *( (int16_t*) Data() );
case mxUINT16_CLASS: return (int) *( (uint16_t*) Data() );
case mxINT32_CLASS : return (int) *( (int32_t*) Data() );
case mxUINT32_CLASS: return (int) *( (uint32_t*) Data() );
case mxSINGLE_CLASS: return (int) *( (float*) Data() );
case mxDOUBLE_CLASS: return (int) *( (double*) Data() );
case mxLOGICAL_CLASS: return (int) mxIsLogicalScalarTrue( m_pcItem );
default:
assert( false );
return errval;
}
}
return errval;
}
/**
* \brief Get 64 bit integer value from item
*
* \param errval Value to return in case of non-convertible variable data type
* \returns Item value converted to integer type.
*/
sqlite3_int64 GetInt64( int errval = 0 ) const
{
if( !IsEmpty() )
{
switch( ClassID() )
{
case mxINT64_CLASS : return *( (sqlite3_int64*) Data() );
default:
assert( false );
return (sqlite3_int64) errval;
}
}
return errval;
}
/// \returns Scalar item value (double), or NaN if it's not a scalar
double GetScalar() const
{
return IsScalar() ? mxGetScalar( m_pcItem ) : DBL_NAN;
}
/**
* \brief Get field from a struct array
*
* \param n Index of the array
* \param name Name of the requested field
* \returns Handle to mxArray
*/
const mxArray* GetField( int n, const char* name ) const
{
mxArray* result = NULL;
if( m_pcItem )
{
result = mxGetField( m_pcItem, n, name );
if( !result )
{
// MATLAB bug? For implicit non-initialized structure members mxGetField()
// returns NULL.
// ( Thx Knut Voigtlaender for pointing that out )
// Workaround: Check if struct has requested field
if( mxGetFieldNumber( m_pcItem, name ) >= 0 )
{
// Create and return an empty array in this case
// (MATLAB destroys it automatically when exitting MEX function)
result = mxCreateNumericMatrix( 0, 1, mxDOUBLE_CLASS, mxREAL );
}
}
}
return result;
}
/**
* @brief Sets a cell of a MATLAB cell array
*
* @param i Index
* @param cell Cell content
*/
void SetCell( int i, const mxArray* cell )
{
if( m_pcItem )
{
mxSetCell( m_pcItem, i, const_cast<mxArray*>(cell) );
}
}
/**
* @brief Make the MATLAB array persistent
*/
void MakePersistent()
{
if( m_pcItem )
{
mexMakeArrayPersistent( m_pcItem );
m_isConst = false;
}
}
/**
* @brief Throws an exception if any occured
*/
void Throw()
{
if( !IsEmpty() && mxIsClass( m_pcItem, "MException" ) )
{
mxArray* exception = Detach();
mexCallMATLAB( 0, NULL, 1, &exception, "throw" );
}
}
/**
* @brief Calling a MATLAB function (handle) with arguemnts
*
* @param lhs Return value (only one)
* @param exception Exception container
*/
void Call( ValueMex* lhs, ValueMex* exception )
{
assert( IsCell() && !IsEmpty() );
mxArray* _lhs = NULL; // Function return value
mxArray** prhs = (mxArray**)Data(); // Function handle and arguments
mxArray* _exception = mexCallMATLABWithTrap( 1, &_lhs, (int)NumElements(), prhs, "feval" );
// Function returned results?
if( _lhs )
{
*lhs = ValueMex( _lhs ).Adopt();
_lhs = NULL;
}
// An exception occured?
if( _exception )
{
*exception = ValueMex( _exception ).Adopt();
_exception = NULL;
}
// Cleanup
if( _lhs )
{
mxDestroyArray( _lhs );
}
if( _exception )
{
mxDestroyArray( _exception );
}
}
};
/**
* \brief Class encapsulating a SQL field value
*
* SQLite supports following types:
* - SQLITE_INTEGER
* - SQLITE_FLOAT
* - SQLITE_TEXT
* - SQLITE_BLOB
* - SQLITE_NULL
* - SQLITE3_TEXT (not supported)
*
* ValueSQL holds one field value of listed types.
*/
class ValueSQL : public ValueBase
{
public:
int m_typeID; ///< Type of SQL value as integer ID
size_t m_blobsize; ///< Size of BLOB in bytes (only type SQLITE_BLOBX)
/// Dtor
~ValueSQL()
{
Destroy();
}
/// Standard ctor
ValueSQL() NOEXCEPT
{
m_blobsize = 0;
m_typeID = SQLITE_NULL;
}
/// Copy ctor for constant objects
ValueSQL( const ValueSQL& other ) NOEXCEPT : ValueBase( other )
{
m_typeID = other.m_typeID;
m_blobsize = other.m_blobsize;
}
/// Move ctor for lvalues
ValueSQL( ValueSQL& other ) NOEXCEPT : ValueBase( other )
{
m_typeID = other.m_typeID;
m_blobsize = other.m_blobsize;
}
/// Move ctor for rvalues (temporary objects)
ValueSQL( ValueSQL&& other ) NOEXCEPT : ValueBase( other )
{
m_typeID = other.m_typeID;
m_blobsize = other.m_blobsize;
}
/// Assignment operator for constant objects
ValueSQL& operator=( const ValueSQL& other ) NOEXCEPT
{
ValueBase::operator=(other);
m_typeID = other.m_typeID;
m_blobsize = other.m_blobsize;
return *this;
}
/// Move assignment operator for lvalues
ValueSQL& operator=( ValueSQL& other ) NOEXCEPT
{
ValueBase::operator=(other);
m_typeID = other.m_typeID;
m_blobsize = other.m_blobsize;
return *this;
}
/// Move assignment operator for rvalues (temporary objects)
ValueSQL& operator=( ValueSQL&& other ) NOEXCEPT
{
ValueBase::operator=(other);
m_typeID = other.m_typeID;
m_blobsize = other.m_blobsize;
return *this;
}
/// Ctor for double type initializer
explicit
ValueSQL( double dValue )
{
m_float = dValue;
m_typeID = SQLITE_FLOAT;
}
/// Ctor for llong type initializer
explicit
ValueSQL( sqlite3_int64 iValue )
{
m_integer = iValue;
m_typeID = SQLITE_INTEGER;
}
/// Ctor for const char* type initializer
explicit
ValueSQL( const char* txtValue )
{
m_text = const_cast<char*>(txtValue);
m_typeID = SQLITE_TEXT;
}
/// Ctor for char* type initializer
explicit
ValueSQL( char* txtValue )
{
m_text = txtValue;
m_typeID = SQLITE_TEXT;
}
/// Ctor for char* type initializer
explicit
ValueSQL( char* blobValue, size_t size )
{
m_isConst = false;
m_text = blobValue;
m_blobsize = size;
m_typeID = SQLITE_BLOBX;
}
/// Ctor for MATLAB const mxArray* type initializer
explicit
ValueSQL( const mxArray* blobValue )
{
m_blob = const_cast<mxArray*>(blobValue);
m_typeID = SQLITE_BLOB;
}
/// Ctor for MATLAB mxArray* type initializer
explicit
ValueSQL( mxArray* blobValue )
{
m_isConst = false;
m_blob = blobValue;
m_typeID = SQLITE_BLOB;
}
/// Release custody and return pointer type
void* Detach()
{
assert( m_text && ( m_typeID == SQLITE_TEXT || m_typeID == SQLITE_BLOB || m_typeID == SQLITE_BLOBX ) );
m_isConst = true;
return (void*)m_text;
}
/**
* \brief Freeing memory space if having ownership
*
* Text and BLOB reserve dynamic memory space to store its contents