Changeset 980 for XmlTools2


Ignore:
Timestamp:
Mar 6, 2014, 5:40:35 PM (11 years ago)
Author:
s10k
Message:

XmlTools
Singleton implementation

Location:
XmlTools2/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • XmlTools2/trunk/util.cpp

    r958 r980  
    88QString AppExecutable="./"+AppName; // Mac OS needs unix like executing
    99#endif
    10 QString AppVersion="2.0a";
     10QString AppVersion="2.0b";
    1111}
    1212
  • XmlTools2/trunk/xmlcustomcode.cpp

    r967 r980  
    11#include "xmlcustomcode.h"
    22
    3 // http://stackoverflow.com/questions/7092765/what-does-it-mean-to-have-an-undefined-reference-to-a-static-member
    4 QVector<QScriptEngine*> XmlCustomCode::scriptEngines;
    5 QVector<QScriptValue*> XmlCustomCode::jsFunctions;
    6 QVector<QScriptValue*> XmlCustomCode::getXmlDataFunctions;
    7 QVector<QScriptValue*> XmlCustomCode::setXmlDataFunctions;
     3XmlCustomCode* XmlCustomCode::uniqueInstance = NULL;
    84
    95QScriptValue echo(QScriptContext *context, QScriptEngine*)
     
    1612XmlCustomCode::XmlCustomCode(): numThreads(omp_get_num_procs()*2)
    1713{
    18     // create individual thread script engines if empty
    19     if(this->scriptEngines.isEmpty()){
    20         this->scriptEngines.reserve(this->numThreads);
    21         this->jsFunctions.reserve(this->numThreads);
    22         this->getXmlDataFunctions.reserve(this->numThreads);
    23         this->setXmlDataFunctions.reserve(this->numThreads);
     14    // create individual thread script engines
     15    this->scriptEngines.reserve(this->numThreads);
     16    this->jsFunctions.reserve(this->numThreads);
     17    this->getXmlDataFunctions.reserve(this->numThreads);
     18    this->setXmlDataFunctions.reserve(this->numThreads);
    2419
    25         QString jsxmlString;
    26         QFile jsxmlfile(":/resources/libs/jsxml.js");
     20    QString jsxmlString;
     21    QFile jsxmlfile(":/resources/libs/jsxml.js");
    2722
    28         jsxmlfile.open(QFile::ReadOnly | QFile::Text);
     23    jsxmlfile.open(QFile::ReadOnly | QFile::Text);
    2924
    30         jsxmlString=QTextStream(&jsxmlfile).readAll();
     25    jsxmlString=QTextStream(&jsxmlfile).readAll();
    3126
    32         for(int i=0; i<this->numThreads; i++){
    33             this->scriptEngines.append(new QScriptEngine());
    34             this->jsFunctions.append(new QScriptValue());
     27    for(int i=0; i<this->numThreads; i++){
     28        this->scriptEngines.append(new QScriptEngine());
     29        this->jsFunctions.append(new QScriptValue());
    3530
    36             // main needs to be called so the user code is evaluated
    37             // alternatively you can do: myFunc=engine.evaluate('(function main(){})'); myFunc.call();
    38             // Note the () around the function
    39             this->getXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function getXmlData() { return $xmlData; })")));
    40             this->setXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function setXmlData(newXmlData) { $xmlData=newXmlData; })")));
     31        // main needs to be called so the user code is evaluated
     32        // alternatively you can do: myFunc=engine.evaluate('(function main(){})'); myFunc.call();
     33        // Note the () around the function
     34        this->getXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function getXmlData() { return $xmlData; })")));
     35        this->setXmlDataFunctions.append(new QScriptValue(this->scriptEngines.at(i)->evaluate("(function setXmlData(newXmlData) { $xmlData=newXmlData; })")));
    4136
    42             // Add echo function so user can debug the code
    43             QScriptValue echoFunction = this->scriptEngines.at(i)->newFunction(echo);
    44             this->scriptEngines.at(i)->globalObject().setProperty("echo", echoFunction);
     37        // Add echo function so user can debug the code
     38        QScriptValue echoFunction = this->scriptEngines.at(i)->newFunction(echo);
     39        this->scriptEngines.at(i)->globalObject().setProperty("echo", echoFunction);
    4540
    46             // Add the js library for XmlEditing
    47             this->scriptEngines.at(i)->evaluate(jsxmlString);
    48         }
     41        // Add the js library for XmlEditing
     42        this->scriptEngines.at(i)->evaluate(jsxmlString);
    4943    }
     44}
     45
     46XmlCustomCode* XmlCustomCode::getInstance(){
     47
     48    if (uniqueInstance==NULL){   // allow only one instance
     49        uniqueInstance = new XmlCustomCode();
     50    }
     51
     52    return uniqueInstance;
    5053}
    5154
     
    6972
    7073            customCodeUnwinding(filesToProcess.at(i),currXmlFileString,*this->scriptEngines.at(0),begin,elapsed_secs,engineResult,
    71                     *this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled);
     74                                *this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled);
    7275        }
    7376    }
     
    99102
    100103                customCodeUnwinding(filesToProcess.at(i),currXmlFileString,*this->scriptEngines.at(0),begin,elapsed_secs,engineResult,
    101                         *this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled);
     104                                    *this->jsFunctions.at(0),*this->getXmlDataFunctions.at(0),*this->setXmlDataFunctions.at(0),backupsEnabled,verboseEnabled);
    102105            }
    103106        }
  • XmlTools2/trunk/xmlcustomcode.h

    r964 r980  
    1313#define CUSTOM_FILES_PER_THREAD 2
    1414
     15// Uses a singleton implementation (based on here: http://www.yolinux.com/TUTORIALS/C++Singleton.html)
     16// which allows each thread to keep one script engine without always create/destruct them
    1517class XmlCustomCode
    1618{
    1719public:
    18     XmlCustomCode();
     20    static XmlCustomCode* getInstance();
    1921    void executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled);
    2022private:
     23    static XmlCustomCode* uniqueInstance;
    2124    const int numThreads;
    22     static QVector<QScriptEngine*> scriptEngines; // allows each thread to keep one script engine without always create/destruct them
    23     static QVector<QScriptValue*> jsFunctions;
    24     static QVector<QScriptValue*> getXmlDataFunctions;
    25     static QVector<QScriptValue*> setXmlDataFunctions;
     25    QVector<QScriptEngine*> scriptEngines;
     26    QVector<QScriptValue*> jsFunctions;
     27    QVector<QScriptValue*> getXmlDataFunctions;
     28    QVector<QScriptValue*> setXmlDataFunctions;
     29
     30    XmlCustomCode(); // constructor is private (use getInstance)
     31    XmlCustomCode(XmlCustomCode const&);             // copy constructor is private
     32    XmlCustomCode& operator=(XmlCustomCode const&);  // assignment operator is private
    2633
    2734    void displayJsException(QScriptEngine &engine, QScriptValue &engineResult);
  • XmlTools2/trunk/xmlpatch.cpp

    r967 r980  
    220220    }
    221221
    222     this->customCodeOperation.executeCustomCode(jsString,filesToProcess,this->backupsEnabled,this->verboseEnabled);
     222    XmlCustomCode::getInstance()->executeCustomCode(jsString,filesToProcess,this->backupsEnabled,this->verboseEnabled);
    223223
    224224
  • XmlTools2/trunk/xmlpatch.h

    r964 r980  
    1717    pugi::xml_node rootNode;
    1818    bool backupsEnabled, verboseEnabled;
    19     XmlCustomCode customCodeOperation;
     19    XmlCustomCode *customCodeOperation;
    2020    QString getPatchParameterValue(const QString& line, QString parameter);
    2121    void insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard="");
Note: See TracChangeset for help on using the changeset viewer.