COVISE Core
tryPrint.h
Go to the documentation of this file.
1#ifndef COVISE_TRY_PRINT_H
2#define COVISE_TRY_PRINT_H
3#include <type_traits>
4#include <iostream>
5namespace covise
6{
7
8 template <class T>
9 static auto hasPrintMethod(int)
10 -> std::integral_constant<bool, std::is_reference<decltype(operator<<(std::cerr, T()))>::value>;
11
12 template <class>
13 static auto hasPrintMethod(...) -> std::false_type;
14
15 template <typename T>
16 struct HasPrintMethod : decltype(hasPrintMethod<T>(0))
17 {
18 };
19
20 template <class T>
22 tryPrintWithError(std::ostream &os, const T &t, const char *className, const char *errorMsg)
23 {
24 os << t;
25 }
26
27 template <class T>
29 tryPrintWithError(std::ostream &os, const T &t, const char *className, const char *errorMsg)
30 {
31 os << t;
32 }
33
34 // same as above
35 template <class T>
37 tryPrintWithError(std::ostream &os, const T &t, const char *className, const char *errorMsg)
38 {
39 if (className && errorMsg)
40 {
41 os << className << errorMsg;
42 }
43 }
44
45
46 template<>
47 inline void tryPrintWithError<char const*>(std::ostream& os, const char* const& t, const char* className, const char* errorMsg)
48 {
49 if (t)
50 {
51 os << t;
52 }
53 else
54 {
55 os << "nullptr";
56 }
57 }
58
59 template<>
60 inline void tryPrintWithError<char *>(std::ostream& os, char* const& t, const char* className, const char* errorMsg)
61 {
62 if (t)
63 {
64 os << t;
65 }
66 else
67 {
68 os << "nullptr";
69 }
70 }
71
72
73 template <class T>
74 void tryPrint(const T &t)
75 {
76 tryPrintWithError(std::cerr, t, nullptr, nullptr);
77 }
78
79} // namespace covise
80
81#endif // !COVISE_TRY_PRINT_H
GLsizei const GLfloat * value
Definition: khronos-glext.h:6760
GLdouble GLdouble t
Definition: khronos-glext.h:6449
GLenum type
Definition: khronos-glext.h:6279
list of all chemical elements
Definition: coConfig.h:27
std::enable_if< HasPrintMethod< T >::value >::type tryPrintWithError(std::ostream &os, const T &t, const char *className, const char *errorMsg)
Definition: tryPrint.h:22
static auto hasPrintMethod(int) -> std::integral_constant< bool, std::is_reference< decltype(operator<<(std::cerr, T()))>::value >
void tryPrintWithError< char const * >(std::ostream &os, const char *const &t, const char *className, const char *errorMsg)
Definition: tryPrint.h:47
QTextStream & operator<<(QTextStream &out, const coConfigEntryStringList list)
Definition: coConfigEntryString.cpp:159
void tryPrintWithError< char * >(std::ostream &os, char *const &t, const char *className, const char *errorMsg)
Definition: tryPrint.h:60
void tryPrint(const T &t)
Definition: tryPrint.h:74
Definition: tryPrint.h:17