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 
00043     /**
00044      * The common Object class all other object-classes are
00045      * inheritated from.
00046      *
00047      * The Object class is used as base class to provide
00048      * common functionality. It's similar to what we know 
00049      * in Python as PyObject or in Qt as QObject.
00050      *
00051      * Inherited from e.g. \a Value, \a Module and \a Class .
00052      *
00053      * This class implementates reference counting for shared
00054      * objects. So, no need to take care of freeing objects.
00055      */
00056     class Object : public KShared
00057     {
00058         public:
00059 
00060             /**
00061              * Shared pointer to implement reference-counting.
00062              */
00063             typedef KSharedPtr<Object> Ptr;
00064 
00065             /**
00066              * Constructor.
00067              *
00068              * \param name The name this object has. Return
00069              *        it via \a getName() and set a new
00070              *        name via \a setName().
00071              * \param parent The parent \a Object or NULL if
00072              *        this object doesn't has an parent.
00073              */
00074             explicit Object(const QString& name, Object::Ptr parent = 0);
00075 
00076             /**
00077              * Destructor.
00078              */
00079             virtual ~Object();
00080 
00081             /**
00082              * Return the name this object has.
00083              *
00084              * \return Name of this object.
00085              */
00086             const QString& getName() const;
00087 
00088             /**
00089              * Return the class name. This could be something
00090              * like "Kross::Api::Object" for this object. The
00091              * value is mainly used for display purposes.
00092              *
00093              * \return The name of this class.
00094              */
00095             virtual const QString getClassName() const = 0;
00096 
00097             /**
00098              * \return a string representation of the object or
00099              * it's content. This method is mainly used for
00100              * debugging and testing purposes.
00101              */
00102             virtual const QString toString();
00103 
00104             /**
00105              * Return the parent object or NULL if this object
00106              * doesn't has a parent.
00107              *
00108              * \return The parent-Object or NULL if this Object
00109              *         doesn't has a parent.
00110              */
00111             Object::Ptr getParent() const;
00112 
00113             /**
00114              * Returns if the defined child is avaible.
00115              *
00116              * \return true if child exists else false.
00117              */
00118             bool hasChild(const QString& name) const;
00119 
00120             /**
00121              * Return the defined child or NULL if there is
00122              * no such object with that name avaible.
00123              *
00124              * \param name The name of the Object to return.
00125              * \return The Object matching to the defined
00126              *         name or NULL if there is no such Object.
00127              */
00128             Object::Ptr getChild(const QString& name) const;
00129 
00130             /**
00131              * Return all children.
00132              *
00133              * \return A \a ObjectMap of children this Object has.
00134              */
00135             QMap<QString, Object::Ptr> getChildren() const;
00136 
00137             /**
00138              * Add a new child. Replaces a possible already existing
00139              * child with such a name.
00140              *
00141              * \param name the name of the child
00142              * \param object The Object to add.
00143              * \return true if the Object was added successfully
00144              *         else false.
00145              */
00146             bool addChild(Object::Ptr object, const QString& name = QString::null);
00147 
00148             /**
00149              * Remove an existing child.
00150              *
00151              * \param name The name of the Object to remove.
00152              *        If there doesn't exists an Object with
00153              *        such name just nothing will be done.
00154              */
00155             void removeChild(const QString& name);
00156 
00157             /**
00158              * Remove all children.
00159              */
00160             void removeAllChildren();
00161 
00162             /**
00163              * Pass a call to the object and evaluated it recursive
00164              * down the object-hierachy. Objects like \a Class are able
00165              * to handle call's by just implementing this function.
00166              * If the call is done the \a called() method will be
00167              * executed recursive from bottom up the call hierachy.
00168              *
00169              * \throws TypeException if the object or the name
00170              *         is not callable.
00171              * \param name Each call has a name that says what
00172              *        should be called. In the case of a \a Class
00173              *        the name is the functionname.
00174              * \param arguments The list of arguments passed to
00175              *        the call.
00176              * \return The call-result as \a Object::Ptr instance or
00177              *         NULL if the call has no result.
00178              */
00179             virtual Object::Ptr call(const QString& name, KSharedPtr<List> arguments);
00180 
00181             /**
00182              * Return a list of supported callable objects.
00183              *
00184              * \return List of supported calls.
00185              */
00186             virtual QStringList getCalls() { return QStringList(); }
00187             //FIXME replace function with getChildren() functionality ?
00188 
00189             /**
00190              * Try to convert the \a Object instance to the
00191              * template class T.
00192              *
00193              * \throw TypeException if the cast failed.
00194              * \param object The Object to cast.
00195              * \return The to a instance from template type T
00196              *         casted Object.
00197              */
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:
00207             /// Name of this object.
00208             QString m_name;
00209             /// The parent object.
00210             Object::Ptr m_parent;
00211             /// A list of childobjects.
00212             QMap<QString, Object::Ptr> m_children;
00213     };
00214 
00215 }}
00216 
00217 #endif
00218 

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