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
24#include "../main/krossconfig.h"
25
26using namespace Kross::Api;
27
28Callable::Callable(const TQString& name)
29 : Object()
30 , m_name(name)
31{
32}
33
37
38const TQString Callable::getName() const
39{
40 return m_name;
41}
42
43const TQString Callable::getClassName() const
44{
45 return "Kross::Api::Callable";
46}
47
48bool Callable::hasChild(const TQString& name) const
49{
50 return m_children.contains(name);
51}
52
53Object::Ptr Callable::getChild(const TQString& name) const
54{
55 return m_children[name];
56}
57
58TQMap<TQString, Object::Ptr> Callable::getChildren() const
59{
60 return m_children;
61}
62
63bool Callable::addChild(const TQString& name, Object* object)
64{
65#ifdef KROSS_API_OBJECT_ADDCHILD_DEBUG
66 krossdebug( TQString("Kross::Api::Callable::addChild() object.name='%1' object.classname='%2'")
67 .arg(name).arg(object->getClassName()) );
68#endif
69 m_children.replace(name, Object::Ptr(object));
70 return true;
71}
72
74{
75 return addChild(object->getName(), object);
76}
77
78void Callable::removeChild(const TQString& name)
79{
80#ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
81 krossdebug( TQString("Kross::Api::Callable::removeChild() name='%1'").arg(name) );
82#endif
83 m_children.remove(name);
84}
85
87{
88#ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
89 krossdebug( "Kross::Api::Callable::removeAllChildren()" );
90#endif
91 m_children.clear();
92}
93
94Object::Ptr Callable::call(const TQString& name, List::Ptr args)
95{
96#ifdef KROSS_API_CALLABLE_CALL_DEBUG
97 krossdebug( TQString("Kross::Api::Callable::call() name=%1 getName()=%2 arguments=%3").arg(name).arg(getName()).arg(args ? args->toString() : TQString("")) );
98#endif
99
100 if(name.isEmpty()) // return a self-reference if no functionname is defined.
101 return this;
102
103 // if name is defined try to get the matching child and pass the call to it.
104 Object::Ptr object = getChild(name);
105 if(object) {
106 //TODO handle namespace, e.g. "mychild1.mychild2.myfunction"
107 return object->call(name, args);
108 }
109
110 if(name == "get") {
111 TQString s = Variant::toString(args->item(0));
112 Object::Ptr obj = getChild(s);
113 if(! obj)
114 throw Exception::Ptr( new Exception(TQString("The object '%1' has no child object '%2'").arg(getName()).arg(s)) );
115 return obj;
116 }
117 else if(name == "has") {
118 return new Variant( hasChild( Variant::toString(args->item(0)) ) );
119 }
120 else if(name == "call") {
121 //TODO should we remove first args-item?
122 return Object::call(Variant::toString(args->item(0)), args);
123 }
124 else if(name == "list") {
125 TQStringList list;
126 TQMap<TQString, Object::Ptr> children = getChildren();
127 TQMap<TQString, Object::Ptr>::Iterator it( children.begin() );
128 for(; it != children.end(); ++it)
129 list.append( it.key() );
130 return new Variant(list);
131 }
132 else if(name == "dict") {
133 return new Dict( getChildren() );
134 }
135
136 // If there exists no such object return NULL.
137 krossdebug( TQString("Object '%1' has no callable object named '%2'.").arg(getName()).arg(name) );
138 return 0;
139}
TQMap< TQString, Object::Ptr > getChildren() const
Return all children.
Definition callable.cpp:58
virtual ~Callable()
Destructor.
Definition callable.cpp:34
bool hasChild(const TQString &name) const
Returns if the defined child is avaible.
Definition callable.cpp:48
virtual Object::Ptr call(const TQString &name, List::Ptr arguments)
Call the object.
Definition callable.cpp:94
bool addChild(const TQString &name, Object *object)
Add a new child.
Definition callable.cpp:63
const TQString getName() const
Definition callable.cpp:38
Callable(const TQString &name)
Constructor.
Definition callable.cpp:28
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:53
virtual const TQString getClassName() const
Return the class name.
Definition callable.cpp:43
void removeAllChildren()
Remove all children.
Definition callable.cpp:86
void removeChild(const TQString &name)
Remove an existing child.
Definition callable.cpp:78
The Dict class implementates Value to handle key=value base dictonaries/maps.
Definition dict.h:36
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:44
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:57
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:31
void krossdebug(const TQString &s)
Debugging function.