Kross::Api::Variant Class Reference

#include <variant.h>

Inheritance diagram for Kross::Api::Variant:

Inheritance graph
[legend]
Collaboration diagram for Kross::Api::Variant:

Collaboration graph
[legend]
List of all members.

Detailed Description

Variant value to wrap a QVariant into a Kross::Api::Value to enable primitive types like strings or numerics.

Definition at line 37 of file variant.h.

Public Types

typedef KSharedPtr< ObjectPtr

Public Member Functions

 Variant (const QVariant &value, const QString &name="variant")
 operator bool ()
 operator int ()
 operator uint ()
 operator double ()
 operator const char * ()
 operator QString ()
 operator const QString ()
 operator const QString & ()
 operator QCString ()
 operator const QCString ()
 operator const QCString & ()
 operator const QVariant & ()
virtual ~Variant ()
virtual const QString getClassName () const
virtual const QString toString ()
V & getValue ()
const QStringgetName () const
Object::Ptr getParent () const
bool hasChild (const QString &name) const
Object::Ptr getChild (const QString &name) const
QMap< QString, Object::PtrgetChildren () const
bool addChild (Object::Ptr object, const QString &name=QString::null)
void removeChild (const QString &name)
void removeAllChildren ()
virtual Object::Ptr call (const QString &name, KSharedPtr< List > arguments)
virtual QStringList getCalls ()

Static Public Member Functions

static const QString getVariantType (Object::Ptr object)
static const QVarianttoVariant (Object::Ptr object)
static const QString toString (Object::Ptr object)
static int toInt (Object::Ptr object)
static uint toUInt (Object::Ptr object)
static double toDouble (Object::Ptr object)
static Q_LLONG toLLONG (Object::Ptr object)
static Q_ULLONG toULLONG (Object::Ptr object)
static bool toBool (Object::Ptr object)
static QValueList< QVarianttoList (Object::Ptr object)
template<class T>
static T * fromObject (Object::Ptr object)

Friends

class Value< Variant, QVariant >


Member Typedef Documentation

typedef KSharedPtr<Object> Kross::Api::Object::Ptr [inherited]
 

Shared pointer to implement reference-counting.

Reimplemented in Kross::Api::Callable, Kross::Api::EventAction, Kross::Api::EventScript, Kross::Api::EventSignal, Kross::Api::EventSlot, Kross::Api::Exception, Kross::Api::List, Kross::Api::Module, Kross::Api::QtObject, Kross::Api::MainModule, Kross::Api::ScriptAction, and Kross::Api::ScriptContainer.

Definition at line 63 of file object.h.


Constructor & Destructor Documentation

Variant::Variant const QVariant value,
const QString name = "variant"
 

Constructor.

Parameters:
value The initial QVariant-value this Variant-Object has.
name The name this Value has.

Definition at line 27 of file variant.cpp.

00028     : Value<Variant, QVariant>(value, name)
00029 {
00030 }

Variant::~Variant  )  [virtual]
 

Destructor.

Definition at line 32 of file variant.cpp.

00033 {
00034 }


Member Function Documentation

bool Object::addChild Object::Ptr  object,
const QString name = QString::null
[inherited]
 

Add a new child. Replaces a possible already existing child with such a name.

Parameters:
name the name of the child
object The Object to add.
Returns:
true if the Object was added successfully else false.

Definition at line 79 of file object.cpp.

References QString::arg(), QString::isEmpty(), QString::isNull(), and QMap::replace().

Referenced by Kross::Api::MainModule::addKAction(), Kross::Api::MainModule::addQObject(), Kross::Api::MainModule::addSignal(), Kross::Api::MainModule::addSlot(), Kross::Api::QtObject::QtObject(), and TestPluginModule::TestPluginModule().

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 }

virtual Object::Ptr Kross::Api::Object::call const QString name,
KSharedPtr< List arguments
[virtual, inherited]
 

Pass a call to the object and evaluated it recursive down the object-hierachy. Objects like Class are able to handle call's by just implementing this function. If the call is done the called() method will be executed recursive from bottom up the call hierachy.

