make some nullables scriptable
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Thu, 21 Oct 2010 11:56:13 +0000 (11:56 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Thu, 21 Oct 2010 11:56:13 +0000 (11:56 +0000)
remove bogus shortcut types

git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@605 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

qtbase/include/nullable.h
qtbase/src/nullscript.cpp [new file with mode: 0644]
qtbase/src/object.cpp
woc/qt/qtfiles.qrc [new file with mode: 0644]
woc/qt/qtout.cpp
woc/qt/qtout.h
woc/woc.pro

index 304168a..4417e7b 100644 (file)
@@ -64,18 +64,17 @@ template<class T>class Nullable
 #include<QString>
 #include<QMetaType>
 //convenience wrappers
-typedef Nullable<bool> Bool;Q_DECLARE_METATYPE(Bool)
-typedef Nullable<int> Int;
-typedef Nullable<qint32> Int32;Q_DECLARE_METATYPE(Int32)
-typedef Nullable<qint64> Int64;Q_DECLARE_METATYPE(Int64)
-typedef Nullable<quint32> UInt32;Q_DECLARE_METATYPE(UInt32)
-typedef Nullable<quint64> UInt64;Q_DECLARE_METATYPE(UInt64)
-typedef Nullable<QString> NString;Q_DECLARE_METATYPE(NString)
-typedef Nullable<QByteArray> NByteArray;Q_DECLARE_METATYPE(NByteArray)
+Q_DECLARE_METATYPE(Nullable<bool>)
+Q_DECLARE_METATYPE(Nullable<qint32>)
+Q_DECLARE_METATYPE(Nullable<qint64>)
+Q_DECLARE_METATYPE(Nullable<quint32>)
+Q_DECLARE_METATYPE(Nullable<quint64>)
+Q_DECLARE_METATYPE(Nullable<QString>)
+Q_DECLARE_METATYPE(Nullable<QByteArray>)
 
-inline bool operator==(Int64 i1,int i2){return i1.operator==(i2);}
-inline bool operator==(UInt32 i1,int i2){return i1.operator==(i2);}
-inline bool operator==(UInt64 i1,int i2){return i1.operator==(i2);}
-inline bool operator==(UInt64 i1,unsigned int i2){return i1.operator==(i2);}
+inline bool operator==(Nullable<qint64> i1,int i2){return i1.operator==(i2);}
+inline bool operator==(Nullable<quint32> i1,int i2){return i1.operator==(i2);}
+inline bool operator==(Nullable<quint64> i1,int i2){return i1.operator==(i2);}
+inline bool operator==(Nullable<quint64> i1,unsigned int i2){return i1.operator==(i2);}
 
 #endif
diff --git a/qtbase/src/nullscript.cpp b/qtbase/src/nullscript.cpp
new file mode 100644 (file)
index 0000000..5d18d55
--- /dev/null
@@ -0,0 +1,43 @@
+//start: nullscript.cpp
+//currently this file is copied verbatim into the generated
+//interface code, in the future it may be included here instead
+
+#include <QScriptEngine>
+#include <Nullable>
+
+static QScriptValue Bool_toScriptValue(QScriptEngine*,const Nullable<bool>&obj)
+{
+       if(obj.isNull())return QScriptValue::NullValue;
+       else return QScriptValue(obj.value());
+}
+
+static void Bool_fromScriptValue(const QScriptValue&val,Nullable<bool>&obj)
+{
+       if(val.isNull())obj=Nullable<bool>();
+       if(val.isBool())obj=val.toBool();
+       else obj=Nullable<bool>();
+}
+
+static QScriptValue String_toScriptValue(QScriptEngine*,const Nullable<QString>&obj)
+{
+       if(obj.isNull())return QScriptValue::NullValue;
+       else return QScriptValue(obj.value());
+}
+
+static void String_fromScriptValue(const QScriptValue&val,Nullable<QString>&obj)
+{
+       if(val.isNull())obj=Nullable<QString>();
+       if(val.isString())obj=val.toString();
+       else obj=Nullable<QString>();
+}
+
+//list of supported types
+static void initNullableScripting(QScriptEngine*engine)
+{
+       QScriptValue gval=engine->globalObject();
+       qScriptRegisterMetaType(engine,Bool_toScriptValue,Bool_fromScriptValue);
+       qScriptRegisterMetaType(engine,String_toScriptValue,String_fromScriptValue);
+}
+
+
+//end: nullscript.cpp
index ae172b8..eb0f93d 100644 (file)
 #include "Nullable"
 
 static int itype0=
-       qRegisterMetaType<Int32>()+
-       qRegisterMetaType<Int64>()+
-       qRegisterMetaType<UInt32>()+
-       qRegisterMetaType<UInt64>()+
-       qRegisterMetaType<NString>()+
-       qRegisterMetaType<NByteArray>()+
-       qRegisterMetaType<Bool>();
+       qRegisterMetaType<Nullable<qint32> >()+
+       qRegisterMetaType<Nullable<qint64> >()+
+       qRegisterMetaType<Nullable<quint32> >()+
+       qRegisterMetaType<Nullable<quint64> >()+
+       qRegisterMetaType<Nullable<QString> >()+
+       qRegisterMetaType<Nullable<QByteArray> >()+
+       qRegisterMetaType<Nullable<bool> >();
diff --git a/woc/qt/qtfiles.qrc b/woc/qt/qtfiles.qrc
new file mode 100644 (file)
index 0000000..1a13a3f
--- /dev/null
@@ -0,0 +1,6 @@
+<!DOCTYPE RCC>
+<RCC version="1.0">
+    <qresource>
+        <file alias="nullscript.cpp">../../qtbase/src/nullscript.cpp</file>
+    </qresource>
+</RCC>
index 3cb9581..eb237fb 100644 (file)
@@ -79,11 +79,7 @@ WocQtOut::WocQtOut(QDomElement&el)
        m_iface.write(QString("  Q_INVOKABLE static QString svnRepositoryUrl(){return \""+woc->svnRepositoryUrl()+"\";}\n\n").toAscii());
        
        //init scripting
-       m_scriptcode="#include <QScriptEngine>\n";
-       m_scriptcode+="void "+ifaceClassName()+"::initScriptEngine(QScriptEngine*engine)\n{\n";
-       m_scriptcode+="\tQScriptValue gval=engine->globalObject();\n";
-       m_scriptcode+="\tgval.setProperty(\"Interface\",engine->newQMetaObject(this->metaObject()));\n";
-       m_scriptcode+="\tgval.setProperty(\"interface\",engine->newQObject(this,QScriptEngine::QtOwnership, QScriptEngine::ExcludeDeleteLater));\n";
+       initScripting();
        
        //create all includer
        m_hdr.setFileName(m_basedir+"/"+m_subdir+"/"+m_prefix+"IncludeAll");
@@ -254,3 +250,21 @@ WocQtTransaction::WocQtTransaction(WocQtOut*p)
 {
        connect(this,SIGNAL(errorFound()),p,SIGNAL(errorFound()));
 }
+
+void WocQtOut::initScripting()
+{
+//     m_scriptcode="#include <QScriptEngine>\n";
+       //script converters for nullables
+       QFile nscr(":/nullscript.cpp");nscr.open(QIODevice::ReadOnly);
+       m_scriptcode+=QString::fromAscii(nscr.readAll());
+       m_scriptcode+="\n\n";
+       nscr.close();
+       //initializer routine
+       m_scriptcode+="void "+ifaceClassName()+"::initScriptEngine(QScriptEngine*engine)\n{\n";
+       m_scriptcode+="\tQScriptValue gval=engine->globalObject();\n";
+       //register interfacce itself
+       m_scriptcode+="\tgval.setProperty(\"Interface\",engine->newQMetaObject(this->metaObject()));\n";
+       m_scriptcode+="\tgval.setProperty(\"interface\",engine->newQObject(this,QScriptEngine::QtOwnership, QScriptEngine::ExcludeDeleteLater));\n";
+       //register nullables
+       m_scriptcode+="\tinitNullableScripting(engine);\n";
+}
index a4c83f6..85cabe8 100644 (file)
@@ -88,6 +88,8 @@ class WocQtOut:public WocOutput
                void addScriptInit(QString s){m_scriptcode+=s;}
                ///helper: returns true if sub-classes are supposed to create scripting code
                bool doGenerateScripting()const{return m_genscript;}
+               ///internal: init the scripting env
+               void initScripting();
                
                WocQtClass*qclass;
                WocQtTable*qtable;
index 39ceeb8..d2a2aca 100644 (file)
@@ -20,6 +20,8 @@ HEADERS+= \
        htmlout.h \
        mfile.h \
        domquery.h
+       
+RESOURCES += qt/qtfiles.qrc
 
 include(proc/proc.pri)
 include(php/php.pri)