filters

aielement.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, Dirk Schönberger <dirk.schoenberger@sz-online.de>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "aielement.h"
21#include <tqglobal.h>
22
23AIElement::Private::Private()
24{
25 typ = AIElement::Invalid;
26}
27
28AIElement::Private::Private( Private* d )
29{
30 switch( d->typ )
31 {
32 case AIElement::Invalid:
33 break;
34 case AIElement::String:
35 case AIElement::Reference:
36 case AIElement::Operator:
37 value.ptr = new TQString( *((TQString*)d->value.ptr) );
38 break;
39 case AIElement::CString:
40 // TQCString is explicit shared
41 value.ptr = new TQCString( *((TQCString*)d->value.ptr) );
42 break;
43/* case AIElement::List:
44 value.ptr = new TQValueList<AIElement>( *((TQValueList<AIElement>*)d->value.ptr) );
45 break; */
46 case AIElement::ElementArray:
47 value.ptr = new TQValueVector<AIElement>( *((TQValueVector<AIElement>*)d->value.ptr) );
48 break;
49 case AIElement::Block:
50 value.ptr = new TQValueVector<AIElement>( *((TQValueVector<AIElement>*)d->value.ptr) );
51 break;
52 case AIElement::ByteArray:
53 value.ptr = new TQByteArray( *((TQByteArray*)d->value.ptr) );
54 break;
55 case AIElement::Int:
56 value.i = d->value.i;
57 break;
58 case AIElement::UInt:
59 value.u = d->value.u;
60 break;
61 case AIElement::Double:
62 value.d = d->value.d;
63 break;
64 case AIElement::Byte:
65 value.b = d->value.b;
66 break;
67 default:
68 Q_ASSERT( 0 );
69 }
70
71 typ = d->typ;
72}
73
74AIElement::Private::~Private()
75{
76 clear();
77}
78
79void AIElement::Private::clear()
80{
81 switch( typ )
82 {
83 case AIElement::String:
84 case AIElement::Operator:
85 case AIElement::Reference:
86 delete (TQString*)value.ptr;
87 break;
88 case AIElement::CString:
89 delete (TQCString*)value.ptr;
90 break;
91/* case AIElement::List:
92 delete (TQValueList<AIElement>*)value.ptr;
93 break; */
94 case AIElement::ElementArray:
95 delete (TQValueVector<AIElement>*)value.ptr;
96 break;
97 case AIElement::Block:
98 delete (TQValueVector<AIElement>*)value.ptr;
99 break;
100 case AIElement::ByteArray:
101 delete (TQByteArray*)value.ptr;
102 break;
103 case AIElement::Invalid:
104 case AIElement::Int:
105 case AIElement::UInt:
106 case AIElement::Double:
107 case AIElement::Byte:
108 break;
109 }
110
111 typ = AIElement::Invalid;
112}
113
118{
119 d = new Private;
120}
121
131{
132 if ( d->deref() )
133 delete d;
134}
135
142{
143 d = new Private;
144 *this = p;
145}
146
150AIElement::AIElement( const TQString& val, Type type )
151{
152 d = new Private;
153 d->typ = type;
154 d->value.ptr = new TQString( val );
155}
156
164AIElement::AIElement( const TQCString& val )
165{
166 d = new Private;
167 d->typ = CString;
168 d->value.ptr = new TQCString( val );
169}
170
177AIElement::AIElement( const char* val )
178{
179 d = new Private;
180 if ( val == 0 )
181 return;
182 d->typ = CString;
183 d->value.ptr = new TQCString( val );
184}
185
190{
191 d = new Private;
192 d->typ = Int;
193 d->value.i = val;
194}
195
200{
201 d = new Private;
202 d->typ = UInt;
203 d->value.u = val;
204}
205
210{
211 d = new Private;
212 d->typ = Byte;
213 d->value.b = val;
214}
215
216
221{
222 d = new Private;
223 d->typ = Double;
224 d->value.d = val;
225}
226
230/* AIElement::AIElement( const TQValueList<AIElement>& val )
231{
232 d = new Private;
233 d->typ = List;
234 d->value.ptr = new TQValueList<AIElement>( val );
235} */
236
237AIElement::AIElement( const TQValueVector<AIElement>& val, Type type )
238{
239 d = new Private;
240 d->typ = type;
241 d->value.ptr = new TQValueVector<AIElement>( val );
242}
243
244AIElement::AIElement( const TQByteArray& val )
245{
246 d = new Private;
247 d->typ = ByteArray;
248 d->value.ptr = new TQByteArray( val );
249}
250
259{
260 AIElement& other = (AIElement&)aielement;
261
262 other.d->ref();
263 if ( d->deref() )
264 delete d;
265
266 d = other.d;
267
268 return *this;
269}
270
274void AIElement::detach()
275{
276 if ( d->count == 1 )
277 return;
278
279 d->deref();
280 d = new Private( d );
281}
282
289const char* AIElement::typeName() const
290{
291 return typeToName( d->typ );
292}
293
298{
299 if ( d->count > 1 )
300 {
301 d->deref();
302 d = new Private;
303 return;
304 }
305
306 d->clear();
307}
308
309static const int ntypes = 11;
310static const char* const type_map[ntypes] =
311{
312 0,
313// "TQValueList<AIElement>",
314 "TQString",
315 "int",
316 "uint",
317 "double",
318 "TQCString",
319 "Operator",
320 "Reference",
321 "TQValueVector<AIElement>",
322 "TQByteArray",
323 "uchar",
324};
325
330const char* AIElement::typeToName( Type typ )
331{
332 if ( typ >= ntypes )
333 return 0;
334 return type_map[typ];
335}
336
344AIElement::Type AIElement::nameToType( const char* name )
345{
346 for ( int i = 0; i < ntypes; i++ ) {
347 if ( !qstrcmp( type_map[i], name ) )
348 return (Type) i;
349 }
350 return Invalid;
351}
352
360const TQString AIElement::toString() const
361{
362 if ( d->typ == CString )
363 return TQString::fromLatin1( toCString() );
364 if ( d->typ == Int )
365 return TQString::number( toInt() );
366 if ( d->typ == UInt )
367 return TQString::number( toUInt() );
368 if ( d->typ == Double )
369 return TQString::number( toDouble() );
370 if ( d->typ == Byte )
371 return TQString::number( toByte() );
372 if ( d->typ != String )
373 return TQString();
374 return *((TQString*)d->value.ptr);
375}
376
377const TQString AIElement::toReference() const
378{
379 if ( d->typ != Reference )
380 return TQString();
381 return *((TQString*)d->value.ptr);
382}
383
384const TQString AIElement::toOperator() const
385{
386 if ( d->typ != Operator )
387 return TQString();
388 return *((TQString*)d->value.ptr);
389}
390
397const TQCString AIElement::toCString() const
398{
399 if ( d->typ == CString )
400 return *((TQCString*)d->value.ptr);
401 if ( d->typ == String )
402 return ((TQString*)d->value.ptr)->latin1();
403 if ( d->typ == Operator )
404 return ((TQString*)d->value.ptr)->latin1();
405 if ( d->typ == Reference )
406 return ((TQString*)d->value.ptr)->latin1();
407
408 return 0;
409}
410
411
421int AIElement::toInt( bool * ok ) const
422{
423 if( d->typ == String )
424 return ((TQString*)d->value.ptr)->toInt( ok );
425 if ( d->typ == CString )
426 return ((TQCString*)d->value.ptr)->toInt( ok );
427 if ( ok )
428 *ok = canCast( UInt );
429 if( d->typ == Int )
430 return d->value.i;
431 if( d->typ == UInt )
432 return (int)d->value.u;
433 if( d->typ == Byte )
434 return (int)d->value.b;
435 if ( d->typ == Double )
436 return (int)d->value.d;
437 return 0;
438}
439
440uchar AIElement::toByte( bool * ok ) const
441{
442 if( d->typ == String )
443 return ((TQString*)d->value.ptr)->toShort( ok );
444 if ( d->typ == CString )
445 return ((TQCString*)d->value.ptr)->toShort( ok );
446 if ( ok )
447 *ok = canCast( UInt );
448 if( d->typ == Byte )
449 return d->value.b;
450 if( d->typ == Int )
451 return (uchar)d->value.i;
452 if( d->typ == UInt )
453 return (uchar)d->value.u;
454 if ( d->typ == Double )
455 return (uchar)d->value.d;
456 return 0;
457}
458
459
469uint AIElement::toUInt( bool * ok ) const
470{
471 if( d->typ == String )
472 return ((TQString*)d->value.ptr)->toUInt( ok );
473 if ( d->typ == CString )
474 return ((TQCString*)d->value.ptr)->toUInt( ok );
475 if ( ok )
476 *ok = canCast( UInt );
477 if( d->typ == Int )
478 return d->value.i;
479 if( d->typ == UInt )
480 return (int)d->value.u;
481 if( d->typ == Byte )
482 return (int)d->value.b;
483 if ( d->typ == Double )
484 return (int)d->value.d;
485
486 return 0;
487}
488
498double AIElement::toDouble( bool * ok ) const
499{
500 if( d->typ == String )
501 return ((TQString*)d->value.ptr)->toDouble( ok );
502 if ( d->typ == CString )
503 return ((TQCString*)d->value.ptr)->toDouble( ok );
504 if ( ok )
505 *ok = canCast( Double );
506 if ( d->typ == Double )
507 return d->value.d;
508 if ( d->typ == Int )
509 return (double)d->value.i;
510 if ( d->typ == UInt )
511 return (double)d->value.u;
512 if ( d->typ == Byte )
513 return (double)d->value.b;
514 return 0.0;
515}
516
534/* const TQValueList<AIElement> AIElement::toList() const
535{
536 if ( d->typ == List )
537 return *((TQValueList<AIElement>*)d->value.ptr);
538 return TQValueList<AIElement>();
539} */
540
541const TQValueVector<AIElement> AIElement::toElementArray() const
542{
543 if ( d->typ == ElementArray )
544 return *((TQValueVector<AIElement>*)d->value.ptr);
545 return TQValueVector<AIElement>();
546}
547
548const TQValueVector<AIElement> AIElement::toBlock() const
549{
550 if ( d->typ == Block )
551 return *((TQValueVector<AIElement>*)d->value.ptr);
552 return TQValueVector<AIElement>();
553}
554
555
556const TQByteArray AIElement::toByteArray() const
557{
558 if ( d->typ == ByteArray )
559 return *((TQByteArray*)d->value.ptr);
560 return TQByteArray();
561}
562
563#define TQ_VARIANT_AS( f ) TQ##f& AIElement::as##f() { \
564 if ( d->typ != f ) *this = AIElement( to##f() ); else detach(); return *((TQ##f*)d->value.ptr);}
565
566TQ_VARIANT_AS(String)
567TQ_VARIANT_AS(CString)
568
569
573{
574 detach();
575 if ( d->typ != Int ) {
576 int i = toInt();
577 d->clear();
578 d->value.i = i;
579 d->typ = Int;
580 }
581 return d->value.i;
582}
583
588{
589 detach();
590 if ( d->typ != UInt ) {
591 uint u = toUInt();
592 d->clear();
593 d->value.u = u;
594 d->typ = UInt;
595 }
596 return d->value.u;
597}
598
603{
604 if ( d->typ != Double ) {
605 double dbl = toDouble();
606 d->clear();
607 d->value.d = dbl;
608 d->typ = Double;
609 }
610 return d->value.d;
611}
612
617{
618 detach();
619 if ( d->typ != Byte ) {
620 uchar b = toByte();
621 d->clear();
622 d->value.b = b;
623 d->typ = Byte;
624 }
625 return d->value.b;
626}
627
628
643/* TQValueList<AIElement>& AIElement::asList()
644{
645 if ( d->typ != List )
646 *this = AIElement( toList() );
647 return *((TQValueList<AIElement>*)d->value.ptr);
648} */
649
650TQValueVector<AIElement>& AIElement::asElementArray()
651{
652 if ( d->typ != ElementArray )
653 *this = AIElement( toElementArray() );
654 return *((TQValueVector<AIElement>*)d->value.ptr);
655}
656
657TQValueVector<AIElement>& AIElement::asBlock()
658{
659 if ( d->typ != Block )
660 *this = AIElement( toBlock() );
661 return *((TQValueVector<AIElement>*)d->value.ptr);
662}
663
664
665TQByteArray& AIElement::asByteArray()
666{
667 if ( d->typ != ByteArray )
668 *this = AIElement( toByteArray() );
669 return *((TQByteArray*)d->value.ptr);
670}
671
686bool AIElement::canCast( Type t ) const
687{
688 if ( d->typ == t )
689 return true;
690 if ( t == Int && ( d->typ == String || d->typ == Double || d->typ == UInt || d->typ == Byte) )
691 return true;
692 if ( t == UInt && ( d->typ == String || d->typ == Double || d->typ == Int || d->typ == Byte) )
693 return true;
694 if ( t == Double && ( d->typ == String || d->typ == Int || d->typ == UInt || d->typ == Byte) )
695 return true;
696 if ( t == CString && d->typ == String )
697 return true;
698 if ( t == String && ( d->typ == CString || d->typ == Int || d->typ == UInt || d->typ == Double || d->typ == Byte) )
699 return true;
700
701 return false;
702}
703
718
719bool AIElement::cast( Type t )
720{
721 switch ( t ) {
722/* case AIElement::List:
723 asList();
724 break; */
725 case AIElement::ElementArray:
727 break;
728 case AIElement::Block:
729 asBlock();
730 break;
731 case AIElement::String:
732 asString();
733 break;
734 case AIElement::Int:
735 asInt();
736 break;
737 case AIElement::UInt:
738 asUInt();
739 break;
740 case AIElement::Double:
741 asDouble();
742 break;
743 case AIElement::CString:
744 asCString();
745 break;
746 case AIElement::Byte:
747 asByte();
748 break;
749 case AIElement::ByteArray:
750 asByteArray();
751 break;
752 default:
753 case AIElement::Invalid:
754 (*this) = AIElement();
755 }
756 return canCast( t );
757}
758
762
763bool AIElement::operator==( const AIElement &v ) const
764{
765 if ( !v.canCast( type() ) )
766 return false;
767 switch( d->typ ) {
768/* case List:
769 return v.toList() == toList(); */
770 case ElementArray:
771 return v.toElementArray() == toElementArray();
772 case Block:
773 return v.toBlock() == toBlock();
774 case ByteArray:
775 return v.toByteArray() == toByteArray();
776
777 case String:
778 return v.toString() == toString();
779 case Operator:
780 return v.toOperator() == toOperator();
781 case Reference:
782 return v.toReference() == toReference();
783 case CString:
784 return v.toCString() == toCString();
785 case Int:
786 return v.toInt() == toInt();
787 case UInt:
788 return v.toUInt() == toUInt();
789 case Byte:
790 return v.toByte() == toByte();
791 case Invalid:
792 break;
793 }
794 return false;
795}
796
800
801bool AIElement::operator!=( const AIElement &v ) const
802{
803 return !( v == *this );
804}
bool canCast(Type) const
bool operator==(const AIElement &) const
AIElement & operator=(const AIElement &)
const TQValueVector< AIElement > toElementArray() const
double toDouble(bool *ok=0) const
uint toUInt(bool *ok=0) const
const TQString toString() const
int & asInt()
uint & asUInt()
int toInt(bool *ok=0) const
uchar & asByte()
static const char * typeToName(Type typ)
double & asDouble()
static Type nameToType(const char *name)
const TQCString toCString() const
bool cast(Type)
Casts the aielement to the requested type.
TQValueVector< AIElement > & asElementArray()
void clear()
const char * typeName() const
bool operator!=(const AIElement &) const