lib

callable.cpp
1/***************************************************************************
2 * callable.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "callable.h"
21#include "variant.h"
22#include "dict.h"
23#include "krossdebug.h"
24
25using namespace Kross::Api;
26
27Callable::Callable(const TQString& name)
28 : Object()
29 , m_name(name)
30{
31}
32
36
37const TQString Callable::getName() const
38{
39 return m_name;
40}
41
42const TQString Callable::getClassName() const
43{
44 return "Kross::Api::Callable";
45}
46
47bool Callable::hasChild(const TQString& name) const
48{
49 return m_children.contains(name);
50}
51
52Object::Ptr Callable::getChild(const TQString& name) const
53{
54 return m_children[name];
55}
56
57TQMap<TQString, Object::Ptr> Callable::getChildren() const
58{
59 return m_children;
60}
61
62bool Callable::addChild(const TQString& name, Object* object)
63{
64#ifdef KROSS_API_OBJECT_ADDCHILD_DEBUG
65 krossdebug( TQString("Kross::Api::Callable::addChild() object.name='%1' object.classname='%2'")
66 .arg(name).arg(object->getClassName()) );
67#endif
68 m_children.replace(name, Object::Ptr(object));
69 return true;
70}
71
73{
74 return addChild(object->getName(), object);
75}
76
77void Callable::removeChild(const TQString& name)
78{
79#ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
80 krossdebug( TQString("Kross::Api::Callable::removeChild() name='%1'").arg(name) );
81#endif
82 m_children.remove(name);
83}
84
86{
87#ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
88 krossdebug( "Kross::Api::Callable::removeAllChildren()" );
89#endif
90 m_children.clear();
91}
92
93Object::Ptr Callable::call(const TQString& name, List::Ptr args)
94{
95#ifdef KROSS_API_CALLABLE_CALL_DEBUG
96 krossdebug( TQString("Kross::Api::Callable::call() name=%1 getName()=%2 arguments=%3").arg(name).arg(getName()).arg(args ? args->toString() : TQString("")) );
97#endif
98
99 if(name.isEmpty()) // return a self-reference if no functionname is defined.
100 return this;
101
102 // if name is defined try to get the matching child and pass the call to it.
103 Object::Ptr object = getChild(name);
104 if(object) {
105 //TODO handle namespace, e.g. "mychild1.mychild2.myfunction"
106 return object->call(name, args);
107 }
108
109 if(name == "get") {
110 TQString s = Variant::toString(args->item(0));
111 Object::Ptr obj = getChild(s);
112 if(! obj)
113 throw Exception::Ptr( new Exception(TQString("The object '%1' has no child object '%2'").arg(getName()).arg(s)) );
114 return obj;
115 }
116 else if(name == "has") {
117 return new Variant( hasChild( Variant::toString(args->item(0)) ) );
118 }
119 else if(name == "call") {
120 //TODO should we remove first args-item?
121 return Object::call(Variant::toString(args->item(0)), args);
122 }
123 else if(name == "list") {
124 TQStringList list;
125 TQMap<TQString, Object::Ptr> children = getChildren();
126 TQMap<TQString, Object::Ptr>::Iterator it( children.begin() );
127 for(; it != children.end(); ++it)
128 list.append( it.key() );
129 return new Variant(list);
130 }
131 else if(name == "dict") {
132 return new Dict( getChildren() );
133 }
134
135 // If there exists no such object return NULL.
136 krossdebug( TQString("Object '%1' has no callable object named '%2'.").arg(getName()).arg(name) );
137 return 0;
138}
TQMap< TQString, Object::Ptr > getChildren() const
Return all children.
Definition callable.cpp:57
virtual ~Callable()
Destructor.
Definition callable.cpp:33
bool hasChild(const TQString &name) const
Returns if the defined child is avaible.
Definition callable.cpp:47
virtual Object::Ptr call(const TQString &name, List::Ptr arguments)
Call the object.
Definition callable.cpp:93
bool addChild(const TQString &name, Object *object)
Add a new child.
Definition callable.cpp:62
const TQString getName() const
Definition callable.cpp:37
Callable(const TQString &name)
Constructor.
Definition callable.cpp:27
Object::Ptr getChild(const TQString &name) const
Return the defined child or NULL if there is no such object with that name avaible.
Definition callable.cpp:52
virtual const TQString getClassName() const
Return the class name.
Definition callable.cpp:42
void removeAllChildren()
Remove all children.
Definition callable.cpp:85
void removeChild(const TQString &name)
Remove an existing child.
Definition callable.cpp:77
The Dict class implementates Value to handle key=value base dictonaries/maps.
Definition dict.h:38
Common exception class used for representing exceptions in Kross.
Definition exception.h:39
TDESharedPtr< Exception > Ptr
Shared pointer to implement reference-counting.
Definition exception.h:45
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
Definition list.h:46
virtual const TQString getClassName() const =0
Return the class name.
virtual Object::Ptr call(const TQString &name, TDESharedPtr< List > arguments)
Pass a call to the object and evaluated it recursive down the object-hierachy.
Definition object.cpp:50
Object()
Constructor.
Definition object.cpp:29
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:56
Variant value to wrap a TQVariant into a Kross::Api::Value to enable primitive types like strings or ...
Definition variant.h:39
virtual const TQString toString()
Definition variant.cpp:41
The common Kross API used as common codebase.
Definition callable.h:33
KOFFICE_EXPORT void krossdebug(const TQString &s)
Debugging function.