- Timestamp:
- Mar 6, 2014, 5:40:35 PM (11 years ago)
- Location:
- XmlTools2/trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
XmlTools2/trunk/util.cpp
r958 r980 8 8 QString AppExecutable="./"+AppName; // Mac OS needs unix like executing 9 9 #endif 10 QString AppVersion="2.0 a";10 QString AppVersion="2.0b"; 11 11 } 12 12 -
XmlTools2/trunk/xmlcustomcode.cpp
r967 r980 1 1 #include "xmlcustomcode.h" 2 2 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; 3 XmlCustomCode* XmlCustomCode::uniqueInstance = NULL; 8 4 9 5 QScriptValue echo(QScriptContext *context, QScriptEngine*) … … 16 12 XmlCustomCode::XmlCustomCode(): numThreads(omp_get_num_procs()*2) 17 13 { 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); 24 19 25 26 20 QString jsxmlString; 21 QFile jsxmlfile(":/resources/libs/jsxml.js"); 27 22 28 23 jsxmlfile.open(QFile::ReadOnly | QFile::Text); 29 24 30 25 jsxmlString=QTextStream(&jsxmlfile).readAll(); 31 26 32 33 34 27 for(int i=0; i<this->numThreads; i++){ 28 this->scriptEngines.append(new QScriptEngine()); 29 this->jsFunctions.append(new QScriptValue()); 35 30 36 37 38 39 40 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; })"))); 41 36 42 43 44 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); 45 40 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); 49 43 } 44 } 45 46 XmlCustomCode* XmlCustomCode::getInstance(){ 47 48 if (uniqueInstance==NULL){ // allow only one instance 49 uniqueInstance = new XmlCustomCode(); 50 } 51 52 return uniqueInstance; 50 53 } 51 54 … … 69 72 70 73 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); 72 75 } 73 76 } … … 99 102 100 103 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); 102 105 } 103 106 } -
XmlTools2/trunk/xmlcustomcode.h
r964 r980 13 13 #define CUSTOM_FILES_PER_THREAD 2 14 14 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 15 17 class XmlCustomCode 16 18 { 17 19 public: 18 XmlCustomCode();20 static XmlCustomCode* getInstance(); 19 21 void executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled); 20 22 private: 23 static XmlCustomCode* uniqueInstance; 21 24 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 26 33 27 34 void displayJsException(QScriptEngine &engine, QScriptValue &engineResult); -
XmlTools2/trunk/xmlpatch.cpp
r967 r980 220 220 } 221 221 222 this->customCodeOperation.executeCustomCode(jsString,filesToProcess,this->backupsEnabled,this->verboseEnabled);222 XmlCustomCode::getInstance()->executeCustomCode(jsString,filesToProcess,this->backupsEnabled,this->verboseEnabled); 223 223 224 224 -
XmlTools2/trunk/xmlpatch.h
r964 r980 17 17 pugi::xml_node rootNode; 18 18 bool backupsEnabled, verboseEnabled; 19 XmlCustomCode customCodeOperation;19 XmlCustomCode *customCodeOperation; 20 20 QString getPatchParameterValue(const QString& line, QString parameter); 21 21 void insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard="");
Note:
See TracChangeset
for help on using the changeset viewer.