list.h

00001 /***************************************************************************
00002  * list.h
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #ifndef KROSS_API_LIST_H
00021 #define KROSS_API_LIST_H
00022 
00023 #include <qstring.h>
00024 #include <qvaluelist.h>
00025 #include <qintdict.h>
00026 
00027 #include "object.h"
00028 #include "value.h"
00029 
00030 namespace Kross { namespace Api {
00031 
00032     /**
00033      * The List class implementates \a Value to handle
00034      * lists and collections.
00035      */
00036     class List : public Value< List, QValueList<Object::Ptr> >
00037     {
00038             friend class Value< List, QValueList<Object::Ptr> >;
00039         public:
00040 
00041             /**
00042              * Shared pointer to implement reference-counting.
00043              */
00044             typedef KSharedPtr<List> Ptr;
00045 
00046             /**
00047              * Constructor.
00048              *
00049              * \param value The list of \a Object instances this
00050              *        list has initialy.
00051              * \param name A name this list has.
00052              */
00053             List(QValueList<Object::Ptr> value = QValueList<Object::Ptr>(), const QString& name = "list");
00054 
00055             /**
00056              * Destructor.
00057              */
00058             virtual ~List();
00059 
00060             /**
00061              * See \see Kross::Api::Object::getClassName()
00062              */
00063             virtual const QString getClassName() const;
00064 
00065             /**
00066              * \return a string representation of the whole list.
00067              *
00068              * \see Kross::Api::Object::toString()
00069              */
00070             virtual const QString toString();
00071 
00072             /**
00073              * Return the \a Object with defined index from the
00074              * QValueList this list holds.
00075              *
00076              * \throw TypeException If index is out of bounds.
00077              * \param idx The QValueList-index.
00078              * \return The \a Object instance.
00079              */
00080             Object::Ptr item(uint idx);
00081 
00082             /**
00083              * Return the number of items in the QValueList this
00084              * list holds.
00085              *
00086              * \return The number of items.
00087              */
00088             uint count();
00089 
00090             /**
00091              * Append an \a Kross::Api::Object to the list.
00092              *
00093              * \param object The \a Kross::Api::Object instance to
00094              *       append to this list.
00095              */
00096             void append(Object::Ptr object);
00097 
00098     };
00099 
00100     /**
00101      * This template class extends the \a List class with a more
00102      * generic way to deal with lists.
00103      */
00104     template< class OBJECT, class TYPE >
00105     class ListT : public List
00106     {
00107         public:
00108             ListT(QValueList<TYPE> values) : List(values) {}
00109 
00110             ListT(QIntDict<TYPE> values) : List() {
00111                 QIntDictIterator<TYPE> it( values );
00112                 TYPE *t;
00113                 while( (t = it.current()) != 0 ) {
00114                     this->append( new OBJECT(t) );
00115                     ++it;
00116                 }
00117             }
00118 
00119             ListT(const QPtrList<TYPE> values) : List() {
00120                 QPtrListIterator<TYPE> it( values );
00121                 TYPE *t;
00122                 while( (t = it.current()) != 0 ) {
00123                     this->append( new OBJECT(t) );
00124                     ++it;
00125                 }
00126             }
00127 
00128             virtual ~ListT() {}
00129     };
00130 
00131 }}
00132 
00133 #endif
00134 

Generated on Thu Feb 9 17:59:10 2006 for Kross by  doxygen 1.4.6