OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ProximLineReader Class Reference

#include <ProximLineReader.hh>

Inheritance diagram for ProximLineReader:
Inheritance graph
Collaboration diagram for ProximLineReader:
Collaboration graph

Public Member Functions

 ProximLineReader ()
 
virtual ~ProximLineReader ()
 
virtual void initialize (std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
 
virtual std::string readLine (std::string prompt="")
 
virtual char charQuestion (std::string question, std::string allowedChars, bool caseSensitive=false, char defaultAnswer='\0')
 
void input (std::string command)
 
virtual std::ostream & outputStream ()
 
void output (std::string text)
 
virtual void setInputHistoryLog (const std::string &historyFilename)
 
std::string inputHistoryFilename () const
 
- Public Member Functions inherited from LineReader
 LineReader (std::istream &iStream=std::cin, std::ostream &oStream=std::cout)
 
virtual ~LineReader ()
 
bool confirmation (std::string question, char defaultAnswer='n', char yesChar='y', char noChar='n')
 
virtual void setSaveInputHistoryToFile (bool flag)
 
virtual bool saveInputHistoryToFile () const
 
virtual void setInputHistoryLength (std::size_t length)
 
virtual size_t inputHistoryMaxLength () const
 
virtual std::size_t inputsInHistory () const
 
virtual std::string inputHistoryEntry (std::size_t age) const
 

Private Member Functions

 ProximLineReader (const ProximLineReader &)
 Copying not allowed.
 
ProximLineReaderoperator= (const ProximLineReader &)
 Assignment not allowed.
 

Private Attributes

wxMutex * mutex_
 
wxCondition * input_
 Condition, which is signaled when user input is received from the GUI.
 
std::string prompt_
 Input prompt.
 
std::queue< std::string > inputQueue_
 Input queue.
 
wxEvtHandler * gui_
 GUI to send the events to.
 
std::ostream * outputStream_
 Output stream which converts the output to SimulatorEvents.
 
std::string historyFile_
 Name of the input history file.
 

Static Private Attributes

static const std::string DEFAULT_LOG_FILE_NAME = ".proximHistory"
 

Additional Inherited Members

- Protected Member Functions inherited from LineReader
void setInitialized ()
 
bool initialized () const
 
void putInInputHistory (const std::string &inputLine)
 

Detailed Description

Handles user input passing between the simulator and GUI threads in Proxim.

This class is designed to act as an interface between two threads: A GUI thread handling the user interface, and a worker thread running the simulator engine. The worker thread waits for the user input by listening to the 'input_' condition. When the line reader receives input from the GUI thread, the worker thread is signaled about the input by setting the input_ condition. If the worker thread is busy and not listening to the input_ condition signal when the GUI thread signals about user input, the signal is lost and the user input is ignored. Therefore the user input should be disabled in the GUI when the worker thread is busy. ProximLineReader requests user input and sends simulator engine output to the GUI thread using SimulatorEvents and the standard wxWidgets event handling system.

Definition at line 60 of file ProximLineReader.hh.

Constructor & Destructor Documentation

◆ ProximLineReader() [1/2]

ProximLineReader::ProximLineReader ( )

The Constructor.

Definition at line 48 of file ProximLineReader.cc.

48 :
49 LineReader(),
50 mutex_(new wxMutex()),
51 input_(new wxCondition(*mutex_)),
53
55}
wxCondition * input_
Condition, which is signaled when user input is received from the GUI.
std::ostream * outputStream_
Output stream which converts the output to SimulatorEvents.
virtual void setInputHistoryLog(const std::string &historyFilename)
static const std::string DEFAULT_LOG_FILE_NAME

References DEFAULT_LOG_FILE_NAME, and setInputHistoryLog().

Here is the call graph for this function:

◆ ~ProximLineReader()

ProximLineReader::~ProximLineReader ( )
virtual

The Destructor.

Definition at line 61 of file ProximLineReader.cc.

61 {
62 mutex_->Unlock();
63 delete mutex_;
64 delete input_;
65}

References input_, and mutex_.

◆ ProximLineReader() [2/2]

ProximLineReader::ProximLineReader ( const ProximLineReader )
private

Copying not allowed.

Member Function Documentation

