lib
callable.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00051 return getChild(arguments);
00052 }
00053 else if(name == "has") {
00054
00055 return hasChild(arguments);
00056 }
00057 else if(name == "call") {
00058
00059 return callChild(arguments);
00060 }
00061 else if(name == "list") {
00062
00063 return getChildrenList(arguments);
00064 }
00065 else if(name == "dict") {
00066
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
00084 if(arglist.size() < m_arglist.getMinParams())
00085 throw Exception::Ptr( new Exception(QString("Too few parameters for callable object '%1'.").arg(getName())) );
00086
00087
00088
00089
00090
00091
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
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 if(argend) {
00114
00115 arglist.append( (*it).getObject() );
00116 }
00117 else {
00118
00119
00120
00121
00122 QString fcn = (*it).getClassName();
00123 QString ocn = (*argit)->getClassName();
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
00134
00135 }
00136 }
00137 #endif
00138
00139 Object::Ptr Callable::hasChild(List::Ptr args)
00140 {
00141
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
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
00169 return new Dict(Object::getChildren(), "Kross::Api::Callable::getChildrenDict::Dict");
00170 }
00171
00172 Object::Ptr Callable::callChild(List::Ptr args)
00173 {
00174
00175 return Object::call(Variant::toString(args->item(0)), args);
00176 }
|