BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Console.h
1 #pragma once
2 
3 #include <iosfwd>
4 
5 #include <Poco/SharedPtr.h>
6 
7 #include "io/Printable.h"
8 
9 namespace BeeeOn {
10 
17 public:
18  typedef Poco::SharedPtr<ConsoleSessionImpl> Ptr;
19 
20  virtual ~ConsoleSessionImpl();
21 
25  virtual std::string readUntil(const char c) = 0;
26 
30  virtual std::string readBytes(const unsigned int length) = 0;
31 
35  virtual void print(const std::string &text, bool newline = true) = 0;
36 
40  virtual bool eof();
41 
42 protected:
46  std::string iosReadUntil(std::istream &in, const char c);
47 
51  std::string iosReadBytes(std::istream &in, const unsigned int length);
52 
56  void iosPrint(std::ostream &out, const std::string &text, bool newline);
57 };
58 
62 class Console;
63 
70 class ConsoleSession : public Printable {
71 public:
72  ConsoleSession(Console &console);
73 
78  std::string readLine();
79 
84  std::string readBytes(const unsigned int length);
85 
90  void print(const std::string &text, bool newline = true) override;
91 
95  bool eof();
96 
97 private:
98  ConsoleSessionImpl::Ptr m_impl;
99  char m_eol;
100  char m_skipEol;
101  std::string m_prompt;
102 };
103 
109 class Console {
110  friend ConsoleSession;
111 public:
112  typedef Poco::SharedPtr<Console> Ptr;
113 
114  Console();
115  virtual ~Console();
116 
117  void setEol(const char c);
118  char eol() const;
119 
120  void setSkipEol(const char c);
121  char skipEol() const;
122 
123  void setPrompt(const std::string &prompt);
124  std::string prompt() const;
125 
126  virtual void close();
127 
128 protected:
129  virtual ConsoleSessionImpl::Ptr openSession() = 0;
130 
132  public:
133  std::string readUntil(const char c) override;
134  std::string readBytes(const unsigned int length) override;
135  void print(const std::string &text, bool newline = true) override;
136  bool eof() override;
137  };
138 
139  ConsoleSessionImpl::Ptr closedSession();
140 
141 private:
142  char m_eol;
143  char m_skipEol;
144  std::string m_prompt;
145 };
146 
147 }
virtual std::string readBytes(const unsigned int length)=0
void print(const std::string &text, bool newline=true) override
Definition: Console.cpp:89
void print(const std::string &text, bool newline=true) override
Definition: Console.cpp:159
std::string readLine()
Definition: Console.cpp:59
bool eof()
Definition: Console.cpp:94
virtual bool eof()
Definition: Console.cpp:15
Definition: Console.h:16
Definition: Printable.h:8
std::string iosReadBytes(std::istream &in, const unsigned int length)
Definition: Console.cpp:27
void iosPrint(std::ostream &out, const std::string &text, bool newline)
Definition: Console.cpp:42
std::string readBytes(const unsigned int length)
Definition: Console.cpp:84
std::string readUntil(const char c) override
Definition: Console.cpp:149
std::string iosReadUntil(std::istream &in, const char c)
Definition: Console.cpp:20
std::string readBytes(const unsigned int length) override
Definition: Console.cpp:154
bool eof() override
Definition: Console.cpp:163
virtual void print(const std::string &text, bool newline=true)=0
Definition: Console.h:109
Definition: Console.h:70
virtual std::string readUntil(const char c)=0