aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmnStreamStdin.h
blob: a6779d5316690b793d67df3c6faad6e479115738 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//===-- MIUtilStreamStdin.h -------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

//++
// File:        MIUtilStreamStdin.h
//
// Overview:    CMICmnStreamStdin interface.
//
// Environment: Compilers:  Visual C++ 12.
//                          gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
//              Libraries:  See MIReadmetxt.
//
// Copyright:   None.
//--

#pragma once

// In-house headers:
#include "MIUtilString.h"
#include "MIUtilThreadBaseStd.h"
#include "MICmnBase.h"
#include "MIUtilSingletonBase.h"

//++ ============================================================================
// Details: MI common code class. Used to handle stream data from Stdin.
//          Singleton class using the Visitor pattern. A driver using the interface
//          provide can receive callbacks when a new line of data is received.
//          Each line is determined by a carriage return.
//          A singleton class.
// Gotchas: None.
// Authors: Illya Rudkin 10/02/2014.
// Changes: Factored out OS specific handling of reading stdin  - IOR 16/06/2014.
//--
class CMICmnStreamStdin : public CMICmnBase, public CMIUtilThreadActiveObjBase, public MI::ISingleton<CMICmnStreamStdin>
{
    // Give singleton access to private constructors
    friend MI::ISingleton<CMICmnStreamStdin>;

    // Class:
  public:
    //++
    // Description: Visitor pattern. Driver(s) use this interface to get a callback
    //              on each new line of data received from stdin.
    //--
    class IStreamStdin
    {
      public:
        virtual bool ReadLine(const CMIUtilString &vStdInBuffer, bool &vrwbYesExit) = 0;

        /* dtor */ virtual ~IStreamStdin(void){};
    };

    //++
    // Description: Specific OS stdin handling implementations are created and used by *this
    //              class. Seperates out functionality and enables handler to be set
    //              dynamically depended on the OS detected.
    //--
    class IOSStdinHandler
    {
      public:
        virtual bool InputAvailable(bool &vwbAvail) = 0;
        virtual const MIchar *ReadLine(CMIUtilString &vwErrMsg) = 0;
        virtual void InterruptReadLine(void){};

        /* dtor */ virtual ~IOSStdinHandler(void){};
    };

    // Methods:
  public:
    bool Initialize(void);
    bool Shutdown(void);
    //
    const CMIUtilString &GetPrompt(void) const;
    bool SetPrompt(const CMIUtilString &vNewPrompt);
    void SetEnablePrompt(const bool vbYes);
    bool GetEnablePrompt(void) const;
    void SetCtrlCHit(void);
    bool SetVisitor(IStreamStdin &vrVisitor);
    bool SetOSStdinHandler(IOSStdinHandler &vrHandler);
    void OnExitHandler(void);

    // Overridden:
  public:
    // From CMIUtilThreadActiveObjBase
    virtual const CMIUtilString &ThreadGetName(void) const;

    // Overridden:
  protected:
    // From CMIUtilThreadActiveObjBase
    virtual bool ThreadRun(bool &vrIsAlive);
    virtual bool
    ThreadFinish(void); // Let this thread clean up after itself

    // Methods:
  private:
    /* ctor */ CMICmnStreamStdin(void);
    /* ctor */ CMICmnStreamStdin(const CMICmnStreamStdin &);
    void operator=(const CMICmnStreamStdin &);

    bool MonitorStdin(bool &vrwbYesExit);
    const MIchar *ReadLine(CMIUtilString &vwErrMsg);
    bool
    InputAvailable(bool &vbAvail); // Bytes are available on stdin

    // Overridden:
  private:
    // From CMICmnBase
    /* dtor */ virtual ~CMICmnStreamStdin(void);

    // Attributes:
  private:
    const CMIUtilString m_constStrThisThreadname;
    IStreamStdin *m_pVisitor;
    CMIUtilString m_strPromptCurrent; // Command line prompt as shown to the user
    volatile bool m_bKeyCtrlCHit;     // True = User hit Ctrl-C, false = has not yet
    bool m_bShowPrompt;               // True = Yes prompt is shown/output to the user (stdout), false = no prompt
    bool m_bRedrawPrompt;             // True = Prompt needs to be redrawn
    IOSStdinHandler *m_pStdinReadHandler;
};