Leviathan  0.8.0.0
Leviathan game engine
Leviathan::ConsoleInput Class Reference

#include <ConsoleInput.h>

+ Inheritance diagram for Leviathan::ConsoleInput:

Public Member Functions

DLLEXPORT ~ConsoleInput ()
 
DLLEXPORT bool Init (std::function< bool(const std::string &)> callback, bool canopenconsole=true)
 Starts listening for input from std in. More...
 
DLLEXPORT void Release (bool waitquit=false)
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Static Public Member Functions

static DLLEXPORT bool IsAttachedToConsole ()
 

Protected Member Functions

virtual bool OnReceivedInput (const std::string &str)
 
bool PrepareWait ()
 
void StopWaiting ()
 Stops reading. More...
 

Additional Inherited Members

- Public Types inherited from Leviathan::ThreadSafeGeneric< MutexType >
using LockT = typename LockTypeResolver< MutexType >::LType
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

Definition at line 20 of file ConsoleInput.h.

Constructor & Destructor Documentation

◆ ~ConsoleInput()

ConsoleInput::~ConsoleInput ( )

Definition at line 14 of file ConsoleInput.cpp.

14  {
15 
16  if(!Initialized)
17  return;
18 
19  if (StdInThread.joinable()) {
20 
21  // If this is true this hasn't been closed properly //
22  LEVIATHAN_ASSERT(!ReadingInput, "ConsoleInput not closed properly");
23 
24  StdInThread.join();
25  }
26 
27  StdInUse = false;
28 }
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:92

Member Function Documentation

◆ Init()

bool ConsoleInput::Init ( std::function< bool(const std::string &)>  callback,
bool  canopenconsole = true 
)

Starts listening for input from std in.

When input is received the callback is called

Parameters
canopenconsoleIf true will open a new console for this process if one doesn't exist. Does nothing on linux
callbackCalled once a full line has been read. If returns true will stop listening

Definition at line 32 of file ConsoleInput.cpp.

34 {
35  Callback = callback;
36 
37  // Check is already in use //
38  if(StdInUse){
39 
40  DEBUG_BREAK;
41  return false;
42  }
43 
44  const bool oldvalue = StdInUse.exchange(true);
45 
46  if(oldvalue){
47 
48  // Somebody else got it //
49  return false;
50  }
51 
52  // Guaranteed to be the only object that gets here before our destructor //
53  Initialized = true;
54 
55 #ifdef _WIN32
56  // Open a console if not already open //
57  if (GetConsoleWindow() == nullptr && canopenconsole) {
58 
59  // Create a new console //
60  Logger::Get()->Info("Creating a console window for input");
61  CreatedNewConsole = true;
62  CreateConsoleWindow();
63 
64  if (GetConsoleWindow() != nullptr) {
65 
66  Logger::Get()->Info("Leviathan Console");
67  }
68  }
69 #endif // _WIN32
70 
71  if(!PrepareWait()){
72 
73  DEBUG_BREAK;
74  return false;
75  }
76 
77  ReadingInput = true;
78  StdInThread = std::thread(std::bind(&ConsoleInput::WaitForInput, this));
79 
80 
81  return true;
82 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ IsAttachedToConsole()

bool ConsoleInput::IsAttachedToConsole ( )
static
Returns
True if this process is controlled by an interactive console Can be used to quit linux servers if accidentally not started from a terminal

Definition at line 102 of file ConsoleInput.cpp.

102  {
103 #ifdef _WIN32
104 
105  return GetConsoleWindow() != nullptr;
106 
107 #else
108  if(isatty(fileno(stdin)))
109  return true;
110 
111  return false;
112 #endif
113 }

◆ OnReceivedInput()

bool ConsoleInput::OnReceivedInput ( const std::string &  str)
protectedvirtual

Called by the listening thread when a line from input has been read

Returns
True if wants to stop listening, false if listening should continue

Definition at line 115 of file ConsoleInput.cpp.

115  {
116 
117  return Callback(str);
118 }

◆ PrepareWait()

bool Leviathan::ConsoleInput::PrepareWait ( )
protected

Must be called before WaitForInput starts in a new thread

Returns
True if can start waiting, false if something is wrong

◆ Release()

void ConsoleInput::Release ( bool  waitquit = false)

Stops listening.

Parameters
waitquitIf true after this finishes the callback won't be called anymore If false callback can be called until the destructor runs, which will block until the read ends

Definition at line 84 of file ConsoleInput.cpp.

84  {
85 
86  if (ReadingInput) {
87 
88  StopWaiting();
89 
90  ReadingInput = false;
91 
92  if(waitquit && StdInThread.joinable())
93  StdInThread.join();
94  }
95 #ifdef _WIN32
96 
97  DestroyConsoleWindow();
98 
99 #endif // _WIN32
100 }
void StopWaiting()
Stops reading.

◆ StopWaiting()

void Leviathan::ConsoleInput::StopWaiting ( )
protected

Stops reading.


The documentation for this class was generated from the following files: