aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hybrid/base.hxx
blob: d4f63014a4249a743fe2a9675efcd29e8cd620cf (plain)
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
// file      : xsde/cxx/hybrid/base.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSDE_CXX_HYBRID_BASE_HXX
#define XSDE_CXX_HYBRID_BASE_HXX

#include <xsde/cxx/config.hxx>

#ifndef XSDE_STL
#  include <string.h> // strcmp
#  include <xsde/cxx/strdupx.hxx>
#endif

#ifdef XSDE_CUSTOM_ALLOCATOR
#  include <xsde/cxx/allocator.hxx>
#endif

namespace xsde
{
  namespace cxx
  {
    namespace hybrid
    {
      // boolean
      //
      struct boolean_base
      {
        bool base_value () const {return x_;}
        bool& base_value () {return x_;}
        void base_value (bool x) {x_ = x;}

        operator const bool& () const {return x_;}
        operator bool& () {return x_;}

        boolean_base& operator= (bool x) {x_ = x; return *this;}

      protected:
        bool x_;
      };

      inline bool
      operator== (const boolean_base& x, const boolean_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const boolean_base& x, const boolean_base& y)
      {
        return !(x == y);
      }

      // byte
      //
      struct byte_base
      {
        signed char base_value () const {return x_;}
        signed char& base_value () {return x_;}
        void base_value (signed char x) {x_ = x;}

        operator const signed char& () const {return x_;}
        operator signed char& () {return x_;}

        byte_base& operator= (signed char x) {x_ = x; return *this;}

      protected:
        signed char x_;
      };

      inline bool
      operator== (const byte_base& x, const byte_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const byte_base& x, const byte_base& y)
      {
        return !(x == y);
      }

      // unsigned_byte
      //
      struct unsigned_byte_base
      {
        unsigned char base_value () const {return x_;}
        unsigned char& base_value () {return x_;}
        void base_value (unsigned char x) {x_ = x;}

        operator const unsigned char& () const {return x_;}
        operator unsigned char& () {return x_;}

        unsigned_byte_base&
        operator= (unsigned char x) {x_ = x; return *this;}

      protected:
        unsigned char x_;
      };

      inline bool
      operator== (const unsigned_byte_base& x, const unsigned_byte_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const unsigned_byte_base& x, const unsigned_byte_base& y)
      {
        return !(x == y);
      }

      // short
      //
      struct short_base
      {
        short base_value () const {return x_;}
        short& base_value () {return x_;}
        void base_value (short x) {x_ = x;}

        operator const short& () const {return x_;}
        operator short& () {return x_;}

        short_base& operator= (short x) {x_ = x; return *this;}

      protected:
        short x_;
      };

      inline bool
      operator== (const short_base& x, const short_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const short_base& x, const short_base& y)
      {
        return !(x == y);
      }

      // unsigned_short
      //
      struct unsigned_short_base
      {
        unsigned short base_value () const {return x_;}
        unsigned short& base_value () {return x_;}
        void base_value (unsigned short x) {x_ = x;}

        operator const unsigned short& () const {return x_;}
        operator unsigned short& () {return x_;}

        unsigned_short_base&
        operator= (unsigned short x) {x_ = x; return *this;}

      protected:
        unsigned short x_;
      };

      inline bool
      operator== (const unsigned_short_base& x, const unsigned_short_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const unsigned_short_base& x, const unsigned_short_base& y)
      {
        return !(x == y);
      }

      // int
      //
      struct int_base
      {
        int base_value () const {return x_;}
        int& base_value () {return x_;}
        void base_value (int x) {x_ = x;}

        operator const int& () const {return x_;}
        operator int& () {return x_;}

        int_base& operator= (int x) {x_ = x; return *this;}

      protected:
        int x_;
      };

      inline bool
      operator== (const int_base& x, const int_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const int_base& x, const int_base& y)
      {
        return !(x == y);
      }

      // unsigned_int
      //
      struct unsigned_int_base
      {
        unsigned int base_value () const {return x_;}
        unsigned int& base_value () {return x_;}
        void base_value (unsigned int x) {x_ = x;}

