iutest  1.17.99.14
iutest_stream.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_STREAM_HPP_3A4AF139_9F24_4730_81D0_DADFCE6DCF99_
16 #define INCG_IRIS_IUTEST_STREAM_HPP_3A4AF139_9F24_4730_81D0_DADFCE6DCF99_
17 
18 //======================================================================
19 // include
20 // IWYU pragma: begin_exports
21 #include "iutest_string.hpp"
22 // IWYU pragma: end_exports
23 
24 namespace iutest {
25 namespace detail
26 {
27 
28 //======================================================================
29 // class
33 class IOutStream
34 {
35 public:
36  virtual ~IOutStream() {}
37 public:
39  virtual bool Write(const void* buf, size_t size, size_t cnt) = 0;
40 public:
41  virtual int Printf(const char* fmt, ...) IUTEST_ATTRIBUTE_FORMAT_PRINTF(2, 3)
42  {
43  va_list va;
44  va_start(va, fmt);
45  const ::std::string str = StringFormat(fmt, va);
46  va_end(va);
47  const size_t len = str.length();
48  Write(str.c_str(), len, 1);
49  return static_cast<int>(len);
50  }
51 };
52 
56 class IInStream
57 {
58 public:
59  virtual ~IInStream() {}
60 public:
62  virtual bool Read(void* buf, size_t size, size_t cnt) = 0;
63 
65  virtual iu_uint_max_t GetSize() = 0;
66 
67 public:
69  virtual ::std::string ReadAll()
70  {
71  ::std::string str;
72  const size_t size = static_cast<size_t>(GetSize());
73  if( size != 0 )
74  {
75  char* buf = new char[size+1];
76  buf[size] = '\0';
77  if( Read(buf, size, 1) )
78  {
79  str = buf;
80  }
81  delete [] buf;
82  }
83  return str;
84  }
85 };
86 
90 class FileOutStream : public IOutStream
91 {
92 protected:
93  FILE* m_fp;
94 public:
95  explicit FileOutStream(FILE* fp) IUTEST_CXX_NOEXCEPT_SPEC
96  : m_fp(fp)
97  {}
98 public:
105  virtual bool Write(const void* buf, size_t size, size_t cnt) IUTEST_CXX_OVERRIDE
106  {
107  if( fwrite(buf, size, cnt, m_fp) < cnt )
108  {
109  return false;
110  }
111  return true;
112  }
113 };
114 
115 } // end of namespace detail
116 } // end of namespace iutest
117 
118 #endif // INCG_IRIS_IUTEST_STREAM_HPP_3A4AF139_9F24_4730_81D0_DADFCE6DCF99_
#define IUTEST_CXX_OVERRIDE
override definition
Definition: iutest_compiler.hpp:747
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:811
iris unit test string utilities
iutest root namespace
Definition: iutest_charcode.hpp:33