iutest  1.17.99.14
iutest_message.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_MESSAGE_HPP_0A05C876_F204_41F5_895F_F8454AB283B1_
16 #define INCG_IRIS_IUTEST_MESSAGE_HPP_0A05C876_F204_41F5_895F_F8454AB283B1_
17 
18 //======================================================================
19 // include
20 // IWYU pragma: begin_exports
21 #include "../iutest_env.hpp"
22 #include "../iutest_printers.hpp"
23 // IWYU pragma: end_exports
24 
25 namespace iutest {
26 namespace detail
27 {
28 
29 //======================================================================
30 // declare
34 ::std::string FormatFileLocation(const char* file, int line);
38 ::std::string FormatCompilerIndependentFileLocation(const char* file, int line);
39 
40 //======================================================================
41 // class
45 class iuStreamMessage
46 {
47 public:
48  iuStreamMessage() {}
49  explicit iuStreamMessage(const char* message) : m_stream(message) {}
50  iuStreamMessage(const iuStreamMessage& rhs) : m_stream(rhs.GetString()) {}
51 
52 public:
53  ::std::string GetString() const { return m_stream.str(); }
54 public:
55  template<typename T>
56  iuStreamMessage& operator << (const T& value)
57  {
58 #if !defined(IUTEST_NO_ARGUMENT_DEPENDENT_LOOKUP)
59  m_stream << PrintToString(value);
60 #else
61  m_stream << value;
62 #endif
63  return *this;
64  }
65 
66 #if !defined(IUTEST_NO_ARGUMENT_DEPENDENT_LOOKUP)
67  template<typename T, size_t SIZE>
68  iuStreamMessage& operator << (const T(&value)[SIZE])
69  {
70  m_stream << PrintToString(value);
71  return *this;
72  }
73 #else
74  iuStreamMessage& operator << (bool b)
75  {
76  m_stream << (b ? "true" : "false");
77  return *this;
78  }
79 #endif
80 
81 #if IUTEST_HAS_IOMANIP
82  iuStreamMessage& operator << (iu_basic_iomanip val)
83  {
84  m_stream << val;
85  return *this;
86  }
87 #endif
88 
89 public:
93  void add_message(const char* str) { append(str); }
94 private:
95  void append(const char* str);
96 
97 private:
98  iuStreamMessage& operator = (const iuStreamMessage&);
99 
100 private:
101  iu_global_format_stringstream m_stream;
102 };
103 
104 inline iu_ostream& operator << (iu_ostream& os, const iuStreamMessage& msg)
105 {
106  return os << msg.GetString();
107 }
108 
112 class iuCodeMessage
113 {
114  ::std::string m_message;
115  const char* m_file;
116  int m_line;
117 public:
118  iuCodeMessage(const char* file, int line, const char* message)
119  : m_message(message)
120  , m_file(file ? file : kStrings::UnknownFile)
121  , m_line(line)
122  {}
123  iuCodeMessage(const char* file, int line, const iuStreamMessage& message)
124  : m_message(message.GetString())
125  , m_file(file ? file : kStrings::UnknownFile)
126  , m_line(line)
127  {}
128 public:
129  const char* message() const { return m_message.c_str(); }
130  const char* file_name() const IUTEST_CXX_NOEXCEPT_SPEC { return m_file; }
131  int line_number() const IUTEST_CXX_NOEXCEPT_SPEC { return m_line; }
132 
133 
134 public:
135  template<typename T>
136  iuCodeMessage& operator << (const T& value)
137  {
138  m_message += (iuStreamMessage() << value).GetString();
139  return *this;
140  }
141 
142 public:
146  void add_message(const ::std::string& str);
147 
148 public:
150  ::std::string make_message() const;
151  ::std::string make_newline_message() const
152  {
153  return make_message() + "\n";
154  }
155 };
156 
157 } // end of namespace detail
158 } // end of namespace iutest
159 
160 #if !IUTEST_HAS_LIB
161 # include "../impl/iutest_message.ipp" // IWYU pragma: export
162 #endif
163 
164 #endif // INCG_IRIS_IUTEST_MESSAGE_HPP_0A05C876_F204_41F5_895F_F8454AB283B1_
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:811
iutest root namespace
Definition: iutest_charcode.hpp:33
std::string PrintToString(const T &v)
文字列化
Definition: iutest_printers.hpp:767