Exceptions:
TypeException if the object or the name is not callable.
Parameters:
name Each call has a name that says what should be called. In the case of a Class the name is the functionname.
arguments The list of arguments passed to the call.
Returns:
The call-result as Object::Ptr instance or NULL if the call has no result.

Reimplemented in Kross::Api::EventScript, Kross::Api::EventSignal, and Kross::Api::EventSlot.

Referenced by Kross::Api::Callable::call(), and Kross::Api::Callable::callChild().

template<class T>
static T* Kross::Api::Object::fromObject Object::Ptr  object  )  [inline, static, inherited]
 

Try to convert the Object instance to the template class T.

Exceptions:
TypeException if the cast failed.
Parameters:
object The Object to cast.
Returns:
The to a instance from template type T casted Object.

Definition at line 198 of file object.h.

00199             {
00200                 T* t = (T*) object.data();
00201                 if(! t)
00202                     throw KSharedPtr<Exception>( new Exception(QString("Object \"%1\" invalid.").arg(object ? object->getClassName() : "")) );
00203                 return t;
00204             }

virtual QStringList Kross::Api::Object::getCalls  )  [inline, virtual, inherited]
 

Return a list of supported callable objects.

Returns:
List of supported calls.

Reimplemented in Kross::Python::PythonObject.

Definition at line 186 of file object.h.

00186 { return QStringList(); }

Object::Ptr Object::getChild const QString name  )  const [inherited]
 

Return the defined child or NULL if there is no such object with that name avaible.

Parameters:
name The name of the Object to return.
Returns:
The Object matching to the defined name or NULL if there is no such Object.

Definition at line 69 of file object.cpp.

Referenced by Kross::Api::Callable::getChild().

00070 {
00071     return m_children[name];
00072 }

QMap< QString, Object::Ptr > Object::getChildren  )  const [inherited]
 

Return all children.

Returns:
A ObjectMap of children this Object has.

Definition at line 74 of file object.cpp.

Referenced by Kross::Api::Callable::getChildrenDict(), and Kross::Api::Callable::getChildrenList().

00075 {
00076     return m_children;
00077 }

const QString Variant::getClassName  )  const [virtual]
 

See also:
Kross::Api::Object::getClassName()

Implements Kross::Api::Object.

Definition at line 36 of file variant.cpp.

00037 {
00038     return "Kross::Api::Variant";
00039 }

const QString & Object::getName  )  const [inherited]
 

Return the name this object has.

Returns:
Name of this object.

Reimplemented in Kross::Api::ScriptContainer.

Definition at line 49 of file object.cpp.

Referenced by Kross::Api::Event< QtObject >::call(), Kross::Api::Callable::call(), Kross::Api::Callable::getChild(), and Kross::Api::Module::~Module().

00050 {
00051     return m_name;
00052 }

Object::Ptr Object::getParent  )  const [inherited]
 

Return the parent object or NULL if this object doesn't has a parent.

Returns:
The parent-Object or NULL if this Object doesn't has a parent.

Definition at line 59 of file object.cpp.

00060 {
00061     return m_parent;
00062 }

template<class T, class V>
V& Kross::Api::Value< T, V >::getValue  )  [inline, inherited]
 

Return the value.

Returns:
The value this Value-class holds.

Definition at line 66 of file value.h.

Referenced by Kross::Api::List::append(), Kross::Api::List::count(), Kross::Api::List::item(), operator bool(), operator const char *(), operator const QCString(), operator const QCString &(), operator const QString(), operator const QString &(), operator const QVariant &(), operator double(), operator int(), operator QCString(), operator QString(), operator uint(), toList(), toString(), Kross::Api::List::toString(), Kross::Api::Dict::toString(), and toVariant().

00066 { return m_value; }

const QString Variant::getVariantType Object::Ptr  object  )  [static]
 

Returns:
a more detailed classname for the passed object variant type.
Exceptions:
TypeException If the object isn't a valid Variant instance.
Parameters:
object the variant object we should return a more detailed classname for.
Returns:
If as example the passed object is a QVariant::String then "Kross::Api::Variant::String" will be returned.

