summaryrefslogtreecommitdiffstats
path: root/lib/kross/api/list.h
blob: 77ea7ea141d46567febfcd9f7294c27ed361ffe6 (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
/***************************************************************************
 * list.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_LIST_H
#define KROSS_API_LIST_H

#include <tqstring.h>
#include <tqvaluelist.h>
#include <tqintdict.h>

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

namespace Kross { namespace Api {

    /**
     * The List class implementates \a Value to handle
     * lists and collections.
     */
    class List : public Value< List, TQValueList<Object::Ptr> >
    {
            friend class Value< List, TQValueList<Object::Ptr> >;
        public:

            /**
             * Shared pointer to implement reference-counting.
             */
            typedef TDESharedPtr<List> Ptr;

/*
            operator TQStringList () {
                //TQValueList<Object::Ptr> getValue()
                krossdebug("999999999999 ...........................");
                return TQStringList();
            }
*/

            /**
             * Constructor.
             *
             * \param value The list of \a Object instances this
             *        list has initialy.
             */
            List(TQValueList<Object::Ptr> value = TQValueList<Object::Ptr>());

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

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

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

            /**
             * Return the \a Object with defined index from the
             * TQValueList this list holds.
             *
             * \throw TypeException If index is out of bounds.
             * \param idx The TQValueList-index.
             * \param defaultobject The default \a Object which should
             *        be used if there exists no item with such an
             *        index. This \a Object instance will be returned
             *        if not NULL and if the index is out of bounds. If
             *        its NULL a \a TypeException will be thrown.
             * \return The \a Object instance.
             */
            Object::Ptr item(uint idx, Object* defaultobject = 0);

            /**
             * Return the number of items in the TQValueList this
             * list holds.
             *
             * \return The number of items.
             */
            uint count();

            /**
             * Append an \a Kross::Api::Object to the list.
             *
             * \param object The \a Kross::Api::Object instance to
             *       append to this list.
             */
            void append(Object::Ptr object);

            template<typename TYPE>
            static Object::Ptr toObject(TYPE t) { return t; }
    };

    /**
     * This template class extends the \a List class with
     * generic functionality to deal with lists.
     */
    template< class OBJECT >
    class ListT : public List
    {
        public:
            ListT() : List() {}
            ListT(TQValueList<OBJECT> values) : List(values) {}

            template< typename TYPE >
            ListT(TQValueList<TYPE> values) : List()
            {
                TQValueListIterator<TYPE> it(values.begin()), end(values.end());
                for(; it != end; ++it)
                    this->append( new OBJECT(*it) );
            }

            template< typename TYPE >
            ListT(TQIntDict<TYPE> values) : List()
            {
                TQIntDictIterator<TYPE> it( values );
                TYPE *t;
                while( (t = it.current()) != 0 ) {
                    this->append( new OBJECT(t) );
                    ++it;
                }
            }

            template< typename TYPE >
            ListT(const TQPtrList<TYPE> values) : List()
            {
                TQPtrListIterator<TYPE> it(values);
                TYPE *t;
                while( (t = it.current()) != 0 ) {
                    this->append( new OBJECT(t) );
                    ++it;
                }
            }

            virtual ~ListT() {}

            template<typename TYPE>
            static Object::Ptr toObject(TYPE t)
            {
                return new ListT(t);
            }
    };

}}

#endif