◆ charQuestion()

char ProximLineReader::charQuestion ( std::string  question,
std::string  allowedChars,
bool  caseSensitive = false,
char  defaultAnswer = '\0' 
)
virtual

Requests answer to a char question from the GUI thread.

Parameters
questionQuestion to ask from the user.
allowedCharsAllowed answers.
caseSensitiveTrue, if the answer characters are case sensitive.
defaultAnswerDefault answer to the question.
Returns
Answer to the char question.

Implements LineReader.

Definition at line 148 of file ProximLineReader.cc.

150 {
151 if (!initialized()) {
152 throw ObjectNotInitialized(__FILE__, __LINE__);
153 }
154
155
156 // Char question answer is requested from the GUI thread until
157 // a valid answer is received.
158 std::string answer;
159 do {
160 if (inputQueue_.empty()) {
161 output(question + " [" + allowedChars + "]? ");
162 input_->Wait();
163 }
164 answer = inputQueue_.front();
165 inputQueue_.pop();
166
167 output(answer + "\n");
168
169 if (answer.length() != 1) {
170 continue;
171 }
172 } while (allowedChars.find(answer, 0) == std::string::npos);
173
174 return answer[0];
175}
bool initialized() const
std::queue< std::string > inputQueue_
Input queue.
void output(std::string text)

References LineReader::initialized(), input_, inputQueue_, and output().

Here is the call graph for this function:

◆ initialize()

void ProximLineReader::initialize ( std::string  defPrompt = "",
FILE *  in = stdin,
FILE *  out = stdout,
FILE *  err = stderr 
)
virtual

Initializes the linereader.

The FILE* pointer parameters specified in the base class are not used.

Parameters
defPromptDefault prompt.

Implements LineReader.

Definition at line 76 of file ProximLineReader.cc.

77 {
78
79 gui_ = wxGetApp().GetTopWindow();
80 prompt_ = defPrompt;
82
83 // Set the linereader output stream as OSAL output stream.
85}
void setInitialized()
static void setOutputStream(std::ostream &newOutputStream)
virtual std::ostream & outputStream()
std::string prompt_
Input prompt.
wxEvtHandler * gui_
GUI to send the events to.

References gui_, outputStream(), prompt_, LineReader::setInitialized(), and OperationGlobals::setOutputStream().

Referenced by ProximSimulationThread::initialize().

Here is the call graph for this function:

◆ input()

void ProximLineReader::input ( std::string  input)

This function is called by the GUI thread to set the user input.

The input_ condtion is signaled about the user input.

Parameters
commandThe user input.

Definition at line 131 of file ProximLineReader.cc.

131 {
132 inputQueue_.push(input);
133 // Signal simulation about the input.
134 input_->Signal();
135}
void input(std::string command)

References input(), input_, and inputQueue_.

Referenced by ProximExecuteFileCmd::Do(), ProximQuitCmd::Do(), input(), ProximCmdHistoryWindow::onCommandDClick(), AddBreakpointDialog::onOK(), AddWatchDialog::onOK(), BreakpointPropertiesDialog::onOK(), SimulatorSettingsDialog::onOK(), WatchPropertiesDialog::onOK(), readLine(), and ConsoleWindow::textEntered().

Here is the call graph for this function:

◆ inputHistoryFilename()

std::string ProximLineReader::inputHistoryFilename ( ) const

Returns input history log file name.

Returns
Name of the input history log file.

Definition at line 310 of file ProximLineReader.cc.

310 {
311 return historyFile_;
312}
std::string historyFile_
Name of the input history file.

References historyFile_.

Referenced by SimulatorSettingsDialog::onOK(), and SimulatorSettingsDialog::TransferDataToWindow().

◆ operator=()

ProximLineReader & ProximLineReader::operator= ( const ProximLineReader )
private

Assignment not allowed.

◆ output()

void ProximLineReader::output ( std::string  text)

Definition at line 194 of file ProximLineReader.cc.

194 {
196 wxPostEvent(gui_, event);
197}
static const wxEventType EVT_SIMULATOR_OUTPUT
Textual output event from simulator interpreter.

References SimulatorEvent::EVT_SIMULATOR_OUTPUT, gui_, and output().