Definition at line 46 of file variant.cpp.

References toVariant().

00047 {
00048     switch( toVariant(object).type() ) {
00049 
00050         case QVariant::CString:
00051         case QVariant::String:
00052             return "Kross::Api::Variant::String";
00053 
00054         case QVariant::Map:
00055             return "Kross::Api::Variant::Dict";
00056 
00057         case QVariant::StringList:
00058         case QVariant::List:
00059             return "Kross::Api::Variant::List";
00060 
00061         case QVariant::Double:
00062             //return "Kross::Api::Variant::Double";
00063         case QVariant::UInt: 
00064             //return "Kross::Api::Variant::UInt"; // python isn't able to differ between int and uint :-(
00065         case QVariant::LongLong:
00066         case QVariant::ULongLong:
00067         case QVariant::Int:
00068             return "Kross::Api::Variant::Integer";
00069 
00070         case QVariant::Bool:
00071             return "Kross::Api::Variant::Bool";
00072 
00073         default: //Date, Time, DateTime, ByteArray, BitArray, Rect, Size, Color, Invalid, etc.
00074             return "Kross::Api::Variant";
00075     }
00076 }

bool Object::hasChild const QString name  )  const [inherited]
 

Returns if the defined child is avaible.

Returns:
true if child exists else false.

Reimplemented in Kross::Api::MainModule.

Definition at line 64 of file object.cpp.

References QMap::contains().

Referenced by Kross::Api::MainModule::hasChild(), and Kross::Api::Callable::hasChild().

00065 {
00066     return m_children.contains(name);
00067 }

void Object::removeAllChildren  )  [inherited]
 

Remove all children.

Definition at line 104 of file object.cpp.

References QMap::clear().

00105 {
00106 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00107     kdDebug() << "Kross::Api::Object::removeAllChildren()" << endl;
00108 #endif
00109     m_children.clear();
00110 }

void Object::removeChild const QString name  )  [inherited]
 

Remove an existing child.

Parameters:
name The name of the Object to remove. If there doesn't exists an Object with such name just nothing will be done.

Definition at line 96 of file object.cpp.

References QMap::remove().

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 }

bool Variant::toBool Object::Ptr  object  )  [static]
 

Try to convert the given Object into a boolean value.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a bool converted object.

Definition at line 131 of file variant.cpp.

References QVariant::canCast(), QVariant::toBool(), toVariant(), and QVariant::typeName().

00132 {
00133     const QVariant& variant = toVariant(object);
00134     if(! variant.canCast(QVariant::Bool))
00135         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::Bool expected, but got %1.").arg(variant.typeName()).latin1()) );
00136     return variant.toBool();
00137 }

double Variant::toDouble Object::Ptr  object  )  [static]
 

Try to convert the given Object into a uint.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a uint converted object.

Definition at line 107 of file variant.cpp.

References QVariant::canCast(), QVariant::toDouble(), toVariant(), and QVariant::typeName().

00108 {
00109     const QVariant& variant = toVariant(object);
00110     if(! variant.canCast(QVariant::Double))
00111         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::Double expected, but got %1.").arg(variant.typeName()).latin1()) );
00112     return variant.toDouble();
00113 }

int Variant::toInt Object::Ptr  object  )  [static]
 

Try to convert the given Object into a int.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a int converted object.

Definition at line 91 of file variant.cpp.

References QVariant::canCast(), QVariant::toInt(), toVariant(), and QVariant::typeName().

00092 {
00093     const QVariant& variant = toVariant(object);
00094     if(! variant.canCast(QVariant::Int))
00095         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::Int expected, but got %1.").arg(variant.typeName()).latin1()) );
00096     return variant.toInt();
00097 }

QValueList< QVariant > Variant::toList Object::Ptr  object  )  [static]
 

Try to convert the given Object into a QValueList of QVariant's.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a QValueList converted object.

Definition at line 139 of file variant.cpp.

References QValueList::begin(), QValueList::end(), Kross::Api::Value< T, V >::getValue(), and toVariant().

