CppCommon  1.0.4.1
C++ Common Library
iostream.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_UTILITY_IOSTREAM_H
10 #define CPPCOMMON_UTILITY_IOSTREAM_H
11 
12 #include <iostream>
13 
14 namespace CppCommon {
15 
17 class WriteChar
18 {
19 public:
20  explicit WriteChar(char ch) : _ch(ch) {}
21 
22  friend std::ostream& operator<<(std::ostream& stream, const WriteChar& writer)
23  { return stream << '\'' << writer._ch << '\''; }
24  friend std::wostream& operator<<(std::wostream& stream, const WriteChar& writer)
25  { return stream << '\'' << writer._ch << '\''; }
26 
27 private:
28  char _ch;
29 };
30 
33 {
34 public:
35  explicit WriteWChar(wchar_t ch) : _ch(ch) {}
36 
37  friend std::ostream& operator<<(std::ostream& stream, const WriteWChar& writer)
38  { return stream << '\'' << (char)writer._ch << '\''; }
39  friend std::wostream& operator<<(std::wostream& stream, const WriteWChar& writer)
40  { return stream << '\'' << writer._ch << '\''; }
41 
42 private:
43  wchar_t _ch;
44 };
45 
48 {
49 public:
50  template <size_t N>
51  explicit WriteString(const char (&str)[N]) : _str(str), _size(N) {}
52 
53  friend std::ostream& operator<<(std::ostream& stream, const WriteString& writer)
54  {
55  stream << '"';
56  stream.write(writer._str, writer._size);
57  stream << '"';
58  return stream;
59  }
60  friend std::wostream& operator<<(std::wostream& stream, const WriteString& writer)
61  {
62  stream << '"';
63  for (size_t i = 0; i < writer._size; ++i)
64  stream << writer._str[i];
65  stream << '"';
66  return stream;
67  }
68 
69 private:
70  const char* _str;
71  size_t _size;
72 };
73 
76 {
77 public:
78  template <size_t N>
79  explicit WriteWString(const wchar_t (&str)[N]) : _str(str), _size(N) {}
80 
81  friend std::ostream& operator<<(std::ostream& stream, const WriteWString& writer)
82  {
83  stream << '"';
84  for (size_t i = 0; i < writer._size; ++i)
85  stream << (char)writer._str[i];
86  stream << '"';
87  return stream;
88  }
89  friend std::wostream& operator<<(std::wostream& stream, const WriteWString& writer)
90  {
91  stream << '"';
92  stream.write(writer._str, writer._size);
93  stream << '"';
94  return stream;
95  }
96 
97 private:
98  const wchar_t* _str;
99  size_t _size;
100 };
101 
102 } // namespace CppCommon
103 
104 #endif // CPPCOMMON_UTILITY_IOSTREAM_H
Write character stream manipulator.
Definition: iostream.h:18
friend std::wostream & operator<<(std::wostream &stream, const WriteChar &writer)
Definition: iostream.h:24
WriteChar(char ch)
Definition: iostream.h:20
friend std::ostream & operator<<(std::ostream &stream, const WriteChar &writer)
Definition: iostream.h:22
Write string stream manipulator.
Definition: iostream.h:48
WriteString(const char(&str)[N])
Definition: iostream.h:51
friend std::wostream & operator<<(std::wostream &stream, const WriteString &writer)
Definition: iostream.h:60
friend std::ostream & operator<<(std::ostream &stream, const WriteString &writer)
Definition: iostream.h:53
Write wide character stream manipulator.
Definition: iostream.h:33
friend std::wostream & operator<<(std::wostream &stream, const WriteWChar &writer)
Definition: iostream.h:39
WriteWChar(wchar_t ch)
Definition: iostream.h:35
friend std::ostream & operator<<(std::ostream &stream, const WriteWChar &writer)
Definition: iostream.h:37
Write wide string stream manipulator.
Definition: iostream.h:76
friend std::ostream & operator<<(std::ostream &stream, const WriteWString &writer)
Definition: iostream.h:81
WriteWString(const wchar_t(&str)[N])
Definition: iostream.h:79
friend std::wostream & operator<<(std::wostream &stream, const WriteWString &writer)
Definition: iostream.h:89
C++ Common project definitions.
Definition: token_bucket.h:15