        operator const unsigned int& () const {return x_;}
        operator unsigned int& () {return x_;}

        unsigned_int_base& operator= (unsigned int x) {x_ = x; return *this;}

      protected:
        unsigned int x_;
      };

      inline bool
      operator== (const unsigned_int_base& x, const unsigned_int_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const unsigned_int_base& x, const unsigned_int_base& y)
      {
        return !(x == y);
      }

      // long
      //
#ifdef XSDE_LONGLONG
      struct long_base
      {
        long long base_value () const {return x_;}
        long long& base_value () {return x_;}
        void base_value (long long x) {x_ = x;}

        operator const long long& () const {return x_;}
        operator long long& () {return x_;}

        long_base& operator= (long long x) {x_ = x; return *this;}

      protected:
        long long x_;
      };
#else
      struct long_base
      {
        long base_value () const {return x_;}
        long& base_value () {return x_;}
        void base_value (long x) {x_ = x;}

        operator const long& () const {return x_;}
        operator long& () {return x_;}

        long_base& operator= (long x) {x_ = x; return *this;}

      protected:
        long x_;
      };
#endif

      inline bool
      operator== (const long_base& x, const long_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const long_base& x, const long_base& y)
      {
        return !(x == y);
      }


      // unsigned_long
      //
#ifdef XSDE_LONGLONG
      struct unsigned_long_base
      {
        unsigned long long base_value () const {return x_;}
        unsigned long long& base_value () {return x_;}
        void base_value (unsigned long long x) {x_ = x;}

        operator const unsigned long long& () const {return x_;}
        operator unsigned long long& () {return x_;}

        unsigned_long_base&
        operator= (unsigned long long x) {x_ = x; return *this;}

      protected:
        unsigned long long x_;
      };
#else
      struct unsigned_long_base
      {
        unsigned long base_value () const {return x_;}
        unsigned long& base_value () {return x_;}
        void base_value (unsigned long x) {x_ = x;}

        operator const unsigned long& () const {return x_;}
        operator unsigned long& () {return x_;}

        unsigned_long_base&
        operator= (unsigned long x) {x_ = x; return *this;}

      protected:
        unsigned long x_;
      };
#endif

      inline bool
      operator== (const unsigned_long_base& x, const unsigned_long_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const unsigned_long_base& x, const unsigned_long_base& y)
      {
        return !(x == y);
      }

      // integer
      //
      struct integer_base
      {
        long base_value () const {return x_;}
        long& base_value () {return x_;}
        void base_value (long x) {x_ = x;}

        operator const long& () const {return x_;}
        operator long& () {return x_;}

        integer_base& operator= (long x) {x_ = x; return *this;}

      protected:
        long x_;
      };

      inline bool
      operator== (const integer_base& x, const integer_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const integer_base& x, const integer_base& y)
      {
        return !(x == y);
      }

      // negative_integer
      //
      struct negative_integer_base
      {
        long base_value () const {return x_;}
        long& base_value () {return x_;}
        void base_value (long x) {x_ = x;}

        operator const long& () const {return x_;}
        operator long& () {return x_;}

        negative_integer_base& operator= (long x) {x_ = x; return *this;}

      protected:
        long x_;
      };

      inline bool
      operator== (const negative_integer_base& x,
                  const negative_integer_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const negative_integer_base& x,
                  const negative_integer_base& y)
      {
        return !(x == y);
      }

      // non_positive_integer
      //
      struct non_positive_integer_base
      {
        long base_value () const {return x_;}
        long& base_value () {return x_;}
        void base_value (long x) {x_ = x;}

        operator const long& () const {return x_;}
        operator long& () {return x_;}

        non_positive_integer_base& operator= (long x) {x_ = x; return *this;}

      protected:
        long x_;
      };

      inline bool
      operator== (const non_positive_integer_base& x,
                  const non_positive_integer_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const non_positive_integer_base& x,
                  const non_positive_integer_base& y)
      {
        return !(x == y);
      }

      // positive_integer
      //
      struct positive_integer_base
      {
        unsigned long base_value () const {return x_;}
        unsigned long& base_value () {return x_;}
        void base_value (unsigned long x) {x_ = x;}