00140 {
00141     if(object->getClassName() == "Kross::Api::List") {
00142         QValueList<QVariant> l;
00143         QValueList<Object::Ptr> list = Object::fromObject<List>( object.data() )->getValue();
00144         for(QValueList<Object::Ptr>::Iterator it = list.begin(); it != list.end(); ++it)
00145             l.append( toVariant(*it) );
00146         return l;
00147     }
00148     const QVariant& variant = toVariant(object);
00149     if(! variant.canCast(QVariant::List))
00150         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::List expected, but got '%1'.").arg(variant.typeName()).latin1()) );
00151     return variant.toList();
00152 }

Q_LLONG Variant::toLLONG Object::Ptr  object  )  [static]
 

Try to convert the given Object into a Q_LLONG.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a Q_LLONG converted object.

Definition at line 115 of file variant.cpp.

References QVariant::canCast(), QVariant::toLongLong(), toVariant(), and QVariant::typeName().

00116 {
00117     const QVariant& variant = toVariant(object);
00118     if(! variant.canCast(QVariant::LongLong))
00119         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::LLONG expected, but got %1.").arg(variant.typeName()).latin1()) );
00120     return variant.toLongLong();
00121 }

const QString Variant::toString Object::Ptr  object  )  [static]
 

Try to convert the given Object into a QString.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a QString converted object.

Definition at line 83 of file variant.cpp.

References QVariant::canCast(), QVariant::toString(), toVariant(), and QVariant::typeName().

00084 {
00085     const QVariant& variant = toVariant(object);
00086     if(! variant.canCast(QVariant::String))
00087         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::String expected, but got %1.").arg(variant.typeName()).latin1()) );
00088     return variant.toString();
00089 }

const QString Variant::toString  )  [virtual]
 

Returns:
a string representation of the variant.
See also:
Kross::Api::Object::toString()

Reimplemented from Kross::Api::Object.

Definition at line 41 of file variant.cpp.

References Kross::Api::Value< T, V >::getValue().

Referenced by Kross::Api::Callable::callChild(), Kross::Api::Callable::getChild(), Kross::Api::Callable::hasChild(), and Kross::Api::QtObject::toQUObject().

00042 {
00043     return getValue().toString();
00044 }

uint Variant::toUInt Object::Ptr  object  )  [static]
 

Try to convert the given Object into a uint.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a uint converted object.

Definition at line 99 of file variant.cpp.

References QVariant::canCast(), QVariant::toUInt(), toVariant(), and QVariant::typeName().

00100 {
00101     const QVariant& variant = toVariant(object);
00102     if(! variant.canCast(QVariant::UInt))
00103         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::UInt expected, but got %1.").arg(variant.typeName()).latin1()) );
00104     return variant.toUInt();
00105 }

Q_ULLONG Variant::toULLONG Object::Ptr  object  )  [static]
 

Try to convert the given Object into a Q_ULLONG.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a Q_ULLONG converted object.

Definition at line 123 of file variant.cpp.

References QVariant::canCast(), QVariant::toULongLong(), toVariant(), and QVariant::typeName().

00124 {
00125     const QVariant& variant = toVariant(object);
00126     if(! variant.canCast(QVariant::ULongLong))
00127         throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::ULLONG expected, but got %1.").arg(variant.typeName()).latin1()) );
00128     return variant.toULongLong();
00129 }

const QVariant & Variant::toVariant Object::Ptr  object  )  [static]
 

Try to convert the given Object into a QVariant.

Exceptions:
TypeException If the convert failed.
Parameters:
object The object to convert.
Returns:
The to a QVariant converted object.

Definition at line 78 of file variant.cpp.

References Kross::Api::Value< T, V >::getValue().

Referenced by getVariantType(), toBool(), toDouble(), toInt(), toList(), toLLONG(), toString(), toUInt(), and toULLONG().

00079 {
00080     return Object::fromObject<Variant>( object.data() )->getValue();
00081 }


The documentation for this class was generated from the following files:
Generated on Thu Feb 9 18:00:54 2006 for Kross by  doxygen 1.4.6