Advertisement
Guest User

Untitled

a guest
Dec 4th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QCoreApplication>
  2.  
  3. #include <QString>
  4.  
  5. class Command {
  6. private:
  7.     bool _isSuccess { false };
  8. public:
  9.     virtual ~Command() { }
  10.     virtual void execute() = 0;
  11.     bool isSuccess() const { return _isSuccess; }
  12. protected:
  13.     void setSuccess(const bool value) {
  14.         _isSuccess = value;
  15.     }
  16. }; // Command
  17.  
  18. template<class TInput>
  19. class CommandWithInput : public Command {
  20. private:
  21.     TInput _input;
  22. public:
  23.     void setInput(TInput value) {
  24.         _input = value;
  25.     }
  26.     TInput getInput() const {
  27.         return _input;
  28.     }
  29. }; // CommandWithInput
  30.  
  31. template<class TInput, class TOutput>
  32. class CommandWithInputOtput : public Command {
  33. private:
  34.     TInput _input;
  35.     TOutput _output;
  36. public:
  37.     void setInput(TInput value) {
  38.         _input = value;
  39.     }
  40.     TInput getInput() const {
  41.         return _input;
  42.     }
  43.     TOutput getOutput() const {\
  44.         return _output;
  45.     }
  46. protected:
  47.     void setOutput(TOutput value) {
  48.         _output = value;
  49.     }
  50. }; // CommandWithInput
  51.  
  52. template<class TInput, class TOutput>
  53. struct Converter {
  54. private:
  55.     TInput _input;
  56.     TOutput _output;
  57.     bool _isSuccess { false };
  58. public:
  59.     virtual ~Converter() { }
  60.     virtual void convert() = 0;
  61.     virtual bool isSuccess() const { return _isSuccess; }
  62.     virtual void setInput(TInput value) {
  63.         _input = value;
  64.     }
  65.     virtual TOutput getOutput() const {
  66.         return _output;
  67.     }
  68. protected:
  69.     void setSuccess(const bool value) {
  70.         _isSuccess = value;
  71.     }
  72. }; // Converter
  73.  
  74. #include <QFile>
  75.  
  76. class OpenFileCommand : public CommandWithInputOtput<QString, QFile*> {
  77. public:
  78.     typedef CommandWithInputOtput<QString, QFile*> base;
  79.     void execute() override {
  80.         QFile *file = getOutput();
  81.         if (file == nullptr) {
  82.             file = new QFile();
  83.         }
  84.         if (file->isOpen()) {
  85.             file->close();
  86.         }
  87.         file->setFileName(getInput());
  88.         if (file->open(QFile::ReadOnly)) {
  89.             setOutput(file);
  90.             setSuccess(true);
  91.         } else {
  92.             setSuccess(false);
  93.         }
  94.     }
  95. }; // OpenFileCommand
  96.  
  97. class ReadAllCommand : public CommandWithInputOtput<QFile*, QString> {
  98. public:
  99.     typedef CommandWithInputOtput<QFile*, QString> base;
  100.     void execute() override {
  101.         QFile *file = getInput();
  102.         if (file->isOpen()) {
  103.             setOutput(file->readAll());
  104.             setSuccess(true);
  105.         } else {
  106.             setSuccess(false);
  107.         }
  108.     }
  109. }; // ReadAllCommand
  110.  
  111. class CloseFileCommand : public CommandWithInput<QFile*> {
  112. public:
  113.     typedef CommandWithInput<QFile*> base;
  114.     void execute() override {
  115.         QFile *file = getInput();
  116.         if (file->isOpen()) {
  117.             file->close();
  118.             setSuccess(true);
  119.         } else {
  120.             setSuccess(false);
  121.         }
  122.     }
  123. }; // CloseFileCommand
  124.  
  125. #include <QDir>
  126. #include <QTextStream>
  127. #include <QTextCodec>
  128. #include <QDebug>
  129.  
  130. int main(int argc, char *argv[])
  131. {
  132.     QCoreApplication a(argc, argv);
  133.  
  134.     const QString filePath = QString::fromUtf8("%1%2%3")
  135.             .arg(QCoreApplication::applicationDirPath())
  136.             .arg(QDir::separator())
  137.             .arg("test.txt");
  138.  
  139.     QFile file(filePath);
  140.     file.open(QFile::ReadWrite);
  141.     QTextStream stream(&file);
  142.     stream.setCodec(QTextCodec::codecForName("UTF-8"));
  143.     stream << QString::fromUtf8("Hello world!\nFuck you C++!!!!\nCommand pattern!");
  144.     stream.flush();
  145.     file.close();
  146.  
  147.     OpenFileCommand::base *open = new OpenFileCommand();
  148.     open->setInput(filePath);
  149.     ReadAllCommand::base *readAll = new ReadAllCommand();
  150.     readAll->setInput(open->getOutput());
  151.     CloseFileCommand::base *close = new CloseFileCommand();
  152.     close->setInput(open->getOutput());
  153.  
  154.     QVector<Command*> commands {
  155.         open, readAll, close
  156.     };
  157.  
  158.     foreach (Command *command, commands) {
  159.         if (command != nullptr) {
  160.             command->execute();
  161.             if (command->isSuccess()) {
  162.                 qDebug() << "Current command success!";
  163.             } else {
  164.                 qDebug() << "Current command failed!";
  165.             }
  166.         }
  167.     }
  168.  
  169.     qDebug() << "Commands result: ";
  170.     qDebug() << readAll->getOutput();
  171.  
  172.     QFile::remove(filePath);
  173.  
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement