Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QCoreApplication>
- #include <QString>
- class Command {
- private:
- bool _isSuccess { false };
- public:
- virtual ~Command() { }
- virtual void execute() = 0;
- bool isSuccess() const { return _isSuccess; }
- protected:
- void setSuccess(const bool value) {
- _isSuccess = value;
- }
- }; // Command
- template<class TInput>
- class CommandWithInput : public Command {
- private:
- TInput _input;
- public:
- void setInput(TInput value) {
- _input = value;
- }
- TInput getInput() const {
- return _input;
- }
- }; // CommandWithInput
- template<class TInput, class TOutput>
- class CommandWithInputOtput : public Command {
- private:
- TInput _input;
- TOutput _output;
- public:
- void setInput(TInput value) {
- _input = value;
- }
- TInput getInput() const {
- return _input;
- }
- TOutput getOutput() const {\
- return _output;
- }
- protected:
- void setOutput(TOutput value) {
- _output = value;
- }
- }; // CommandWithInput
- template<class TInput, class TOutput>
- struct Converter {
- private:
- TInput _input;
- TOutput _output;
- bool _isSuccess { false };
- public:
- virtual ~Converter() { }
- virtual void convert() = 0;
- virtual bool isSuccess() const { return _isSuccess; }
- virtual void setInput(TInput value) {
- _input = value;
- }
- virtual TOutput getOutput() const {
- return _output;
- }
- protected:
- void setSuccess(const bool value) {
- _isSuccess = value;
- }
- }; // Converter
- #include <QFile>
- class OpenFileCommand : public CommandWithInputOtput<QString, QFile*> {
- public:
- typedef CommandWithInputOtput<QString, QFile*> base;
- void execute() override {
- QFile *file = getOutput();
- if (file == nullptr) {
- file = new QFile();
- }
- if (file->isOpen()) {
- file->close();
- }
- file->setFileName(getInput());
- if (file->open(QFile::ReadOnly)) {
- setOutput(file);
- setSuccess(true);
- } else {
- setSuccess(false);
- }
- }
- }; // OpenFileCommand
- class ReadAllCommand : public CommandWithInputOtput<QFile*, QString> {
- public:
- typedef CommandWithInputOtput<QFile*, QString> base;
- void execute() override {
- QFile *file = getInput();
- if (file->isOpen()) {
- setOutput(file->readAll());
- setSuccess(true);
- } else {
- setSuccess(false);
- }
- }
- }; // ReadAllCommand
- class CloseFileCommand : public CommandWithInput<QFile*> {
- public:
- typedef CommandWithInput<QFile*> base;
- void execute() override {
- QFile *file = getInput();
- if (file->isOpen()) {
- file->close();
- setSuccess(true);
- } else {
- setSuccess(false);
- }
- }
- }; // CloseFileCommand
- #include <QDir>
- #include <QTextStream>
- #include <QTextCodec>
- #include <QDebug>
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- const QString filePath = QString::fromUtf8("%1%2%3")
- .arg(QCoreApplication::applicationDirPath())
- .arg(QDir::separator())
- .arg("test.txt");
- QFile file(filePath);
- file.open(QFile::ReadWrite);
- QTextStream stream(&file);
- stream.setCodec(QTextCodec::codecForName("UTF-8"));
- stream << QString::fromUtf8("Hello world!\nFuck you C++!!!!\nCommand pattern!");
- stream.flush();
- file.close();
- OpenFileCommand::base *open = new OpenFileCommand();
- open->setInput(filePath);
- ReadAllCommand::base *readAll = new ReadAllCommand();
- readAll->setInput(open->getOutput());
- CloseFileCommand::base *close = new CloseFileCommand();
- close->setInput(open->getOutput());
- QVector<Command*> commands {
- open, readAll, close
- };
- foreach (Command *command, commands) {
- if (command != nullptr) {
- command->execute();
- if (command->isSuccess()) {
- qDebug() << "Current command success!";
- } else {
- qDebug() << "Current command failed!";
- }
- }
- }
- qDebug() << "Commands result: ";
- qDebug() << readAll->getOutput();
- QFile::remove(filePath);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement