OpenMPTL - Helper Library
C++ Microprocessor Template Library
poorman_ostream.hpp
Go to the documentation of this file.
1 /*
2  * OpenMPTL - C++ Microprocessor Template Library
3  *
4  * Copyright (C) 2012-2017 Axel Burri <axel@tty0.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef POORMAN_OSTREAM_HPP_INCLUDED
22 #define POORMAN_OSTREAM_HPP_INCLUDED
23 
24 #include <type_traits>
25 
26 namespace poorman {
27 
28 template<typename Tp>
29 class ostream
30 {
31  static char nibble(unsigned val) {
32  val &= 0xf;
33  return val < 10 ? '0' + val : 'a' + val - 10;
34  }
35 
36 public:
37  using char_type = Tp;
38 
39  virtual ostream & put(char_type c) = 0;
40  virtual ostream & puts(const char_type* s) = 0;
41  virtual ostream & write(const char_type* s, unsigned int count) = 0;
42  virtual ostream & flush() = 0;
43  virtual ostream & endl() = 0;
44  // virtual ostream & widen(char_type c) { }; // TODO: implement in terminal_ostream
45 
46  /** hexadecimal output of any integral type */
47  template<typename valT>
48  typename std::enable_if<std::is_integral<valT>::value, ostream &>::type
49  friend operator <<(ostream & st, valT val)
50  {
51  for(int i = sizeof(valT) * 8 - 4; i >= 0; i -= 4) {
52  st.put(nibble(val >> i));
53  }
54  return st;
55  }
56 
57  friend ostream & operator<<(ostream & st, const char * s) {
58  st.puts(s);
59  return st;
60  }
61 
63  return func(*this);
64  }
65 };
66 
67 /** manipulator, flushes the output stream */
68 template<typename Tp>
69 inline ostream<Tp> & flush(ostream<Tp> & st) {
70  return st.flush();
71 }
72 
73 /** manipulator, outputs newline and flushes the output stream */
74 template<typename Tp>
75 inline ostream<Tp> & endl(ostream<Tp> & st) {
76  return st.endl();
77 }
78 
79 } // namespace poorman
80 
81 #endif // POORMAN_OSTREAM_HPP_INCLUDED
virtual ostream & write(const char_type *s, unsigned int count)=0
virtual ostream & puts(const char_type *s)=0
Definition: poorman_ostream.hpp:29
virtual ostream & flush()=0
virtual ostream & put(char_type c)=0
std::enable_if< std::is_integral< valT >::value, ostream & >::type friend operator<<(ostream &st, valT val)
hexadecimal output of any integral type
Definition: poorman_ostream.hpp:49
friend ostream & operator<<(ostream &st, const char *s)
Definition: poorman_ostream.hpp:57
Definition: poorman_cstring.hpp:26
fifoT::char_type char_type
Definition: poorman_ostream.hpp:37
ostream & operator<<(ostream &(*func)(ostream &))
Definition: poorman_ostream.hpp:62
virtual ostream & endl()=0