lib

callable.cpp

00001 /***************************************************************************
00002  * callable.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 "callable.h"
00021 #include "variant.h"
00022 #include "dict.h"
00023 
00024 #include <kdebug.h>
00025 
00026 using namespace Kross::Api;
00027 
00028 Callable::Callable(const QString& name, Object::Ptr parent, const ArgumentList& arglist)
00029     : Object(name, parent)
00030     , m_arglist(arglist)
00031 {
00032 }
00033 
00034 Callable::~Callable()
00035 {
00036 }
00037 
00038 const QString Callable::getClassName() const
00039 {
00040     return "Kross::Api::Callable";
00041 }
00042 
00043 Object::Ptr Callable::call(const QString& name, List::Ptr arguments)
00044 {
00045 #ifdef KROSS_API_CALLABLE_CALL_DEBUG
00046     kdDebug() << QString("Kross::Api::Callable::call() name=%1 getName()=%2 arguments=%3").arg(name).arg(getName()).arg(arguments ? arguments->toString() : QString("")) << endl;
00047 #endif
00048 
00049     if(name == "get") {
00050         //checkArguments( ArgumentList() << Argument("Kross::Api::Variant::String") );
00051         return getChild(arguments);
00052     }
00053     else if(name == "has") {
00054         //checkArguments( ArgumentList() << Argument("Kross::Api::Variant::String") );
00055         return hasChild(arguments);
00056     }
00057     else if(name == "call") {
00058         //checkArguments( ArgumentList() << Argument("Kross::Api::Variant::String") << Argument("Kross::Api::List", new List( QValueList<Object::Ptr>() )) );
00059         return callChild(arguments);
00060     }
00061     else if(name == "list") {
00062         //checkArguments( ArgumentList() << Argument("Kross::Api::Variant::String") << Argument("Kross::Api::List", new List( QValueList<Object::Ptr>() )) );
00063         return getChildrenList(arguments);
00064     }
00065     else if(name == "dict") {
00066         //checkArguments( ArgumentList() << Argument("Kross::Api::Variant::String") << Argument("Kross::Api::List", new List( QValueList<Object::Ptr>() )) );
00067         return getChildrenDict(arguments);
00068     }
00069 
00070     return Object::call(name, arguments);
00071 }
00072 
00073 #if 0
00074 void Callable::checkArguments(List::Ptr arguments)
00075 {
00076 #ifdef KROSS_API_CALLABLE_CHECKARG_DEBUG
00077     kdDebug() << QString("Kross::Api::Callable::checkArguments() getName()=%1 arguments=%2")
00078                  .arg(getName()).arg(arguments ? arguments->toString() : QString::null) << endl;
00079 #endif
00080 
00081     QValueList<Object::Ptr>& arglist = arguments->getValue();
00082 
00083     // check the number of parameters passed.
00084     if(arglist.size() < m_arglist.getMinParams())
00085         throw Exception::Ptr( new Exception(QString("Too few parameters for callable object '%1'.").arg(getName())) );
00086     // Don't check if the user passed more arguments as allowed cause it's cheaper
00087     // to just ignore the additional arguments.
00088     //if(arglist.size() > m_arglist.getMaxParams())
00089     //    throw Exception::Ptr( new Exception(QString("Too many parameters for callable object '%1'.").arg(getName())) );
00090 
00091     // check type of passed parameters.
00092     QValueList<Argument>& farglist = m_arglist;
00093     QValueList<Argument>::Iterator it = farglist.begin();
00094     QValueList<Object::Ptr>::Iterator argit = arglist.begin();
00095     bool argend = ( argit == arglist.end() );
00096     for(; it != farglist.end(); ++it) {
00097         //if(! argend)
00098 /*
00099 
00100         if( ! (*it).isVisible() ) {
00101             // if the argument isn't visibled, we always use the default argument.
00102             if(argend)
00103                 arglist.append( (*it).getObject() );
00104             else
00105                 arglist.insert(argit, (*it).getObject());
00106         }
00107         else {
00108             // the argument is visibled and therefore the passed arguments may
00109             // define the value.
00110 
00111             if(argend) {
00112 */
00113             if(argend) {
00114                 // no argument defined, use the default value.
00115                 arglist.append( (*it).getObject() );
00116             }
00117             else {
00118 
00119                 // Check if the type of the passed argument matches to what we 
00120                 // expect. The given argument could have just the same type like 
00121                 // the expected argument or could be a specialization of it.
00122                 QString fcn = (*it).getClassName(); // expected argument
00123                 QString ocn = (*argit)->getClassName(); // given argument
00124                 if(! ocn.startsWith(fcn)) {
00125                     if(! (ocn.startsWith("Kross::Api::Variant") && fcn.startsWith("Kross::Api::Variant")))
00126                         throw Exception::Ptr( new Exception(QString("Callable object '%1' expected parameter of type '%2', but got '%3'").arg(getName()).arg(fcn).arg(ocn)) );
00127                 }
00128 ++argit;
00129 argend = ( argit == arglist.end() );
00130             }
00131         //}
00132 
00133         //if(! argend)
00134 
00135     }
00136 }
00137 #endif
00138 
00139 Object::Ptr Callable::hasChild(List::Ptr args)
00140 {
00141     //kdDebug() << QString("Kross::Api::Callable::hasChild() getName()=%1").arg(getName()) << endl;
00142     return new Variant( Object::hasChild( Variant::toString(args->item(0)) ),
00143                         "Kross::Api::Callable::hasChild::Bool" );
00144 }
00145 
00146 Object::Ptr Callable::getChild(List::Ptr args)
00147 {
00148     QString s = Variant::toString(args->item(0));
00149     //kdDebug() << QString("Kross::Api::Callable::getChild() getName()=%1 childName=%2").arg(getName()).arg(s) << endl;
00150     Object::Ptr obj = Object::getChild(s);
00151     if(! obj)
00152         throw Exception::Ptr( new Exception(QString("The object '%1' has no child object '%2'").arg(getName()).arg(s)) );
00153     return obj;
00154 }
00155 
00156 Object::Ptr Callable::getChildrenList(List::Ptr)
00157 {
00158     QStringList list;
00159     QMap<QString, Object::Ptr> children = getChildren();
00160     QMap<QString, Object::Ptr>::Iterator it( children.begin() );
00161     for(; it != children.end(); ++it)
00162         list.append( it.key() );
00163     return new Variant(list);
00164 }
00165 
00166 Object::Ptr Callable::getChildrenDict(List::Ptr)
00167 {
00168     //kdDebug()<<"Kross::Api::Callable::getChildrenDict()"<<endl;
00169     return new Dict(Object::getChildren(), "Kross::Api::Callable::getChildrenDict::Dict");
00170 }
00171 
00172 Object::Ptr Callable::callChild(List::Ptr args)
00173 {
00174     //kdDebug() << QString("Kross::Api::Callable::callChild() getName()=%1").arg(getName()) << endl;
00175     return Object::call(Variant::toString(args->item(0)), args);
00176 }
KDE Home | KDE Accessibility Home | Description of Access Keys