summaryrefslogtreecommitdiffstats
path: root/lib/kross/ruby/rubyvariant.h
blob: d0d2575992509120bf915f88a32a3f752aa2c74e (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
/***************************************************************************
 * rubyvariant.h
 * This file is part of the KDE project
 * copyright (C)2005 by Cyrille Berger (cberger@cberger.net)
 * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program 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
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#ifndef KROSS_RUBYVARIANT_H
#define KROSS_RUBYVARIANT_H

#define HAVE_STRLCAT_PROTO 1
#define HAVE_STRLCPY_PROTO 1
#include "config.h"

#include <ruby.h>

#ifndef HAVE_RUBY_1_9
#include <st.h>
#else // HAVE_RUBY_1_9
#include <ruby/st.h>
#endif // HAVE_RUBY_1_9
//#include <typeinfo>

#include "rubyconfig.h"

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvariant.h>
#include <tqsize.h>
#include <tqpoint.h>
#include <tqrect.h>
#include <tqurl.h>
#include <tqdatetime.h>

namespace Kross {

    /**
     * The RubyType helper classes used to cast between TQVariant
     * and VALUE values.
     *
     * Following TQVariant::Type's are implemented;
     *   \li TQVariant::Invalid
     *   \li TQVariant::Int
     *   \li TQVariant::UInt
     *   \li TQVariant::Double
     *   \li TQVariant::Bool
     *   \li TQVariant::LongLong
     *   \li TQVariant::ULongLong
     *   \li TQVariant::ByteArray
     *   \li TQVariant::String
     *   \li TQVariant::StringList
     *   \li TQVariant::Size
     *   \li TQVariant::SizeF
     *   \li TQVariant::Point
     *   \li TQVariant::PointF
     *   \li TQVariant::Rect
     *   \li TQVariant::RectF
     *   \li TQVariant::Url
     *   \li TQVariant::List
     *   \li TQVariant::Map
     *
     * Following TQVariant::Type's are unimplemented yet (do we need them anyways?);
     *   \li TQVariant::BitArray
     *   \li TQVariant::Date
     *   \li TQVariant::Time
     *   \li TQVariant::DateTime
     *   \li TQVariant::Bitmap
     *   \li TQVariant::Brush
     *   \li TQVariant::Char
     *   \li TQVariant::Color
     *   \li TQVariant::Cursor
     *   \li TQVariant::Font
     *   \li TQVariant::Icon
     *   \li TQVariant::Image
     *   \li TQVariant::KeySequence
     *   \li TQVariant::Line
     *   \li TQVariant::LineF
     *   \li TQVariant::Locale
     *   \li TQVariant::Palette
     *   \li TQVariant::Pen
     *   \li TQVariant::Pixmap
     *   \li TQVariant::PointArray
     *   \li TQVariant::Polygon
     *   \li TQVariant::RegExp
     *   \li TQVariant::Region
     *   \li TQVariant::SizePolicy
     *   \li TQVariant::TextFormat
     *   \li TQVariant::TextLength
     */
    template<typename VARIANTTYPE, typename RBTYPE = VALUE>
    struct RubyType
    {
        // template-specialisations need to implement following both static
        // functions to translate between TQVariant and Ruby's VALUE values.

        //inline static RBTYPE toVALUE(const VARIANTTYPE&) { return Py::None(); }
        //inline static TQVARIANTTYPE toVariant(const VARIANTTYPE&) { return TQVariant(); }
    };

    /// \internal
    template<>
    struct RubyType<TQVariant>
    {
        static VALUE toVALUE(const TQVariant& v);
        static TQVariant toVariant(VALUE value);
    };

    /// \internal
    template<>
    struct RubyType<int>
    {
        inline static VALUE toVALUE(int i) {
            return INT2FIX(i);
        }
        inline static int toVariant(VALUE value) {
            switch( TYPE(value) ) {
                case T_FIXNUM:
                    return FIX2INT(value);
                case T_BIGNUM:
                    return rb_big2int(value);
                case T_FLOAT:
#ifdef HAVE_RUBY_1_9
                    return (int)(RFLOAT_VALUE(value));
#else
                    return (int)(RFLOAT(value)->value);
#endif
                default:
                    break;
            }
            rb_raise(rb_eTypeError, "Integer must be a fixed number");
            return 0;
        }
    };

    /// \internal
    template<>
    struct RubyType<uint>
    {
        inline static VALUE toVALUE(uint i) {
            return UINT2NUM(i);
        }
        inline static uint toVariant(VALUE value) {
            switch( TYPE(value) ) {
                case T_FIXNUM:
                    return FIX2UINT(value);
                case T_BIGNUM:
                    return rb_big2uint(value);
                case T_FLOAT:
#ifdef HAVE_RUBY_1_9
                    return (uint)(RFLOAT_VALUE(value));
#else
                    return (uint)(RFLOAT(value)->value);
#endif
                default:
                    break;
            }
            rb_raise(rb_eTypeError, "Unsigned integer must be a fixed number");
            return 0;
        }
    };

    /// \internal
    template<>
    struct RubyType<double>
    {
        inline static VALUE toVALUE(double d) {
            return rb_float_new(d);
        }
        inline static double toVariant(VALUE value) {
            return NUM2DBL(value);
        }
    };

    /// \internal
    template<>
    struct RubyType<bool>
    {
        inline static VALUE toVALUE(bool b) {
            return b ? TRUE : FALSE;
        }
        inline static bool toVariant(VALUE value) {
            switch( TYPE(value) ) {
                case T_TRUE:
                    return true;
                case T_FALSE:
                    return false;
                default: {
                    rb_raise(rb_eTypeError, "Boolean value expected");
                    return false;
                } break;
            }
        }
    };

    /// \internal
    template<>
    struct RubyType<TQ_LLONG>
    {
        inline static VALUE toVALUE(TQ_LLONG l) {
            return /*INT2NUM*/ LONG2NUM((long)l);
        }
        inline static TQ_LLONG toVariant(VALUE value) {
            return NUM2LONG(value);
        }
    };

    /// \internal
    template<>
    struct RubyType<TQ_ULLONG>
    {
        inline static VALUE toVALUE(TQ_ULLONG l) {
            return UINT2NUM((unsigned long)l);
        }
        inline static TQ_ULLONG toVariant(VALUE value) {
            return NUM2UINT(value);
        }
    };

    /// \internal
    template<>
    struct RubyType<TQByteArray>
    {
        inline static VALUE toVALUE(const TQByteArray& ba) {
            return rb_str_new(ba.data(), ba.size());
        }
        inline static TQByteArray toVariant(VALUE value) {
            if( TYPE(value) != T_STRING ) {
                rb_raise(rb_eTypeError, "TQByteArray must be a string");
                //return STR2CSTR( rb_inspect(value) );
                return TQCString("");
            }
#ifdef HAVE_RUBY_1_9
            long length = LONG2NUM( RSTRING_LEN(value) );
#else
            long length = LONG2NUM( RSTRING(value)->len );
#endif
            if( length < 0 )
                return TQCString("");
#ifdef HAVE_RUBY_1_9
            return TQCString(RSTRING_PTR(value), RSTRING_LEN(value));
#else // HAVE_RUBY_1_9
            char* ca = rb_str2cstr(value, &length);
            return TQCString(ca, length);
#endif // HAVE_RUBY_1_9
        }
    };

    /// \internal
    template<>
    struct RubyType<TQString>
    {
        inline static VALUE toVALUE(const TQString& s) {
            return s.isNull() ? rb_str_new2("") : rb_str_new2(s.latin1());
        }
        inline static TQString toVariant(VALUE value) {
            if( TYPE(value) != T_STRING ) {
                rb_raise(rb_eTypeError, "TQString must be a string");
                return TQString();
            }
            return STR2CSTR(value);
        }
    };

    /// \internal
    template<>
    struct RubyType<TQSize>
    {
        inline static VALUE toVALUE(const TQSize& s) {
            VALUE l = rb_ary_new();
            rb_ary_push(l, RubyType<int>::toVALUE(s.width()));
            rb_ary_push(l, RubyType<int>::toVALUE(s.height()));
            return l;
        }
        inline static TQSize toVariant(VALUE value) {
#ifdef HAVE_RUBY_1_9
            if( TYPE(value) != T_ARRAY || RARRAY_LEN(value) != 2 ) {
#else
            if( TYPE(value) != T_ARRAY || RARRAY(value)->len != 2 ) {
#endif
                rb_raise(rb_eTypeError, "TQSize must be an array with 2 elements");
                return TQSize();
            }
            return TQSize( RubyType<int>::toVariant( rb_ary_entry(value,0) ), RubyType<int>::toVariant( rb_ary_entry(value,1) ) );
        }
    };

#if 0
    /// \internal
    template<>
    struct RubyType<TQSizeF>
    {
        inline static VALUE toVALUE(const TQSizeF& s) {
            VALUE l = rb_ary_new();
            rb_ary_push(l, RubyType<double>::toVALUE(s.width()));
            rb_ary_push(l, RubyType<double>::toVALUE(s.height()));
            return l;
        }
        inline static TQSizeF toVariant(VALUE value) {
#ifdef HAVE_RUBY_1_9
            if( TYPE(value) != T_ARRAY || RARRAY_LEN(value) != 2 ) {
#else
            if( TYPE(value) != T_ARRAY || RARRAY(value)->len != 2 ) {
#endif
                rb_raise(rb_eTypeError, "TQSizeF must be an array with 2 elements");
                return TQSizeF();
            }
            return TQSizeF( RubyType<double>::toVariant( rb_ary_entry(value,0) ), RubyType<double>::toVariant( rb_ary_entry(value,1) ) );

        }
    };
#endif

    /// \internal
    template<>
    struct RubyType<TQPoint>
    {
        inline static VALUE toVALUE(const TQPoint& s) {
            VALUE l = rb_ary_new();
            rb_ary_push(l, RubyType<int>::toVALUE(s.x()));
            rb_ary_push(l, RubyType<int>::toVALUE(s.y()));
            return l;
        }
        inline static TQPoint toVariant(VALUE value) {
#ifdef HAVE_RUBY_1_9
            if( TYPE(value) != T_ARRAY || RARRAY_LEN(value) != 2 ) {
#else
            if( TYPE(value) != T_ARRAY || RARRAY(value)->len != 2 ) {
#endif
                rb_raise(rb_eTypeError, "TQPoint must be an array with 2 elements");
                return TQPoint();
            }
            return TQPoint( RubyType<int>::toVariant( rb_ary_entry(value,0) ), RubyType<int>::toVariant( rb_ary_entry(value,1) ) );
        }
    };

#if 0
    /// \internal
    template<>
    struct RubyType<TQPointF>
    {
        inline static VALUE toVALUE(const TQPointF& s) {
            VALUE l = rb_ary_new();
            rb_ary_push(l, RubyType<double>::toVALUE(s.x()));
            rb_ary_push(l, RubyType<double>::toVALUE(s.y()));
            return l;
        }
        inline static TQPointF toVariant(VALUE value) {
#ifdef HAVE_RUBY_1_9
            if( TYPE(value) != T_ARRAY || RARRAY_LEN(value) != 2 ) {
#else
            if( TYPE(value) != T_ARRAY || RARRAY(value)->len != 2 ) {
#endif
                rb_raise(rb_eTypeError, "TQPointF must be an array with 2 elements");
                return TQPointF();
            }
            return TQPointF( RubyType<double>::toVariant( rb_ary_entry(value,0) ), RubyType<double>::toVariant( rb_ary_entry(value,1) ) );
        }
    };
#endif

    /// \internal
    template<>
    struct RubyType<TQRect>
    {
        inline static VALUE toVALUE(const TQRect& s) {
            VALUE l = rb_ary_new();
            rb_ary_push(l, RubyType<int>::toVALUE(s.x()));
            rb_ary_push(l, RubyType<int>::toVALUE(s.y()));
            rb_ary_push(l, RubyType<int>::toVALUE(s.width()));
            rb_ary_push(l, RubyType<int>::toVALUE(s.height()));
            return l;
        }
        inline static TQRect toVariant(VALUE value) {
#ifdef HAVE_RUBY_1_9
            if( TYPE(value) != T_ARRAY || RARRAY_LEN(value) != 4 ) {
#else
            if( TYPE(value) != T_ARRAY || RARRAY(value)->len != 4 ) {
#endif
                rb_raise(rb_eTypeError, "TQRect must be an array with 4 elements");
                return TQRect();
            }
            return TQRect( RubyType<int>::toVariant( rb_ary_entry(value,0) ), RubyType<int>::toVariant( rb_ary_entry(value,1) ),
                           RubyType<int>::toVariant( rb_ary_entry(value,2) ), RubyType<int>::toVariant( rb_ary_entry(value,3) ) );
        }
    };

#if 0
    /// \internal
    template<>
    struct RubyType<TQRectF>
    {
        inline static VALUE toVALUE(const TQRectF& s) {
            VALUE l = rb_ary_new();
            rb_ary_push(l, RubyType<double>::toVALUE(s.x()));
            rb_ary_push(l, RubyType<double>::toVALUE(s.y()));
            rb_ary_push(l, RubyType<double>::toVALUE(s.width()));
            rb_ary_push(l, RubyType<double>::toVALUE(s.height()));
            return l;
        }
        inline static TQRectF toVariant(VALUE value) {
#ifdef HAVE_RUBY_1_9
            if( TYPE(value) != T_ARRAY || RARRAY_LEN(value) != 4 ) {
#else
            if( TYPE(value) != T_ARRAY || RARRAY(value)->len != 4 ) {
#endif
                rb_raise(rb_eTypeError, "TQRectF must be an array with 4 elements");
                return TQRectF();
            }
            return TQRectF( RubyType<double>::toVariant( rb_ary_entry(value,0) ), RubyType<double>::toVariant( rb_ary_entry(value,1) ),
                           RubyType<double>::toVariant( rb_ary_entry(value,2) ), RubyType<double>::toVariant( rb_ary_entry(value,3) ) );
        }
    };
#endif

    /// \internal
    template<>
    struct RubyType<TQUrl>
    {
        inline static VALUE toVALUE(const TQUrl& url) {
            return RubyType<TQString>::toVALUE( url.toString() );
        }
        inline static TQUrl toVariant(VALUE value) {
            return TQUrl( RubyType<TQString>::toVariant(value) );
        }
    };

    /// \internal
    template<>
    struct RubyType<TQStringList>
    {
        inline static VALUE toVALUE(const TQStringList& list) {
            VALUE l = rb_ary_new();
            for (TQStringList::const_iterator it = list.begin(); it != list.end(); ++it) {
                rb_ary_push(l, RubyType<TQString>::toVALUE(*it));
            }
            return l;
        }
        inline static TQStringList toVariant(VALUE value) {
            if( TYPE(value) != T_ARRAY ) {
                rb_raise(rb_eTypeError, "TQStringList must be an array");
                return TQStringList();
            }
            TQStringList l;
#ifdef HAVE_RUBY_1_9
            for(int i = 0; i < RARRAY_LEN(value); i++)
#else
            for(int i = 0; i < RARRAY(value)->len; i++)
#endif
                l.append( RubyType<TQString>::toVariant( rb_ary_entry(value, i) ) );
            return l;
        }
    };

#if 0
    /// \internal
    template<>
    struct RubyType<TQVariantList>
    {
        inline static VALUE toVALUE(const TQVariantList& list) {
            VALUE l = rb_ary_new();
            foreach(TQVariant v, list)
                rb_ary_push(l, RubyType<TQVariant>::toVALUE(v));
            return l;
        }
        inline static TQVariantList toVariant(VALUE value) {
            if( TYPE(value) != T_ARRAY ) {
                rb_raise(rb_eTypeError, "TQVariantList must be an array");
                return TQVariantList();
            }
            TQVariantList l;
#ifdef HAVE_RUBY_1_9
            for(int i = 0; i < RARRAY_LEN(value); i++)
#else
            for(int i = 0; i < RARRAY(value)->len; i++)
#endif
                l.append( RubyType<TQVariant>::toVariant( rb_ary_entry(value, i) ) );
            return l;
        }
    };
#endif

#if 0
    /// \internal
    template<>
    struct RubyType<TQVariantMap>
    {
        inline static VALUE toVALUE(const TQVariantMap& map) {
            VALUE h = rb_hash_new();
            TQMap<TQString, TQVariant>::ConstIterator it(map.constBegin()), end(map.end());
            for(; it != end; ++it)
                rb_hash_aset(h, RubyType<TQString>::toVALUE(it.key()), RubyType<TQVariant>::toVALUE(it.value()) );
            return h;
        }
        inline static int convertHash(VALUE key, VALUE value, VALUE  vmap) {
            TQVariantMap* map; 
            Data_Get_Struct(vmap, TQVariantMap, map);
            if (key != TQundef)
                map->insert(STR2CSTR(key), RubyType<TQVariant>::toVariant(value));
            return ST_CONTINUE;
        }
        inline static TQVariantMap toVariant(VALUE value) {
            if( TYPE(value) != T_HASH ) {
                rb_raise(rb_eTypeError, "TQVariantMap must be a hash");
                return TQVariantMap();
            }
            TQVariantMap map;
            VALUE vmap = Data_Wrap_Struct(rb_cObject, 0,0, &map);
            rb_hash_foreach(value, (int (*)(...))convertHash, vmap);
            return map;
        }
    };
#endif

#if 0
    /**
     * The RubyMetaTypeFactory helper class us used as factory within
     * \a RubyExtension to translate an argument into a \a MetaType
     * needed for TQGenericArgument's data pointer.
     */
    class RubyMetaTypeFactory
    {
        public:
            static MetaType* create(int typeId, int metaTypeId, VALUE valueect = TQnil);
    };

    /// \internal
    template<typename VARIANTTYPE>
    class RubyMetaTypeVariant : public MetaTypeVariant<VARIANTTYPE>
    {
        public:
            RubyMetaTypeVariant(VALUE value)
                : MetaTypeVariant<VARIANTTYPE>(
                    (TYPE(value) == T_NIL)
                        ? TQVariant().value<VARIANTTYPE>()
                        : RubyType<VARIANTTYPE>::toVariant(value)
                ) {}

            virtual ~RubyMetaTypeVariant() {}
    };

#endif

}

#endif