Skip to content

Commit

Permalink
Command input interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mhtvsSFrpHdE committed Aug 7, 2022
1 parent 1326e24 commit 1a1c9eb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
1 change: 1 addition & 0 deletions qpp/prefetch/Source/Global/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "..\Setting\setting.h"

QCoreApplication *Global::qCoreApplication = NULL;
LoopThread *Global::inputLoopThreadAddress = NULL;

void Global::init(int argc, char *argv[])
{
Expand Down
4 changes: 4 additions & 0 deletions qpp/prefetch/Source/Global/global.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include <QCoreApplication>

#include "..\Input\Thread\loop_thread.h"

// Static class
class Global
{
public:
static QCoreApplication *qCoreApplication;

static LoopThread *inputLoopThreadAddress;

// Any init code
static void init(int argc, char *argv[]);

Expand Down
38 changes: 34 additions & 4 deletions qpp/prefetch/Source/Input/Thread/loop_thread.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <QTextStream>

#include "loop_thread.h"
#include "..\..\Input\stdin.h"
#include "..\..\Output\stdout.h"

LoopThread::LoopThread() {}
Expand All @@ -7,10 +10,37 @@ void LoopThread::run()
{
while (true)
{
*StdOut::consoleOutput << "Std in loop thread test"
<< endl;
StdOut::consoleOutput->flush();
// Get input
auto input = StdIn::consoleInput->readLine();

sleep(3);
// Find command and run command
if (commandMap.contains(input))
{
auto target = commandMap[input];
(*target)();
}
// Command not found
else
{
*StdOut::consoleOutput << "Invalid command"
<< endl;
StdOut::consoleOutput->flush();
}
}
}

namespace ConsoleCommandFunction
{
void pause()
{
*StdOut::consoleOutput << "Trying to pause prefetch"
<< endl;
StdOut::consoleOutput->flush();
};
}

// Cool stuff: https://stackoverflow.com/questions/8157625/how-do-i-populate-values-of-a-static-qmap-in-c-qt
// Use initializer list and one of the QMap constructor
QMap<QString, void (*)()> LoopThread::commandMap(
std::map<QString, void (*)()>{
{"pause", &ConsoleCommandFunction::pause}});
7 changes: 6 additions & 1 deletion qpp/prefetch/Source/Input/Thread/loop_thread.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QThread>
#include <QMap>

class LoopThread : public QThread
{
Expand All @@ -7,4 +8,8 @@ class LoopThread : public QThread
LoopThread();

void run();
};

private:
// Convert stdin command to function pointer
static QMap<QString, void (*)()> commandMap;
};
9 changes: 6 additions & 3 deletions qpp/prefetch/Source/Input/stdin.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "stdin.h"

LoopThread *StdIn::loopThreadAddress = NULL;
#include "..\Global\global.h"

QTextStream *StdIn::consoleInput;

void StdIn::init()
{
loopThreadAddress = new LoopThread();
consoleInput = new QTextStream(stdin);
Global::inputLoopThreadAddress = new LoopThread();
}

void StdIn::start()
{
loopThreadAddress->start();
Global::inputLoopThreadAddress->start();
}
4 changes: 2 additions & 2 deletions qpp/prefetch/Source/Input/stdin.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "Thread\loop_thread.h"
#include <QTextStream>

// Static class
class StdIn
{
public:
static LoopThread *loopThreadAddress;
static QTextStream *consoleInput;

// Any init code
static void init();
Expand Down

0 comments on commit 1a1c9eb

Please sign in to comment.