From a9a85210aa1318f72291576696251fc90b16ccf5 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 17 Dec 2014 19:35:03 +0100 Subject: [PATCH] started CodeModel --- mix/CodeModel.cpp | 81 ++++++++++++++++++++++++++++++++++ mix/CodeModel.h | 109 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 mix/CodeModel.cpp create mode 100644 mix/CodeModel.h diff --git a/mix/CodeModel.cpp b/mix/CodeModel.cpp new file mode 100644 index 000000000..120a81ac7 --- /dev/null +++ b/mix/CodeModel.cpp @@ -0,0 +1,81 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file CodeModel.cpp + * @author Arkadiy Paronyan arkadiy@ethdev.com + * @date 2014 + * Ethereum IDE client. + */ + +#include "CodeModel.h" + +namespace dev +{ +namespace mix +{ + +void BackgroundWorker::queueCodeChange(int _jobId, QString const& _content) +{ + m_model->runCompilationJob(_jobId, _content); +} + + +CodeModel::CodeModel(QObject* _parent) : QObject(_parent), m_backgroundWorker(this) +{ + m_backgroundWorker.moveToThread(&m_backgroundThread); + + connect(this, &CodeModel::compilationComplete, this, &CodeModel::onCompilationComplete, Qt::QueuedConnection); + connect(this, &CodeModel::scheduleCompilationJob, &m_backgroundWorker, &BackgroundWorker::queueCodeChange, Qt::QueuedConnection); + m_backgroundThread.start(); +} + +CodeModel::~CodeModel() +{ + stop(); + disconnect(this); +} + +void CodeModel::stop() +{ + ///@todo: cancel bg job + m_backgroundThread.exit(); + m_backgroundThread.wait(); +} + +void CodeModel::registerCodeChange(const QString &_code) +{ + // launch the background thread + m_backgroundJobId++; + emit scheduleCompilationJob(m_backgroundJobId, _code); +} + +void CodeModel::onCompilationComplete(std::shared_ptr _compilationResult) +{ + m_result.swap(_compilationResult); +} + + +void CodeModel::runCompilationJob(int _jobId, QString const& _content) +{ + if (_jobId != m_backgroundJobId) + return; //obsolete job + + +} + + +} +} diff --git a/mix/CodeModel.h b/mix/CodeModel.h new file mode 100644 index 000000000..1667f19a4 --- /dev/null +++ b/mix/CodeModel.h @@ -0,0 +1,109 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file CodeModel.h + * @author Arkadiy Paronyan arkadiy@ethdev.com + * @date 2014 + * Ethereum IDE client. + */ + +#pragma once + +#include +#include +#include + +namespace dev +{ +namespace mix +{ + +class CodeModel; +class QContractDefinition; + +class BackgroundWorker: public QObject +{ + Q_OBJECT + +public: + BackgroundWorker(CodeModel* _model): QObject(), m_model(_model) {} + +public slots: + void queueCodeChange(int _jobId, QString const& _content); +private: + CodeModel* m_model; +}; + + +class CompilationResult : public QObject +{ + Q_OBJECT + Q_PROPERTY(QContractDefinition const* contract READ contract) + +public: + QContractDefinition const* contract() { return m_contract; } + +private: + bool m_successfull; + QContractDefinition* m_contract; + QString m_hexCode; + QString m_compilerMessage; ///< @todo: use some structure here + dev::bytes m_bytes; + ///@todo syntax highlighting, etc +}; + +class CodeModel : public QObject +{ + enum Status + { + Idle, + Compiling, + }; + + Q_OBJECT + +public: + CodeModel(QObject* _parent); + ~CodeModel(); + + CompilationResult* getLastCompilationResult(); + +signals: + void statusChanged(Status _from, Status _to); + void compilationComplete(std::shared_ptr _compilationResult); + void scheduleCompilationJob(int _jobId, QString const& _content); + +public slots: + void registerCodeChange(QString const& _code); + +private slots: + void onCompilationComplete(std::shared_ptr _compilationResult); + +private: + void runCompilationJob(int _jobId, QString const& _content); + void stop(); + + std::shared_ptr m_result; + QThread m_backgroundThread; + BackgroundWorker m_backgroundWorker; + int m_backgroundJobId = 0; //protects from starting obsolete compilation job + + friend class BackgroundWorker; +}; + +} + +}