lib

object.cpp

00001 /***************************************************************************
00002  * object.cpp
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 #include "object.h"
00021 #include "list.h"
00022 #include "variant.h"
00023 //#include "function.h"
00024 #include "event.h"
00025 #include "exception.h"
00026 
00027 #include <kdebug.h>
00028 
00029 using namespace Kross::Api;
00030 
00031 Object::Object(const QString& name, Object::Ptr parent)
00032     : KShared()
00033     , m_name(name)
00034     , m_parent(parent)
00035 {
00036 #ifdef KROSS_API_OBJECT_CTOR_DEBUG
00037     kdDebug() << QString("Kross::Api::Object::Constructor() name='%1' refcount='%2'").arg(m_name).arg(_KShared_count()) << endl;
00038 #endif
00039 }
00040 
00041 Object::~Object()
00042 {
00043 #ifdef KROSS_API_OBJECT_DTOR_DEBUG
00044     kdDebug() << QString("Kross::Api::Object::Destructor() name='%1' refcount='%2'").arg(m_name).arg(_KShared_count()) << endl;
00045 #endif
00046     //removeAllChildren(); // not needed cause we use KShared to handle ref-couting and freeing.
00047 }
00048 
00049 const QString& Object::getName() const
00050 {
00051     return m_name;
00052 }
00053 
00054 const QString Object::toString()
00055 {
00056     return QString("%1 (%2)").arg(m_name).arg(getClassName());
00057 }
00058 
00059 Object::Ptr Object::getParent() const
00060 {
00061     return m_parent;
00062 }
00063 
00064 bool Object::hasChild(const QString& name) const
00065 {
00066     return m_children.contains(name);
00067 }
00068 
00069 Object::Ptr Object::getChild(const QString& name) const
00070 {
00071     return m_children[name];
00072 }
00073 
00074 QMap<QString, Object::Ptr> Object::getChildren() const
00075 {
00076     return m_children;
00077 }
00078 
00079 bool Object::addChild(Object::Ptr object, const QString& name)
00080 {
00081     QString n = name.isNull() ? object->getName() : name;
00082 
00083 #ifdef KROSS_API_OBJECT_ADDCHILD_DEBUG
00084     kdDebug() << QString("Kross::Api::Object::addChild() object.name='%2' object.classname='%3'")
00085         .arg(n).arg(object->getClassName()) << endl;
00086 #endif
00087 
00088     if(n.isEmpty()) // prevent invalid items.
00089         return false; //throw Exception::Ptr( new Exception( QString("Failed to add child object to object '%1'. Invalid name for class '%2'.").arg(getName()).arg(object->getClassName()) ) );
00090 
00091     object->m_parent = this;
00092     m_children.replace(n, object);
00093     return true;
00094 }
00095 
00096 void Object::removeChild(const QString& name)
00097 {
00098 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00099     kdDebug() << QString("Kross::Api::Object::removeChild() name='%1'").arg(name) << endl;
00100 #endif
00101     m_children.remove(name);
00102 }
00103 
00104 void Object::removeAllChildren()
00105 {
00106 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00107     kdDebug() << "Kross::Api::Object::removeAllChildren()" << endl;
00108 #endif
00109     m_children.clear();
00110 }
00111 
00112 Object::Ptr Object::call(const QString& name, List::Ptr arguments)
00113 {
00114 #ifdef KROSS_API_OBJECT_CALL_DEBUG
00115     kdDebug() << QString("Kross::Api::Object::call(%1) name=%2 class=%3").arg(name).arg(getName()).arg(getClassName()) << endl;
00116 #endif
00117 
00118     if(name.isEmpty()) // return a self-reference if no functionname is defined.
00119         return this;
00120 
00121     // if name is defined try to get the matching child and pass the call to it.
00122     Object::Ptr object = getChild(name);
00123     if(object) {
00124         //FIXME namespace, e.g. "mychild1.mychild2.myfunction"
00125         return object->call(name, arguments);
00126     }
00127 
00128     // If there exists no such object return NULL.
00129     kdDebug() << QString("Object '%1' has no callable object named '%2'.").arg(getName()).arg(name) << endl;
00130     return 0;
00131 }
00132 
KDE Home | KDE Accessibility Home | Description of Access Keys