-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucontainer.cpp
1533 lines (1342 loc) · 38.1 KB
/
ucontainer.cpp
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
/*
* UniversalContainer library.
* Copyright Jason Denton, 2008,2010.
* Made available under the new BSD license, as described in LICENSE
*
* Send comments and bug reports to jason.denton@gmail.com
* http://www.greatpanic.com/code.html
*/
extern "C" {
#include <stdio.h>
}
#if _GLIBCXX_USE_C99_DYNAMIC
#undef _GLIBCXX_USE_C99_DYNAMIC // Clang on ndk8e has an snprintf collision with stdio.h & cstdio
#include <cstdio>
#define _GLIBCXX_USE_C99_DYNAMIC 1 // Put it back
#else
#include <cstdio> // Clang on ndk8e has an snprintf collision with stdio.h & cstdio
#endif // _GLIBCXX_USE_C99_DYNAMIC
#include <cstring>
#include <cctype>
#include <cerrno>
#include <cstdlib>
#include <limits>
#include "ucontainer.h"
#include "stl_util.h"
/*
Limitations :
All maps, vectors, and strings are references, and copies copy the reference.
The array behavior is strange. If an index has not been previously
used, it must be equal to the current size of the array. That is, it must
be one past the previously used indicies.
*/
#ifndef NDEBUG
#define internal_ucexception_uc(C,uc) UniversalContainer::construct_exception(C,__FUNCTION__,__FILE__,__LINE__,&uc,NULL)
#define internal_ucexception_s(C,s) UniversalContainer::construct_exception(C,__FUNCTION__,__FILE__,__LINE__,this,s)
#define internal_ucexception(C) construct_exception(C,__FUNCTION__,__FILE__,__LINE__,this,NULL)
#else
#define internal_ucexception_uc(C,uc) UniversalContainer::construct_exception(C,NULL,NULL,0,&uc,NULL)
#define internal_ucexception_s(C,s) UniversalContainer::construct_exception(C,NULL,NULL,0,this,s)
#define internal_ucexception(C) construct_exception(C,NULL,NULL,0,this,NULL)
#endif
using namespace std;
namespace JAD {
static const char* BOOL_VAL_STR = "#boolean_value";
static const char* TRUE_STR = "TRUE";
static const char* FALSE_STR = "FALSE";
static const char* true_str = "true";
static const char* false_str = "false";
std::map<UniversalContainerType, UniversalContainerTypeAdapter*> UniversalContainerAdapter;
static UniversalContainerType last_registered_type = uc_LastType;
UniversalContainerType UniversalContainer::register_new_type(UniversalContainerTypeAdapter* adapter)
{
last_registered_type++;
UniversalContainerAdapter[last_registered_type] = adapter;
return last_registered_type;
}
/*
Getting strtol right is apparently a royal bitch, despite the
availability of correct versions under the BSD license. This task
is needed twice, and is prone to need fixing when ported.
*/
void UniversalContainer::is_integer(string const& str, long& num, bool& is) const
{
const char* start = str.c_str();
char* end = (char*) start;
errno = 0;
num = strtol(start,&end,10);
if (errno || *end != '\0') is = false;
else is = true;
}
/*
The set routines are mostly called by the assignment operators
to do the actual work of setting a particular type and value into
the UniversalContainer (uc).
*/
void UniversalContainer::set_value_integer(long l)
{
type = uc_Integer;
data.num = l;
dirty = true;
}
void UniversalContainer::set_value_uinteger(unsigned long l)
{
type = uc_Integer;
data.num = l;
dirty = true;
}
void UniversalContainer::set_value_char(char c)
{
type = uc_Character;
data.chr = c;
dirty = true;
}
void UniversalContainer::set_value_double(double d)
{
type = uc_Real;
data.real = d;
dirty = true;
}
void UniversalContainer::set_value_bool(bool b)
{
type = uc_Boolean;
data.tf = b;
dirty = true;
}
void UniversalContainer::set_value_string(const string& s)
{
if (remove_refcount()) cleanup();
refcount = new unsigned;
*refcount = 1;
type = uc_String;
dirty = true;
data.str = new string(s);
}
void UniversalContainer::set_value_wstring(const wstring& s)
{
if (remove_refcount()) cleanup();
refcount = new unsigned;
*refcount = 1;
type = uc_WString;
dirty = true;
data.wstr = new wstring(s);
}
void UniversalContainer::set_value_cstr(const char* s)
{
if (s)
set_value_string(string(s));
else {
string tmp;
set_value_string(tmp);
}
}
/*
Create new container types.
*/
void UniversalContainer::init_map(void)
{
type = uc_Map;
data.map = new UniversalMap;
refcount = new unsigned;
(*refcount) = 1;
dirty = true;
}
void UniversalContainer::init_array(void)
{
type = uc_Array;
data.ray = new UniversalArray;
refcount = new unsigned;
(*refcount) = 1;
dirty = true;
}
/*
The various containers create containers of the appropriate type
for the various parameter types that can be passed.
*/
UniversalContainer::UniversalContainer(void)
{
type = uc_Null;
refcount = NULL;
data.str = NULL;
dirty = true;
}
UniversalContainer::UniversalContainer(int i)
{
refcount = NULL;
set_value_integer(i);
}
UniversalContainer::UniversalContainer(long l)
{
refcount = NULL;
set_value_integer(l);
}
UniversalContainer::UniversalContainer(char c)
{
refcount = NULL;
set_value_char(c);
}
UniversalContainer::UniversalContainer(bool b)
{
refcount = NULL;
set_value_bool(b);
}
UniversalContainer::UniversalContainer(double d)
{
refcount = NULL;
set_value_double(d);
}
UniversalContainer::UniversalContainer(const string s)
{
refcount = NULL;
set_value_string(s);
return;
}
UniversalContainer::UniversalContainer(const wstring s)
{
refcount = NULL;
set_value_wstring(s);
return;
}
UniversalContainer::UniversalContainer(char* s)
{
refcount = NULL;
set_value_cstr(s);
return;
}
UniversalContainer::UniversalContainer(UniversalContainerType typ, void* ptr)
{
if (!(UniversalContainerAdapter.count(type) < 1))
throw internal_ucexception(uce_Unregistered_Type);
refcount = new unsigned;
*refcount = 1;
data.reference = ptr;
type = typ;
return;
}
bool UniversalContainer::remove_refcount(void)
{
if (!refcount) return false;
(*refcount)--;
if (*refcount > 0) return false;
delete refcount;
refcount = NULL;
dirty = true;
return true;
}
void UniversalContainer::cleanup(void)
{
switch(type) {
case uc_Null :
case uc_Integer :
case uc_Character :
case uc_Real :
case uc_Boolean :
break;
case uc_String :
delete data.str;
break;
case uc_WString :
delete data.wstr;
break;
case uc_Map :
delete data.map;
break;
case uc_Array :
delete data.ray;
break;
default :
UniversalContainerAdapter[type]->on_delete(data.reference);
}
}
UniversalContainer::~UniversalContainer(void)
{
if (remove_refcount()) cleanup();
}
//designated code for copy constructor and operator=(UniversalContainer)
void UniversalContainer::duplicate(const UniversalContainer& uc)
{
//if we are using the same refcount, we had better be holding the same reference
//if (refcount && refcount == uc.refcount) return;
//if (refcount && remove_refcount()) cleanup();
if (refcount)
{
if (refcount == uc.refcount) return;
if (remove_refcount()) cleanup();
}
type = uc.type;
dirty = uc.dirty;
refcount = uc.refcount;
if (refcount) (*refcount)++;
switch(type) {
case uc_Map :
data.map = uc.data.map;
break;
case uc_Array :
data.ray = uc.data.ray;
break;
case uc_Integer :
data.num = uc.data.num;
break;
case uc_Real :
data.real = uc.data.real;
break;
case uc_Boolean :
data.tf = uc.data.tf;
break;
case uc_Character :
data.chr = uc.data.chr;
break;
case uc_String :
data.str = uc.data.str;
break;
case uc_WString :
data.wstr = uc.data.wstr;
break;
default :
data.reference = uc.data.reference;
}
}
UniversalContainer::UniversalContainer(const UniversalContainer& uc)
{
refcount = NULL;
duplicate(uc);
}
string saved_map_brackets; // Try to indicate what field had an exception.
//map brackets does the actual work of operator[string]
//it understand . notation to reach nested maps
UniversalContainer& UniversalContainer::map_brackets(const string& s)
{
saved_map_brackets = s; // Stash a copy for exceptions.
//unsigned long pos;
bool ismap;
long idx;
std::string piece1;
std::string piece2;
//split input into first argument and rest
auto pos = s.find('.');
piece1 = s.substr(0,pos);
if (pos != string::npos) piece2 = s.substr(pos+1);
//double check that this isn't really supposed to be a string
if (type == uc_Map) ismap = true;
else {
is_integer(piece1, idx, ismap);
ismap = !ismap;
}
if (ismap) {
if (type == uc_Null) init_map(); //if this is my first appearance, setup
if (type != uc_Map)
throw internal_ucexception(uce_Scalar_as_Collection);
if (piece2 == "") return (*(data.map))[piece1];
else return (*(data.map))[piece1][piece2];
}
else { //isarray
if (type == uc_Null) init_array();
if (type != uc_Array)
throw internal_ucexception(uce_Scalar_as_Collection);
if (piece2 == "") return (this->operator[](idx));
else return (this->operator[](idx))[piece2];
}
}
UniversalContainer& UniversalContainer::operator[](const string& s)
{
return map_brackets(s);
}
UniversalContainer& UniversalContainer::operator[](const wstring& w)
{
return map_brackets(convert_wstring_to_string(&w));
}
UniversalContainer& UniversalContainer::operator[](char* s)
{
string str(s);
return map_brackets(str);
}
UniversalContainer& UniversalContainer::operator[](const char* s)
{
string str(s);
return map_brackets(str);
}
UniversalContainer& UniversalContainer::operator[](int i)
{
if (type == uc_Null) init_array();
if (type != uc_Array)
throw internal_ucexception(uce_Non_Array_as_Array);
int sz = data.ray->size();
if (i == -1) i = sz;
if (i == sz) {
dirty = true;
UniversalContainer uc;
data.ray->push_back(uc);
}
if (i > sz || i < 0)
throw internal_ucexception(uce_Array_Subscript_Out_of_Bounds);
return data.ray->at(i);
}
/*
Assignment operators. They mostly do their job by calling the
appropriate set_* routines.
*/
UniversalContainer& UniversalContainer::operator=(int i)
{
if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_integer(i);
return *this;
}
UniversalContainer& UniversalContainer::operator=(int16_t i)
{
if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_integer(i);
return *this;
}
UniversalContainer& UniversalContainer::operator=(uint16_t i)
{
if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_integer(i);
return *this;
}
UniversalContainer& UniversalContainer::operator=(uint32_t i)
{
if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_integer(i);
return *this;
}
// UniversalContainer& UniversalContainer::operator=(uint64_t i)
// {
// if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
// throw internal_ucexception(uce_TypeMismatch_Write);
//
// set_value_integer(i);
// return *this;
//}
UniversalContainer& UniversalContainer::operator=(long l)
{
if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_integer(l);
return *this;
}
UniversalContainer& UniversalContainer::operator=(unsigned long l)
{
if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_uinteger(l);
return *this;
}
UniversalContainer& UniversalContainer::operator=(double d)
{
if (!(type == uc_Integer || type == uc_Real || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_double(d);
return *this;
}
UniversalContainer& UniversalContainer::operator=(bool b)
{
if (!(type == uc_Boolean || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_bool(b);
return *this;
}
UniversalContainer& UniversalContainer::operator=(const string& s)
{
if (!(type == uc_String || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_string(s);
return *this;
}
UniversalContainer& UniversalContainer::operator=(const wstring& s)
{
if (!(type == uc_WString || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_wstring(s);
return *this;
}
UniversalContainer& UniversalContainer::operator=(const char* s)
{
if (!(type == uc_String || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_cstr(s);
return *this;
}
UniversalContainer& UniversalContainer::operator=(char c)
{
if (!(type == uc_Character || type == uc_Null))
throw internal_ucexception(uce_TypeMismatch_Write);
set_value_char(c);
return *this;
}
UniversalContainer& UniversalContainer::operator=(const UniversalContainer& uc)
{
if (this != &uc) duplicate(uc);
return *this;
}
//utilities for casting strings to longs
long UniversalContainer::convert_string_to_long(const string* s)
{
errno = 0;
long l = strtol(s->c_str(),NULL,10);
if (errno) throw ucexception(uce_TypeMismatch_Read);
return l;
}
//utilities for casting strings to longs
unsigned long UniversalContainer::convert_string_to_ulong(const string* s)
{
errno = 0;
unsigned long l = strtoul(s->c_str(),NULL,10);
if (errno) throw ucexception(uce_TypeMismatch_Read);
return l;
}
long UniversalContainer::convert_wstring_to_long(const wstring* s)
{
string tmp = convert_wstring_to_string(s);
return convert_string_to_long(&tmp);
}
unsigned long UniversalContainer::convert_wstring_to_ulong(const wstring* s)
{
string tmp = convert_wstring_to_string(s);
return convert_string_to_ulong(&tmp);
}
/*
The various convert routines are mostly used by the casting operators,
to cast the various types to what the cast calls for.
*/
int UniversalContainer::convert_int(void) const
{
long retval;
switch (type) {
case uc_Integer :
retval = data.num;
break;
case uc_Boolean :
if (data.tf == true) return 1;
else return 0;
case uc_Real :
retval = static_cast<long>(data.real);
break;
case uc_Character :
return static_cast<long>(data.chr);
case uc_String :
retval = convert_string_to_long(data.str);
break;
case uc_WString :
retval = convert_wstring_to_long(data.wstr);
break;
case uc_Map :
case uc_Array :
throw internal_ucexception(uce_Collection_as_Scalar);
case uc_Null :
return 0;
default :
throw internal_ucexception(uce_Unknown);
}
if (retval > std::numeric_limits<int>::max() ||
retval < std::numeric_limits<int>::min()) {
std::string tmp = saved_map_brackets;
throw internal_ucexception_s(uce_TypeMismatch_Read, &tmp);
} else return static_cast<int>(data.num);
return retval;
}
unsigned int UniversalContainer::convert_uint(void) const
{
unsigned long retval;
switch (type) {
case uc_Integer :
retval = data.num;
break;
case uc_Boolean :
if (data.tf == true) return 1;
else return 0;
case uc_Real :
retval = static_cast<long>(data.real);
break;
case uc_Character :
return static_cast<long>(data.chr);
case uc_String :
retval = convert_string_to_ulong(data.str);
break;
case uc_WString :
retval = convert_wstring_to_ulong(data.wstr);
break;
case uc_Map :
case uc_Array :
throw internal_ucexception(uce_Collection_as_Scalar);
case uc_Null :
return 0;
default :
throw internal_ucexception(uce_Unknown);
}
if (retval > std::numeric_limits<unsigned int>::max()
/*|| retval < std::numeric_limits<unsigned int>::min()*/) {
std::string tmp = saved_map_brackets;
throw internal_ucexception_s(uce_TypeMismatch_Read, &tmp);
} else return static_cast<int>(data.num);
return retval;
}
long UniversalContainer::convert_long(void) const
{
switch (type) {
case uc_Integer :
return data.num;
case uc_Boolean :
if (data.tf) return 1;
else return 0;
case uc_Real :
return static_cast<int>(data.real);
case uc_Character :
return static_cast<char>(data.chr);
case uc_String :
return convert_string_to_long(data.str);
case uc_WString :
return convert_wstring_to_long(data.wstr);
case uc_Map :
case uc_Array :
throw internal_ucexception(uce_Collection_as_Scalar);
case uc_Null :
return 0;
default :
throw internal_ucexception(uce_Unknown);
}
return 0L;
}
std::string UniversalContainer::convert_string(void) const
{
char buf[32];
switch (type) {
case uc_String :
return *(data.str);
case uc_WString :
return convert_wstring_to_string(data.wstr);
case uc_Integer :
snprintf(buf,32,"%ld",data.num);
return string(buf);
break;
case uc_Real :
snprintf(buf,32,"%g",data.real);
return string(buf);
break;
case uc_Boolean :
if (data.tf) return string(true_str);
else return string(false_str);
case uc_Character :
snprintf(buf,32,"%c",data.chr);
return string(buf);
break;
case uc_Null :
return string("null");
break;
case uc_Map :
case uc_Array :
throw internal_ucexception(uce_Collection_as_Scalar);
default :
return UniversalContainerAdapter[type]->to_string(data.reference);
}
return string("error"); //dead code
}
std::wstring UniversalContainer::convert_wstring(void) const
{
wstring retval;
char buf[32];
string tmp;
switch (type) {
case uc_WString :
retval = *(data.wstr);
return retval;
case uc_String :
tmp = *(data.str);
break;
case uc_Integer :
snprintf(buf,32,"%ld",data.num);
tmp = buf;
break;
case uc_Real :
snprintf(buf,32,"%g",data.real);
tmp = buf;
break;
case uc_Boolean :
if (data.tf) tmp = true_str;
else tmp = false_str;
break;
case uc_Character :
snprintf(buf,32,"%c",data.chr);
tmp = buf;
break;
case uc_Null :
tmp = "null";
break;
case uc_Map :
case uc_Array :
throw internal_ucexception(uce_Collection_as_Scalar);
default :
throw internal_ucexception(uce_Unknown);
}
return convert_string_to_wstring(&tmp);
}
UniversalContainer::operator std::string*(void) const
{
if (type == uc_String) return data.str;
if (type == uc_Null) return NULL;
throw internal_ucexception(uce_TypeMismatch_Read);
}
const char* UniversalContainer::c_str(void) const
{
if (type == uc_String) return data.str->c_str();
if (type == uc_Null) return NULL;
throw internal_ucexception(uce_TypeMismatch_Read);
}
UniversalContainer::operator std::wstring*(void) const
{
if (type == uc_WString) return data.wstr;
if (type == uc_Null) return NULL;
throw internal_ucexception(uce_TypeMismatch_Read);
}
char UniversalContainer::convert_char(void) const
{
switch (type) {
case uc_Character :
return data.chr;
case uc_Boolean :
if (data.tf) return 't';
else return 'f';
case uc_String :
return data.str->c_str()[0];
case uc_WString :
case uc_Integer :
case uc_Real :
throw internal_ucexception(uce_TypeMismatch_Read);
case uc_Map :
case uc_Array :
throw internal_ucexception(uce_Collection_as_Scalar);
case uc_Null :
return '\0';
default :
throw internal_ucexception(uce_Unknown);
}
return '\0';
}
bool UniversalContainer::convert_bool(void) const
{
switch (type) {
case uc_Boolean :
return data.tf;
case uc_Character :
if (data.chr != 0) return true;
else return false;
case uc_Integer :
return (data.num != 0);
break;
case uc_Real :
return (data.real != 0.0);
break;
case uc_String :
if (!data.str->compare(TRUE_STR) || !data.str->compare(true_str)) return true;
else if(!data.str->compare(FALSE_STR) || !data.str->compare(false_str)) return false;
throw internal_ucexception(uce_TypeMismatch_Read);
case uc_WString :
throw internal_ucexception(uce_TypeMismatch_Read);
case uc_Map :
if (exists(BOOL_VAL_STR)) return (*(data.map))[BOOL_VAL_STR];
else return true;
case uc_Array :
return true;
case uc_Null :
return false;
break;
default :
throw internal_ucexception(uce_Unknown);
}
return false;
}
// Writing out the smallest IEEE double and reading it back in seems to cause ERANGE.
double UniversalContainer::convert_string_to_double(const string* s)
{
double retval;
char* ptr;
errno = 0;
retval = strtod(s->c_str(),&ptr);
if ((errno && errno != ERANGE) || (retval == 0.0 &&
ptr != (s->c_str() + s->length())))
throw ucexception(uce_TypeMismatch_Read);
return retval;
}
double UniversalContainer::convert_wstring_to_double(const wstring* s)
{
string tmp = convert_wstring_to_string(s);
return convert_string_to_double(&tmp);
}
double UniversalContainer::convert_double(void) const
{
switch (type) {
case uc_Real :
return data.real;
case uc_Integer :
return static_cast<double>(data.num);
case uc_Boolean :
if (data.tf) return 1.0;
else return 0.0;
case uc_Character :
return static_cast<double>(data.chr);
case uc_String :
return convert_string_to_double(data.str);
case uc_WString :
return convert_wstring_to_double(data.wstr);
case uc_Map :
case uc_Array :
throw internal_ucexception(uce_Collection_as_Scalar);
case uc_Null :
return 0.0;
default :
throw internal_ucexception(uce_Unknown);
}
return 0.0;
}
UniversalContainer::operator int(void) const
{
return convert_int();
}
UniversalContainer::operator int16_t(void) const
{
return convert_int();
}
UniversalContainer::operator uint16_t(void) const
{
return convert_uint();
}
UniversalContainer::operator uint32_t(void) const
{
return convert_uint();
}
UniversalContainer::operator double(void) const
{
return convert_double();
}
UniversalContainer::operator float(void) const
{
return convert_double();
}
UniversalContainer::operator char(void) const
{
return convert_char();
}
UniversalContainer::operator bool(void) const
{
return convert_bool();
}
UniversalContainer::operator long(void) const
{
return convert_long();
}
UniversalContainer::operator std::string(void) const
{
return convert_string();
}
UniversalContainer::operator std::wstring(void) const
{
return convert_wstring();
}
void* UniversalContainer::cast_or_throw(UniversalContainerType typ) const
{
if (type != typ) throw ucexception(uce_TypeMismatch_Read);
return data.reference;
}
UniversalContainerType UniversalContainer::get_type(void) const
{
return type;
};
//kind of a string constructor. Callable only on a null container.
//works through various options to figure out if the string
//is an integer, number, boolean, or character.
void UniversalContainer::string_interpret(const string s)
{
const char* str = s.c_str();
char* end;
char* up;
int len;
if (type != uc_Null)
throw internal_ucexception(uce_TypeMismatch_Write);
dirty = true;
refcount = NULL;
len = strlen(str);
const char* last = str + len;
//special empty string case
if (str[0] == '\0') {
set_value_string(s);
return;
}
errno = 0;