src
examples
console_streams.cpp
Go to the documentation of this file.
1
9
/*****************************************************************************
10
** Includes
11
*****************************************************************************/
12
13
#include <iostream>
14
#include <string>
15
#include <
ecl/devices/console.hpp
>
16
#include <
ecl/streams/text_stream.hpp
>
17
#include <
ecl/streams/console_streams.hpp
>
18
#include <
ecl/streams/manipulators/end_of_line.hpp
>
19
20
/*****************************************************************************
21
** Using
22
*****************************************************************************/
23
24
using
std::string;
25
using
ecl::OConsole
;
26
using
ecl::IConsole
;
27
using
ecl::endl
;
28
using
ecl::TextStream
;
29
using
ecl::IConsoleStream
;
30
using
ecl::OConsoleStream
;
31
32
/*****************************************************************************
33
** Main
34
*****************************************************************************/
35
36
int
main
(
int
argc,
char
** argv) {
37
38
bool
test_istreams =
false
;
39
40
std::cout <<
std::endl
;
41
std::cout <<
"***********************************************************"
<<
std::endl
;
42
std::cout <<
" OConsole Stream"
<<
std::endl
;
43
std::cout <<
"***********************************************************"
<<
std::endl
;
44
std::cout <<
std::endl
;
45
46
std::cout <<
"These can't really be tested very easily by google tests, so"
<<
std::endl
;
47
std::cout <<
"it's just provided here as a demo, but functionally is a"
<<
std::endl
;
48
std::cout <<
"unit test (requiring human approval)."
<<
std::endl
;
49
std::cout <<
std::endl
;
50
51
OConsoleStream
ostream;
52
53
std::cout <<
"Streaming char."
<<
std::endl
;
54
ostream <<
'c'
<<
'\n'
;
55
std::cout <<
"Streaming char string."
<<
std::endl
;
56
ostream <<
"Dude\n"
;
57
std::cout <<
"Streaming string."
<<
std::endl
;
58
string
dude(
"dude_string\n"
);
59
ostream << dude;
60
std::cout <<
"Streaming integers."
<<
std::endl
;
61
short
si = 1;
62
ostream << si <<
'\n'
;
63
int
i = 2;
64
ostream << i <<
'\n'
;
65
long
l = 3;
66
ostream << l <<
'\n'
;
67
long
long
ll = 4;
68
ostream << ll <<
'\n'
;
69
unsigned
short
us = 5;
70
ostream << us <<
'\n'
;
71
unsigned
int
ui = 6;
72
ostream << ui <<
'\n'
;
73
unsigned
long
ul = 77;
74
ostream << ul <<
'\n'
;
75
unsigned
long
long
ull = 8888;
76
ostream << ull <<
'\n'
;
77
std::cout <<
"Streaming temporary integers."
<<
std::endl
;
78
ostream << 77 <<
'\n'
;
79
std::cout <<
"Streaming a boolean."
<<
std::endl
;
80
bool
test =
true
;
81
ostream << test <<
'\n'
;
82
ostream <<
false
<<
'\n'
;
83
std::cout <<
"Streaming floating point values."
<<
std::endl
;
84
float
f
= 32.1;
85
double
d
= -33.3;
86
ostream <<
f
<<
'\n'
;
87
ostream <<
d
<<
'\n'
;
88
89
std::cout <<
"Flushing"
<<
std::endl
;
90
ostream.flush();
91
92
std::cout <<
std::endl
;
93
std::cout <<
"***********************************************************"
<<
std::endl
;
94
std::cout <<
" OConsole Manipulators"
<<
std::endl
;
95
std::cout <<
"***********************************************************"
<<
std::endl
;
96
std::cout <<
std::endl
;
97
98
ostream <<
"dude with an ecl::endl"
<<
endl
;
99
100
if
( test_istreams ) {
101
std::cout <<
std::endl
;
102
std::cout <<
"***********************************************************"
<<
std::endl
;
103
std::cout <<
" IConsole Stream"
<<
std::endl
;
104
std::cout <<
"***********************************************************"
<<
std::endl
;
105
std::cout <<
std::endl
;
106
107
char
c;
108
string
response;
109
IConsoleStream
istream;
110
// TextStream<IConsole> istream;
111
112
std::cout <<
"Enter a char."
<<
std::endl
;
113
istream >> c;
114
std::cout <<
"Char read: "
<< c <<
std::endl
;
115
std::cout <<
"Enter a word."
<<
std::endl
;
116
istream >> response;
117
std::cout <<
"Word read: "
<< response <<
std::endl
;
118
std::cout <<
"Enter a short: "
<<
std::endl
;
119
istream >> si;
120
if
( istream.fail() ) {
121
std::cout <<
"Short read failed: "
<< istream.errorStatus().what() <<
std::endl
;
122
}
else
{
123
std::cout <<
"Short read: "
<< si <<
std::endl
;
124
}
125
std::cout <<
"Enter a bool: "
<<
std::endl
;
126
istream >> test;
127
if
( istream.fail() ) {
128
std::cout <<
"Bool read failed: "
<< istream.errorStatus().what() <<
std::endl
;
129
}
else
{
130
std::cout <<
"Bool read: "
;
131
if
( test ) {
132
std::cout <<
"true"
<<
std::endl
;
133
}
else
{
134
std::cout <<
"false"
<<
std::endl
;
135
}
136
}
137
std::cout <<
"Enter a double:"
<<
std::endl
;
138
istream >>
d
;
139
if
( istream.fail() ) {
140
std::cout <<
"Double read failed: "
<< istream.errorStatus().what() <<
std::endl
;
141
}
else
{
142
std::cout <<
"Double read: "
<<
d
<<
std::endl
;
143
}
144
}
145
std::cout <<
std::endl
;
146
std::cout <<
"***********************************************************"
<<
std::endl
;
147
std::cout <<
" Passed"
<<
std::endl
;
148
std::cout <<
"***********************************************************"
<<
std::endl
;
149
std::cout <<
std::endl
;
150
151
return
0;
152
}
console_streams.hpp
Convenience handles for console textstreams.
ecl::demos::f
void f()
Definition:
log_streams.cpp:55
ecl::OConsoleStream
TextStream< OConsole > OConsoleStream
Streams to standard output.
Definition:
console_streams.hpp:64
ecl::TextStream
A text streaming interface.
Definition:
text_stream.hpp:100
main
int main(int argc, char **argv)
Definition:
console_streams.cpp:36
ecl::OConsole
ecl::endl
EndOfLine endl
Definition:
manipulators.cpp:30
console.hpp
ecl::IConsoleStream
TextStream< IConsole > IConsoleStream
Streams to standard input.
Definition:
console_streams.hpp:80
d
void d()
end_of_line.hpp
The familiar end of line manipulator, but as a c++ class.
ecl::IConsole
text_stream.hpp
Convenience header for various text streams.
ecl_streams
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:48