        operator const unsigned long& () const {return x_;}
        operator unsigned long& () {return x_;}

        positive_integer_base&
        operator= (unsigned long x) {x_ = x; return *this;}

      protected:
        unsigned long x_;
      };

      inline bool
      operator== (const positive_integer_base& x,
                  const positive_integer_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const positive_integer_base& x,
                  const positive_integer_base& y)
      {
        return !(x == y);
      }

      // non_negative_integer
      //
      struct non_negative_integer_base
      {
        unsigned long base_value () const {return x_;}
        unsigned long& base_value () {return x_;}
        void base_value (unsigned long x) {x_ = x;}

        operator const unsigned long& () const {return x_;}
        operator unsigned long& () {return x_;}

        non_negative_integer_base&
        operator= (unsigned long x) {x_ = x; return *this;}

      protected:
        unsigned long x_;
      };

      inline bool
      operator== (const non_negative_integer_base& x,
                  const non_negative_integer_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const non_negative_integer_base& x,
                  const non_negative_integer_base& y)
      {
        return !(x == y);
      }

      // float
      //
      struct float_base
      {
        float base_value () const {return x_;}
        float& base_value () {return x_;}
        void base_value (float x) {x_ = x;}

        operator const float& () const {return x_;}
        operator float& () {return x_;}

        float_base& operator= (float x) {x_ = x; return *this;}

      protected:
        float x_;
      };

      inline bool
      operator== (const float_base& x, const float_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const float_base& x, const float_base& y)
      {
        return !(x == y);
      }

      // double
      //
      struct double_base
      {
        double base_value () const {return x_;}
        double& base_value () {return x_;}
        void base_value (double x) {x_ = x;}

        operator const double& () const {return x_;}
        operator double& () {return x_;}

        double_base& operator= (double x) {x_ = x; return *this;}

      protected:
        double x_;
      };

      inline bool
      operator== (const double_base& x, const double_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const double_base& x, const double_base& y)
      {
        return !(x == y);
      }

      // decimal
      //
      struct decimal_base
      {
        double base_value () const {return x_;}
        double& base_value () {return x_;}
        void base_value (double x) {x_ = x;}

        operator const double& () const {return x_;}
        operator double& () {return x_;}

        decimal_base& operator= (double x) {x_ = x; return *this;}

      protected:
        double x_;
      };

      inline bool
      operator== (const decimal_base& x, const decimal_base& y)
      {
        return x.base_value () == y.base_value ();
      }

      inline bool
      operator!= (const decimal_base& x, const decimal_base& y)
      {
        return !(x == y);
      }

#ifndef XSDE_STL
      // string
      //
      struct string_base
      {
        string_base () : x_ (0) {}
        ~string_base ()
        {
#ifndef XSDE_CUSTOM_ALLOCATOR
          delete[] x_;
#else
          cxx::free (x_);
#endif
        }

        const char* base_value () const {return x_;}
        char* base_value () {return x_;}

        void base_value (char* x)
        {
#ifndef XSDE_CUSTOM_ALLOCATOR
          delete[] x_;
#else
          cxx::free (x_);
#endif
          x_ = x;
        }

        char* base_value_detach () {char* r = x_; x_ = 0; return r;}

        operator const char* () const {return x_;}
        operator char* () {return x_;}

        string_base& operator= (char* x) {base_value (x); return *this;}

#ifndef XSDE_EXCEPTIONS
        bool
#else
        void
#endif
        _copy (string_base& c) const
        {
          char* x = strdupx (x_);

#ifndef XSDE_EXCEPTIONS
          if (x == 0)
            return false;
#endif
          c.base_value (x);

#ifndef XSDE_EXCEPTIONS
          return true;
#endif
        }

      protected:
        char* x_;
      };

      inline bool
      operator== (const string_base& x, const string_base& y)
      {
        return strcmp (x.base_value (), y.base_value ()) == 0;
      }

      inline bool
      operator!= (const string_base& x, const string_base& y)
      {
        return !(x == y);
      }
#endif
    }
  }
}

#endif  // XSDE_CXX_HYBRID_BASE_HXX