Make CSConverter a real singleton.

1.3
Ariya Hidayat 2011-08-21 00:11:11 -07:00
parent 8af4fa714c
commit d3f10a8e18
3 changed files with 16 additions and 11 deletions

View File

@ -29,14 +29,24 @@
#include "csconverter.h"
#include <QCoreApplication>
#include <QFile>
#include <QWebFrame>
#include "registry.h"
// public:
CSConverter::CSConverter(QObject *parent)
: QObject(parent)
static CSConverter *csconverter_instance = 0;
CSConverter *CSConverter::instance()
{
if (!csconverter_instance)
csconverter_instance = new CSConverter();
return csconverter_instance;
}
CSConverter::CSConverter()
: QObject(QCoreApplication::instance())
{
QFile file(":/coffee-script.js");
if (!file.open(QFile::ReadOnly)) {

View File

@ -36,10 +36,11 @@
class CSConverter: public QObject
{
public:
CSConverter(QObject *parent = 0);
static CSConverter *instance();
QVariant convert(const QString &script);
private:
CSConverter();
QWebPage m_webPage;
};

View File

@ -72,13 +72,7 @@ void Utils::messageHandler(QtMsgType type, const char *msg)
QVariant Utils::coffee2js(const QString &script)
{
// We need only one instance of the CSConverter to survive for the whole life of PhantomJS
static CSConverter *coffeeScriptConverter = NULL;
if ( !coffeeScriptConverter ) {
coffeeScriptConverter = new CSConverter();
}
return coffeeScriptConverter->convert(script);
return CSConverter::instance()->convert(script);
}
bool Utils::injectJsInFrame(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)