File: FXVariant.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (787 lines) | stat: -rw-r--r-- 21,306 bytes parent folder | download | duplicates (2)
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
/********************************************************************************
*                                                                               *
*                          V a r i a n t   T y p e                              *
*                                                                               *
*********************************************************************************
* Copyright (C) 2013,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "fxascii.h"
#include "fxunicode.h"
#include "FXElement.h"
#include "FXArray.h"
#include "FXString.h"
#include "FXException.h"
#include "FXVariant.h"
#include "FXVariantArray.h"
#include "FXVariantMap.h"

/*
  Notes:
  - Variant is a "discriminated union" type that may hold a integer, floating
    point number, string, or respectively an array or key/value collection of
    variants.  Thus, a single Variant can hold an arbitrarily complex collection
    of information.
  - As such, this makes for a convenient data structure to serialize and deserialize
    JSON files; JSON syntax in fact maps almost 1:1 to Variant capabilities.
  - Access to Variant's information is most typically performed using overloaded
    conversion operators [reading information from variants], and overloaded
    assignment operators [writing data to variants].
  - A few important caveats are worth mentioning for effiencent use of this flexible
    data structure:

      1 When writing to variant as an array, its best to reference the last element
        first, or if the number of elements is known in advance, to set the size
        explicitly prior to assigning elements.  Even though Variant automatically
        adapts the size of the array based on the index being accessed, such usage
        may lead to much unneccessary reallocations and copying; if things get big
        that may be performance bottleneck.

      2 Be aware that variant map may get resized as more key/value pairs are added;
        this means don't hang on to references to Variants that may be affected by
        the resize.

      3 Simple numbers (booleans, integers, floats, etc.) are VERY efficient to store;
        consequently, its fine to store a fairly large array into Variant, as long as
        one keeps point (1) above in mind.

      4 No artificial limits to sizes.  Variant should handle arbitrarily large data-
        structures, but efficient use needs to observe (1) above.

  - Object member operator or array indexing operator have two flavors; the non-const
    version will change the type of the variant automatically, and possibly return
    reference to newly created variant object.
    The const operators will return default Null variant if referencing non-existing
    members.
  - Probably should simplify storage of all integer types to either FXlong or FXulong;
    this will lead to fewer cases and cost no extra storage at all since the union is
    the size of the biggest type, anyway.
*/

// Largest and smallest long values
#ifndef LLONG_MAX
#define LLONG_MAX  FXLONG(9223372036854775807)
#endif
#ifndef LLONG_MIN
#define LLONG_MIN  (-LLONG_MAX-FXLONG(1))
#endif
#ifndef ULLONG_MAX
#define ULLONG_MAX FXULONG(18446744073709551615)
#endif

using namespace FX;

