This repository was archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathiostream.hpp
177 lines (148 loc) · 4.23 KB
/
iostream.hpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef ECL_IOSTREAM_HPP
#define ECL_IOSTREAM_HPP
#include "istream.hpp"
#include "ostream.hpp"
#include "console_driver.hpp"
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
// ecl == [E]mbedded [C]++ [L]ibrary
namespace ecl
{
template<class IO_device>
class iostream
{
public:
// Provides type information of the underlaying device
using device_type = IO_device;
// Initializes a stream with given device
// NOTE: device must be initialized and opened already
iostream(IO_device *device);
~iostream();
// Formatted input functions, similar to ones that defined in STL
iostream& operator<<(int value);
iostream& operator<<(unsigned int value);
iostream& operator<<(char character);
iostream& operator<<(char *string);
// For I\O manipulators
iostream& operator<<(iostream& (*func)(iostream<IO_device>&));
// NOTE: this will be used later with different manipulators,
// such that used for hex or octal output of integers
// Puts a single character
iostream& put(char c);
iostream& operator>>(int& value);
iostream& operator>>(unsigned int& value);
iostream& operator>>(char& character);
iostream& operator>>(char *string);
// For I\O manipulators
iostream& operator>>(iostream& (*func)(iostream<IO_device>&));
// NOTE: this will be used later with different manipulators,
// such that used for hex or octal output of integers
private:
// No need to implement methods again,
// if they are already present
istream<IO_device> m_in;
ostream<IO_device> m_out;
// NOTE: any fields that are essential
// for this class, must be added to this class
};
//!
//! \brief Static initializer for every translation unit.
//! \details [Nifty counter idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter)
//! is used here.
//!
static struct iostream_initializer
{
iostream_initializer();
~iostream_initializer();
} stream_initializer;
// Standard streams, defined elsewhere
// These streams rely on specific driver, which name should be
// the same across all targets
extern istream<console_driver> &cin;
extern ostream<console_driver> &cout;
extern ostream<console_driver> &cerr;
//------------------------------------------------------------------------------
template<class IO_device>
iostream<IO_device>::iostream(IO_device *device)
:m_in{device}
,m_out{device}
{
}
template<class IO_device>
iostream<IO_device>::~iostream()
{
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator<< (int value)
{
m_out << value;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator<< (unsigned int value)
{
m_out << value;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator<<(char character)
{
m_out << character;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator<<(char *string)
{
m_out << string;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator<<(
iostream& (*func)(iostream<IO_device>&))
{
m_out << func;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::put(char c)
{
m_out.put(c);
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator>>(int& value)
{
m_in >> value;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator>>(unsigned int& value)
{
m_in >> value;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator>>(char& character)
{
m_in >> character;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator>>(char *string)
{
m_in >> string;
return *this;
}
template<class IO_device>
iostream<IO_device>& iostream<IO_device>::operator>>(
iostream& (*func)(iostream<IO_device>&))
{
m_in >> func;
return *this;
}
} // namespace ecl
#endif // ECL_IOSTREAM_HPP