]> git.tdb.fi Git - libs/core.git/blob - source/io/console.h
Use size_t for sizes in I/O classes
[libs/core.git] / source / io / console.h
1 #ifndef MSP_IO_CONSOLE_H_
2 #define MSP_IO_CONSOLE_H_
3
4 #include "eventobject.h"
5 #include "handle.h"
6
7 namespace Msp {
8 namespace IO {
9
10 /**
11 Provides access to standard input, output and error streams.  This class can't
12 be instantiated directly - use one of the cin, cout and cerr references
13 instead.
14 */
15 class Console: public EventObject
16 {
17 public:
18         enum Stream
19         {
20                 CIN = 0,
21                 COUT = 1,
22                 CERR = 2
23         };
24
25 private:
26         Stream stream;
27         Handle handle;
28
29         Console(Stream);
30         void platform_init();
31 public:
32         ~Console();
33
34         virtual void set_block(bool);
35         virtual void set_inherit(bool);
36         
37         /** If local echo is enabled, characters will appear on the console as they
38         are typed.  Can only be used on an input Console.  */
39         void set_local_echo(bool);
40
41         /** If line buffering is enabled, input will only be available when a
42         newline is encountered.  On some systems, this may also enable line editing.
43         Can only be used on an input Console.
44         */
45         void set_line_buffer(bool);
46
47         /** Retrieves the size of the Console.  Can only be used on an output
48         Console. */
49         void get_size(unsigned &rows, unsigned &cols);
50
51         /** Redirects input from or output to another I/O object.  This performs a
52         system-level redirect.  Closing the other object won't affect the
53         redirection. */
54         void redirect(Base &);
55
56 protected:
57         virtual std::size_t do_write(const char *, std::size_t);
58         virtual std::size_t do_read(char *, std::size_t);
59
60 public:
61         virtual const Handle &get_handle(Mode);
62         virtual const Handle &get_event_handle() { return handle; }
63
64         static Console &instance(Stream);
65 };
66
67 extern Console &cin;
68 extern Console &cout;
69 extern Console &cerr;
70
71 } // namespace IO
72 } // namespace Msp
73
74 #endif