Leviathan  0.8.0.0
Leviathan game engine
ConsoleInput.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2016 Henri Hyyryläinen
3 #pragma once
4 // ------------------------------------ //
5 #include "Define.h"
6 // ------------------------------------ //
7 #include "../Common/ThreadSafe.h"
8 
9 #ifdef _WIN32
10 #include "WindowsInclude.h"
11 #endif
12 
13 #include <string>
14 #include <functional>
15 #include <thread>
16 #include <atomic>
17 
18 namespace Leviathan {
19 
20 class ConsoleInput : public ThreadSafe {
21 public:
22 
24 
32  DLLEXPORT bool Init(std::function<bool (const std::string&)> callback,
33  bool canopenconsole = true);
34 
39  DLLEXPORT void Release(bool waitquit = false);
40 
43  DLLEXPORT static bool IsAttachedToConsole();
44 
45 protected:
46 
49  virtual bool OnReceivedInput(const std::string &str);
50 
53  bool PrepareWait();
54 
56  void StopWaiting();
57 
58 private:
60  void WaitForInput();
61 
62 private:
63 
66  bool Initialized = false;
67 
69  std::function<bool (const std::string&)> Callback;
70 
72  std::thread StdInThread;
74  std::atomic<bool> ReadingInput { false };
75 
76 
78  static std::atomic<bool> StdInUse;
79 
80  // Variables used from StdInThread //
82  std::string InputBuffer;
83 
85  std::string InputWorkBuffer;
86 
87 #ifdef _WIN32
88 
90  bool CreatedNewConsole = false;
91 
92  HANDLE ConsoleInputFile = nullptr;
93 
95  void CreateConsoleWindow();
97  void DestroyConsoleWindow();
98 
99 #elif defined(__unix__)
100 
102  int ReadCancelPipe[2];
103 
104 #endif //_WIN32
105 };
106 }
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
DLLEXPORT bool Init(std::function< bool(const std::string &)> callback, bool canopenconsole=true)
Starts listening for input from std in.
DLLEXPORT void Release(bool waitquit=false)
virtual bool OnReceivedInput(const std::string &str)
void StopWaiting()
Stops reading.
#define DLLEXPORT
Definition: Include.h:84
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
static DLLEXPORT bool IsAttachedToConsole()