Referenced by charQuestion(), ProximLROutputBuffer::flushBuffer(), output(), ProximLROutputBuffer::overflow(), and readLine().

Here is the call graph for this function:

◆ outputStream()

std::ostream & ProximLineReader::outputStream ( )
virtual

Returns an outputstream, which the worker thread can use to write text to the GUI.

Returns
Reference to an output stream, which can be utilized to write text to the GUI.

Reimplemented from LineReader.

Definition at line 185 of file ProximLineReader.cc.

185 {
186 return *outputStream_;
187}

References outputStream_.

Referenced by initialize().

◆ readLine()

std::string ProximLineReader::readLine ( std::string  prompt = "")
virtual

Waits for input from the GUI thread.

The input_ condition timeout is set as 100ms. After the timeout an empty input is returned. This is done because the worker thread needs to check its stop and delete requests at regular intervals.

Parameters
promptPrompt used for requesting the user input.

Implements LineReader.

Definition at line 98 of file ProximLineReader.cc.

98 {
99 if (!initialized()) {
100 std::string method = "ProximLineReader::readLine";
101 throw ObjectNotInitialized(__FILE__, __LINE__, method);
102 }
103
104 if (prompt == "") {
105 prompt = prompt_;
106 }
107
108 if (inputQueue_.empty()) {
109 output(prompt);
110 mutex_->TryLock();
111 input_->Wait();
112 }
113 std::string input = inputQueue_.front();
114 inputQueue_.pop();
115
116 output(input + "\n");
117
118 // Append read input to the command history.
120 return input;
121}
void putInInputHistory(const std::string &inputLine)

References LineReader::initialized(), input(), input_, inputQueue_, mutex_, output(), prompt_, and LineReader::putInInputHistory().

Referenced by ProximSimulationThread::Entry().

Here is the call graph for this function:

◆ setInputHistoryLog()

void ProximLineReader::setInputHistoryLog ( const std::string &  historyFileName)
virtual

Sets the name of the input history log file.

Parameters
hisoryFileNameName of the input history log file.

Reimplemented from LineReader.

Definition at line 299 of file ProximLineReader.cc.

299 {
300 historyFile_ = historyFileName;
301 LineReader::setInputHistoryLog(historyFileName);
302}
virtual void setInputHistoryLog(const std::string &historyFilename)

References historyFile_, and LineReader::setInputHistoryLog().

Referenced by ProximLineReader().

Here is the call graph for this function:

Member Data Documentation

◆ DEFAULT_LOG_FILE_NAME

const std::string ProximLineReader::DEFAULT_LOG_FILE_NAME = ".proximHistory"
staticprivate

Definition at line 106 of file ProximLineReader.hh.

Referenced by ProximLineReader().

◆ gui_

wxEvtHandler* ProximLineReader::gui_
private

GUI to send the events to.

Definition at line 99 of file ProximLineReader.hh.

Referenced by initialize(), and output().

◆ historyFile_

std::string ProximLineReader::historyFile_
private

Name of the input history file.

Definition at line 104 of file ProximLineReader.hh.

Referenced by inputHistoryFilename(), and setInputHistoryLog().

◆ input_

wxCondition* ProximLineReader::input_
private

Condition, which is signaled when user input is received from the GUI.

Definition at line 93 of file ProximLineReader.hh.

Referenced by charQuestion(), input(), readLine(), and ~ProximLineReader().

◆ inputQueue_

std::queue<std::string> ProximLineReader::inputQueue_
private

Input queue.

Definition at line 97 of file ProximLineReader.hh.

Referenced by charQuestion(), input(), and readLine().

◆ mutex_

wxMutex* ProximLineReader::mutex_
private

Definition at line 91 of file ProximLineReader.hh.

Referenced by readLine(), and ~ProximLineReader().

◆ outputStream_

std::ostream* ProximLineReader::outputStream_
private

Output stream which converts the output to SimulatorEvents.

Definition at line 101 of file ProximLineReader.hh.

Referenced by outputStream().

◆ prompt_

std::string ProximLineReader::prompt_
private

Input prompt.

Definition at line 95 of file ProximLineReader.hh.

Referenced by initialize(), and readLine().


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