namespace FX {

/*******************************************************************************/

// Default variant
const FXVariant FXVariant::null;


// Initialize with default value for type t
FXbool FXVariant::init(Type t){
  switch(t){
  case BoolType:
  case CharType:
  case IntType:
  case UIntType:
  case LongType:
  case ULongType:
  case PointerType:
    value.u=0;
    type=t;
    return true;
  case FloatType:
  case DoubleType:
    value.d=0.0;
    type=t;
    return true;
  case StringType:
    construct(reinterpret_cast<FXString*>(&value.p));
    type=t;
    return true;
  case ArrayType:
    construct(reinterpret_cast<FXVariantArray*>(&value.p));
    type=t;
    return true;
  case MapType:
    construct(reinterpret_cast<FXVariantMap*>(&value.p));
    type=t;
    return true;
  default:
    return true;
    }
  return true;
  }


// Clear the data
FXbool FXVariant::clear(){
  switch(type){
  case BoolType:
  case CharType:
  case IntType:
  case UIntType:
  case LongType:
  case ULongType:
  case PointerType:
  case FloatType:
  case DoubleType:
    value.u=0L;
    type=NullType;
    return true;
  case StringType:
    destruct(reinterpret_cast<FXString*>(&value.p));
    value.u=0L;
    type=NullType;
    return true;
  case ArrayType:
    destruct(reinterpret_cast<FXVariantArray*>(&value.p));
    value.u=0L;
    type=NullType;
    return true;
  case MapType:
    destruct(reinterpret_cast<FXVariantMap*>(&value.p));
    value.u=0L;
    type=NullType;
    return true;
  default:
    return true;
    }
  return true;
  }


// Make a copy
FXVariant& FXVariant::assign(const FXVariant& other){
  if(this!=&other){
    clear();
    switch(other.type){
    case BoolType:
    case CharType:
    case IntType:
    case UIntType:
    case LongType:
    case ULongType:
    case FloatType:
    case DoubleType:
    case PointerType:
      value=other.value;
      type=other.type;
      return *this;
    case StringType:
      construct(reinterpret_cast<FXString*>(&value.p),*reinterpret_cast<const FXString*>(&other.value.p));
      type=other.type;
      return *this;
    case ArrayType:
      construct(reinterpret_cast<FXVariantArray*>(&value.p),*reinterpret_cast<const FXVariantArray*>(&other.value.p));
      type=other.type;
      return *this;
    case MapType:
      construct(reinterpret_cast<FXVariantMap*>(&value.p),*reinterpret_cast<const FXVariantMap*>(&other.value.p));
      type=other.type;
      return *this;
    default:
      return *this;
      }
    }
  return *this;
  }


// Adopt variant from another
FXVariant& FXVariant::adopt(FXVariant& other){
  if(this!=&other){
    swap(value,other.value);
    swap(type,other.type);
    other.clear();
    }
  return *this;
  }

/*******************************************************************************/

// Initialize null variant
FXVariant::FXVariant():type(NullType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.u=0;
  }


// Copy constructor
FXVariant::FXVariant(const FXVariant& other):type(NullType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  assign(other);
  }


// Construct and initialize with bool
FXVariant::FXVariant(FXbool val):type(BoolType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.u=val;
  }


// Construct and initialize with char
FXVariant::FXVariant(FXchar val):type(CharType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.u=val;
  }


// Construct and initialize with int
FXVariant::FXVariant(FXint val):type(IntType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.i=val;
  }


// Construct and initialize with unsigned int
FXVariant::FXVariant(FXuint val):type(UIntType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.u=val;
  }


// Construct and initialize with long
FXVariant::FXVariant(FXlong val):type(LongType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.i=val;
  }


// Construct and initialize with unsigned long
FXVariant::FXVariant(FXulong val):type(ULongType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.u=val;
  }


// Construct and initialize with float
FXVariant::FXVariant(FXfloat val):type(FloatType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.d=val;
  }


// Construct and initialize with double
FXVariant::FXVariant(FXdouble val):type(DoubleType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.d=val;
  }


// Construct and initialize with pointer
FXVariant::FXVariant(FXptr val):type(PointerType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  value.p=val;
  }


// Construct and initialize with string
FXVariant::FXVariant(const FXchar *val):type(StringType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  construct(reinterpret_cast<FXString*>(&value.p),val);
  }


// Construct and initialize with string
FXVariant::FXVariant(const FXString& val):type(StringType){
  FXASSERT(sizeof(value)>=sizeof(FXString) && sizeof(value)>=sizeof(FXVariantArray) &&  sizeof(value)>=sizeof(FXVariantMap));
  construct(reinterpret_cast<FXString*>(&value.p),val);
  }

/*******************************************************************************/

// Change type
void FXVariant::setType(Type t){
  clear();
  init(t);
  }


// Return size of array
FXival FXVariant::no() const {
  return (type==ArrayType) ? reinterpret_cast<const FXVariantArray*>(&value.p)->no() : 0;
  }


// Change number of elements in array
FXbool FXVariant::no(FXival n){
  if(type!=ArrayType){ clear(); init(ArrayType); }
  return reinterpret_cast<FXVariantArray*>(&value.p)->no(n);
  }


// Check if key is mapped
FXbool FXVariant::has(const FXchar* key) const {
  return (type==MapType) && (reinterpret_cast<const FXVariantMap*>(&value.p)->has(key));
  }


// Convert to bool
FXbool FXVariant::toBool() const {
  switch(type){
  case BoolType:
  case CharType:
  case IntType:
  case UIntType:
  case LongType:
  case ULongType:
  case PointerType:
    return !!value.u;                                   // True if non-0
  case FloatType:
  case DoubleType:
    return !!value.d;                                   // True if non-0
  case StringType:
    return !asString().empty();                         // True for non-empty string
  case ArrayType:
    return !!asArray().no();                            // True for non-empty array
  case MapType:
    return !asMap().empty();                            // True for non-empty map
  default:
    return false;                                       // Always false
    }
  return false;
  }


// Convert to pointer
FXptr FXVariant::toPtr() const {
  return (type==PointerType) ? value.p : nullptr;       // NULL anything not a pointer
  }


// Convert to int
FXint FXVariant::toInt(FXbool* ok) const {
  FXbool flag=false;
  FXlong result=0;
  switch(type){
  case BoolType:
  case CharType:
  case IntType:
  case UIntType:
  case LongType:
  case ULongType:
    result=value.i;
    if(__unlikely(result>INT_MAX)){ result=INT_MAX; break; }
    if(__unlikely(result<INT_MIN)){ result=INT_MIN; break; }
    flag=true;
    break;
  case FloatType:
  case DoubleType:
    if(__unlikely(value.d<-2147483648.0)){ result=INT_MIN; break; }
    if(__unlikely(value.d>=2147483648.0)){ result=INT_MAX; break; }
    result=Math::lrint(value.d);        // Nearest int
    flag=true;
    break;
  case StringType:
    return asString().toInt(10,ok);
  default:
    break;
    }
  if(__unlikely(ok)){ *ok=flag; }
  return (FXint)result;
  }


// Convert to unsigned int
FXuint FXVariant::toUInt(FXbool* ok) const {
  FXbool flag=false;
  FXlong result=0;
  switch(type){
  case BoolType:
  case CharType:
  case IntType:
  case UIntType:
  case LongType:
  case ULongType:
    result=value.i;
    if(__unlikely(result<=0)){ result=0; break; }
    if(__unlikely(result>=UINT_MAX)){ result=UINT_MAX; break; }
    flag=true;
    break;
  case FloatType:
  case DoubleType:
    if(__unlikely(value.d<0.0)){ result=0; break; }
    if(__unlikely(value.d>=4294967296.0)){ result=UINT_MAX; break; }
    result=Math::lrint(value.d);        // Nearest unsigned int
    flag=true;
    break;
  case StringType:
    return asString().toUInt(10,ok);
  default:
    break;
    }
  if(__unlikely(ok)){ *ok=flag; }
  return (FXuint)result;
  }


// Convert to long
FXlong FXVariant::toLong(FXbool* ok) const {
  FXbool flag=false;
  FXlong result=0;
  switch(type){
  case BoolType:
  case CharType:
  case IntType:
  case UIntType:
  case LongType:
  case ULongType:
    result=value.i;
    flag=true;
    break;
  case FloatType:
  case DoubleType:
    if(__unlikely(value.d<-9223372036854775808.0)){ result=LLONG_MIN; break; }
    if(__unlikely(value.d>=9223372036854775808.0)){ result=LLONG_MAX; break; }
    result=Math::lrint(value.d);        // Nearest long
    flag=true;
    break;
  case StringType:
    return asString().toLong(10,ok);
  default:
    break;
    }
  if(__unlikely(ok)){ *ok=flag; }
  return result;
  }


// Convert to unsigned long
FXulong FXVariant::toULong(FXbool* ok) const {
  FXbool flag=false;
  FXulong result=0;
  switch(type){
  case BoolType:
  case CharType:
  case IntType:
  case UIntType:
  case LongType:
  case ULongType:
    result=value.u;
    flag=true;
    break;
  case FloatType:
  case DoubleType:
    if(__unlikely(value.d<0.0)){ result=0; break; }
    if(__unlikely(value.d>=18446744073709551616.0)){ result=ULLONG_MAX; break; }
    result=(FXulong)(value.d+0.5);      // Nearest unsigned long
    flag=true;
    break;
  case StringType:
    return asString().toULong(10,ok);
  default:
    break;
    }
  if(__unlikely(ok)){ *ok=flag; }
  return result;
  }


// Convert to float
FXfloat FXVariant::toFloat(FXbool* ok) const {
  FXbool flag=false;
  FXfloat result=0.0f;
  switch(type){
  case BoolType:
  case IntType:
  case LongType:
    result=(FXfloat)value.i;
    flag=true;
    break;
  case CharType:
  case UIntType:
  case ULongType:
    result=(FXfloat)value.u;
    flag=true;
    break;
  case FloatType:
  case DoubleType:
    result=(FXfloat)value.d;
    flag=true;
    break;
  case StringType:
    return asString().toFloat(ok);
  default:
    break;
    }
  if(__unlikely(ok)){ *ok=flag; }
  return result;
  }


// Convert to double
FXdouble FXVariant::toDouble(FXbool* ok) const {
  FXbool flag=false;
  FXdouble result=0.0;
  switch(type){
  case BoolType:
  case IntType:
  case LongType:
    result=(FXdouble)value.i;
    flag=true;
    break;
  case CharType:
  case UIntType:
  case ULongType:
    result=(FXdouble)value.u;
    flag=true;
    break;
  case FloatType:
  case DoubleType:
    result=value.d;
    flag=true;
    break;
  case StringType:
    return asString().toDouble(ok);
  default:
    break;
    }
  if(__unlikely(ok)){ *ok=flag; }
  return result;
  }


// Convert to char pointer
const FXchar* FXVariant::toChars() const {
  return (type==StringType) ? value.s : FXString::null;
  }


// Convert to string
FXString FXVariant::toString(FXbool* ok) const {
  const FXchar truth[2][6]={"false","true"};
  switch(type){
  case BoolType:
    if(ok) *ok=true;
    return FXString(truth[value.u&1]);
  case CharType:
    if(ok) *ok=true;
    return FXString((FXchar)value.u,1);
  case IntType:
  case LongType:
    if(ok) *ok=true;
    return FXString::value(value.i);
  case UIntType:
  case ULongType:
    if(ok) *ok=true;
    return FXString::value(value.u);
  case FloatType:
  case DoubleType:
    if(ok) *ok=true;
    return FXString::value(value.d,16);
  case StringType:
    if(ok) *ok=true;
    return asString();
  default:
    if(ok) *ok=false;
    return FXString::null;
    }
  return FXString::null;
  }

/*******************************************************************************/

// Assign with bool
FXVariant& FXVariant::operator=(FXbool val){
  clear();
  value.u=val;
  type=BoolType;
  return *this;
  }


// Assign with char
FXVariant& FXVariant::operator=(FXchar val){
  clear();
  value.u=val;
  type=CharType;
  return *this;
  }


// Assign with int
FXVariant& FXVariant::operator=(FXint val){
  clear();
  value.i=val;
  type=IntType;
  return *this;
  }


// Assign with unsigned int
FXVariant& FXVariant::operator=(FXuint val){
  clear();
  value.u=val;
  type=UIntType;
  return *this;
  }


// Assign with long
FXVariant& FXVariant::operator=(FXlong val){
  clear();
  value.i=val;
  type=LongType;
  return *this;
  }


// Assign with unsigned long
FXVariant& FXVariant::operator=(FXulong val){
  clear();
  value.u=val;
  type=ULongType;
  return *this;
  }


// Assign with float
FXVariant& FXVariant::operator=(FXfloat val){
  clear();
  value.d=val;
  type=FloatType;
  return *this;
  }


// Assign with double
FXVariant& FXVariant::operator=(FXdouble val){
  clear();
  value.d=val;
  type=DoubleType;
  return *this;
  }


// Assign with pointer
FXVariant& FXVariant::operator=(FXptr val){
  clear();
  value.p=val;
  type=PointerType;
  return *this;
  }


// Assign with string
FXVariant& FXVariant::operator=(const FXchar* val){
  clear();
  construct(reinterpret_cast<FXString*>(&value.p),val);
  type=StringType;
  return *this;
  }


// Assign with string
FXVariant& FXVariant::operator=(const FXString& val){
  clear();
  construct(reinterpret_cast<FXString*>(&value.p),val);
  type=StringType;
  return *this;
  }


// Assign with variant
FXVariant& FXVariant::operator=(const FXVariant& val){
  return assign(val);
  }

/*******************************************************************************/

// Remove entry from the table
FXbool FXVariant::remove(const FXchar* key){
  if(type==MapType && key){
    return reinterpret_cast<FXVariantMap*>(&value.p)->remove(key);
    }
  return false;
  }


// Erase entry at pos in the table
FXbool FXVariant::erase(FXival idx){
  if(type==ArrayType && 0<=idx && reinterpret_cast<const FXVariantArray*>(&value.p)->no()){
    return reinterpret_cast<FXVariantArray*>(&value.p)->erase(idx);
    }
  return false;
  }

/*******************************************************************************/

// Return value of object member
FXVariant& FXVariant::at(const FXchar* key){
  if(type!=MapType){ clear(); init(MapType); }
  return reinterpret_cast<FXVariantMap*>(&value.p)->at(key);
  }


// Return value of object member
const FXVariant& FXVariant::at(const FXchar* key) const {
  if(type!=MapType){ return FXVariant::null; }
  return reinterpret_cast<const FXVariantMap*>(&value.p)->at(key);
  }


// Return value of object member
FXVariant& FXVariant::at(const FXString& key){
  if(type!=MapType){ clear(); init(MapType); }
  return reinterpret_cast<FXVariantMap*>(&value.p)->at(key);
  }


// Return value of object member
const FXVariant& FXVariant::at(const FXString& key) const {
  if(type!=MapType){ return FXVariant::null; }
  return reinterpret_cast<const FXVariantMap*>(&value.p)->at(key);
  }

/*******************************************************************************/

// Return value of array member
FXVariant& FXVariant::at(FXival idx){
  if(idx<0){ throw FXRangeException("FXVariant: index out of range\n"); }
  if(type!=ArrayType){ clear(); init(ArrayType); }
  if(idx>=reinterpret_cast<FXVariantArray*>(&value.p)->no()){
    if(!reinterpret_cast<FXVariantArray*>(&value.p)->append(FXVariant::null,idx-reinterpret_cast<FXVariantArray*>(&value.p)->no()+1)){
      throw FXMemoryException("FXVariant: out of memory\n");
      }
    }
  return reinterpret_cast<FXVariantArray*>(&value.p)->at(idx);
  }


// Return value of array member
const FXVariant& FXVariant::at(FXival idx) const {
  if(idx<0){ throw FXRangeException("FXVariant: index out of range\n"); }
  if(type==ArrayType && idx<reinterpret_cast<const FXVariantArray*>(&value.p)->no()){
    return reinterpret_cast<const FXVariantArray*>(&value.p)->at(idx);
    }
  return FXVariant::null;
  }

/*******************************************************************************/

// Destroy
FXVariant::~FXVariant(){
  clear();
  }

}