-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.hpp
72 lines (63 loc) · 1.66 KB
/
Utilities.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
#ifndef __UTILITIES_HPP
#define __UTILITIES_HPP
/**
* CS-17, Utilities.hpp
* This class provides the base mechanism to display an object
*
* The derived class should provide a "describe" method returning a string
* The << operator is also provided here but not as a member of Utilities
*
*
* @author Christophe Gattardi
* @version 1.0 15/03/2020
*/
// //////////////////////////////////////////////////////////////////////
// Import section
// //////////////////////////////////////////////////////////////////////
// STL
#include <iosfwd>
#include <sstream>
#include <string>
class Utilities{
public:
/**
* Dump an Object into an output stream.
*
* @param ostream& the output stream.
*/
void toStream (std::ostream& ioOut) const {
ioOut << describe();
}
private:
/**
* Return a string representing the object
*
* @return std::string Dump of the object.
*/
virtual std::string describe() const = 0;
};
/**
* Piece of code given by Nicolai M. Josuttis, Section 13.12.1 "Implementing
* Output Operators" (p653) of his book "The C++ Standard Library: A Tutorial
* and Reference", published by Addison-Wesley.
*/
template <class charT, class traits>
inline
std::basic_ostream<charT, traits>&
operator<< (std::basic_ostream<charT, traits>& ioOut,
const Utilities& iUtils) {
/**
string stream:
- with same format
- without special field width
*/
std::basic_ostringstream<charT,traits> ostr;
ostr.copyfmt (ioOut);
ostr.width (0);
// Fill string stream
iUtils.toStream (ostr);
// Print string stream
ioOut << ostr.str();
return ioOut;
}
#endif //__UTILITIES_HPP