summaryrefslogtreecommitdiffstats
path: root/lib/kross/api/variant.h
blob: 65b4c4e8f9e21ca6ac06a053061691845bc5f48b (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
/***************************************************************************
 * variant.h
 * This file is part of the KDE project
 * copyright (C)2004-2005 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_API_VARIANT_H
#define KROSS_API_VARIANT_H

#include <tqstring.h>
#include <tqvariant.h>

#include "object.h"
#include "value.h"
#include "exception.h"

namespace Kross { namespace Api {

    class List;

    /**
     * Variant value to wrap a TQVariant into a \a Kross::Api::Value
     * to enable primitive types like strings or numerics.
     */
    class Variant : public Value<Variant, TQVariant>
    {
            friend class Value<Variant, TQVariant>;
        public:

            /**
             * Constructor.
             *
             * \param value The initial TQVariant-value
             *        this Variant-Object has.
             * \param name The name this Value has.
             */
            Variant(const TQVariant& value);

            inline operator bool () { return getValue().toBool(); }
            inline operator int () { return getValue().toInt(); }
            inline operator uint () { return getValue().toUInt(); }
            inline operator double () { return getValue().toDouble(); }
            inline operator const char* () { return getValue().toString().latin1(); }

            inline operator TQString () { return getValue().toString(); }
            inline operator const TQString () { return getValue().toString(); }
            inline operator const TQString& () { return getValue().asString(); }

            inline operator TQCString () { return getValue().toCString(); }
            inline operator const TQCString () { return getValue().toCString(); }
            inline operator const TQCString& () { return getValue().asCString(); }

            inline operator TQVariant () { return getValue(); }
	    inline operator const TQVariant () { return getValue(); }
            inline operator const TQVariant& () { return getValue(); }

            /**
             * Operator to return a TQStringList.
             *
             * We can not just use getValue().toStringList() here cause maybe
             * this Kross::Api::Variant is a Kross::Api::List which could be
             * internaly used for list of strings as well. So, we use the
             * toStringList() function which will take care of translating a
             * Kross::Api::List to a TQStringList if possible or to throw an
             * exception if the Kross::Api::List isn't a TQStringList.
             */
            inline operator TQStringList () {
                return toStringList(this);
            }
            inline operator TQValueList<TQVariant> () {
                return toList(this);
            }

            /**
             * Destructor.
             */
            virtual ~Variant();

            /// \see Kross::Api::Object::getClassName()
            virtual const TQString getClassName() const;

            /**
             * \return a string representation of the variant.
             *
             * \see Kross::Api::Object::toString()
             */
            virtual const TQString toString();

            /**
             * Try to convert the given \a Object into
             * a TQVariant.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a TQVariant converted object.
             */
            static const TQVariant& toVariant(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a TQString.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a TQString converted object.
             */
            static const TQString toString(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a int.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a int converted object.
             */
            static int toInt(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a uint.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a uint converted object.
             */
            static uint toUInt(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a uint.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a uint converted object.
             */
            static double toDouble(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a TQ_LLONG.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a TQ_LLONG converted object.
             */
            static TQ_LLONG toLLONG(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a TQ_ULLONG.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a TQ_ULLONG converted object.
             */
            static TQ_ULLONG toULLONG(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a boolean value.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a bool converted object.
             */
            static bool toBool(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a TQStringList.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a TQValueList converted object.
             */
            static TQStringList toStringList(Object::Ptr object);

            /**
             * Try to convert the given \a Object into
             * a TQValueList of TQVariant's.
             *
             * \throw TypeException If the convert failed.
             * \param object The object to convert.
             * \return The to a TQValueList converted object.
             */
            static TQValueList<TQVariant> toList(Object::Ptr object);

    };

}}

#endif