lib

object.h

00001 /***************************************************************************
00002  * object.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_OBJECT_H
00021 #define KROSS_API_OBJECT_H
00022 
00023 #include <qstring.h>
00024 #include <qvaluelist.h>
00025 #include <qmap.h>
00026 #include <qvariant.h>
00027 //#include <qobject.h>
00028 #include <ksharedptr.h>
00029 
00030 #include "../main/krossconfig.h"
00031 
00032 namespace Kross { namespace Api {
00033 
00034     // Forward declaration.
00035     class List;
00036 
00037     //FIXME We forward declare class Exception here while it's
00038     //     used at the static fromObject() template-method.
00039     //     Could that provide probs on !=gcc? Maybe that method
00040     //     should go into it's own sourcefile anyway...
00041     class Exception;
00042 
00056     class Object : public KShared
00057     {
00058         public:
00059 
00063             typedef KSharedPtr<Object> Ptr;
00064 
00074             explicit Object(const QString& name, Object::Ptr parent = 0);
00075 
00079             virtual ~Object();
00080 
00086             const QString& getName() const;
00087 
00095             virtual const QString getClassName() const = 0;
00096 
00102             virtual const QString toString();
00103 
00111             Object::Ptr getParent() const;
00112 
00118             bool hasChild(const QString& name) const;
00119 
00128             Object::Ptr getChild(const QString& name) const;
00129 
00135             QMap<QString, Object::Ptr> getChildren() const;
00136 
00146             bool addChild(Object::Ptr object, const QString& name = QString::null);
00147 
00155             void removeChild(const QString& name);
00156 
00160             void removeAllChildren();
00161 
00179             virtual Object::Ptr call(const QString& name, KSharedPtr<List> arguments);
00180 
00186             virtual QStringList getCalls() { return QStringList(); }
00187             //FIXME replace function with getChildren() functionality ?
00188 
00198             template<class T> static T* fromObject(Object::Ptr object)
00199             {
00200                 T* t = (T*) object.data();
00201                 if(! t)
00202                     throw KSharedPtr<Exception>( new Exception(QString("Object \"%1\" invalid.").arg(object ? object->getClassName() : "")) );
00203                 return t;
00204             }
00205 
00206         private:
00208             QString m_name;
00210             Object::Ptr m_parent;
00212             QMap<QString, Object::Ptr> m_children;
00213     };
00214 
00215 }}
00216 
00217 #endif
00218 
KDE Home | KDE Accessibility Home | Description of Access Keys