OTest2
A C++ testing framework
teeostream.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <teeostream.h>
21 
22 #include <assert.h>
23 #include <streambuf>
24 #include <vector>
25 
26 #include <utils.h>
27 
28 namespace OTest2 {
29 
30 class TeeOStream::Buffer : public std::streambuf {
31  private:
32  std::vector<std::ostream*> sinks;
33 
34  public:
35  explicit Buffer(
36  std::initializer_list<std::ostream*> sinks_);
37  virtual ~Buffer();
38 
39  /* -- avoid copying */
40  Buffer(
41  const Buffer&) = delete;
43  const Buffer&) = delete;
44 
45  void appendSink(
46  std::ostream* sink_);
47 
48  private:
49  virtual int overflow(
50  int c_) override;
51 };
52 
54  std::initializer_list<std::ostream*> sinks_) :
55  sinks(sinks_) {
56 
57 }
58 
60 
61 }
62 
64  std::ostream* sink_) {
65  assert(sink_ != nullptr);
66  sinks.push_back(sink_);
67 }
68 
69 int TeeOStream::Buffer::overflow(
70  int c_) {
71  if(c_ != traits_type::eof()) {
72  for(std::ostream* sink_ : sinks) {
73  sink_->put(traits_type::to_char_type(c_));
74  }
75  }
76  return traits_type::not_eof(c_);
77 }
78 
80  buffer(new Buffer{}) {
81  rdbuf(buffer);
82 }
83 
85  std::initializer_list<std::ostream*> sinks_) :
86  buffer(new Buffer(sinks_)) {
87  rdbuf(buffer);
88 }
89 
91  odelete(buffer);
92 }
93 
95  std::ostream* sink_) {
96  buffer->appendSink(sink_);
97 }
98 
99 } /* -- namespace OTest2 */
teeostream.h
OTest2::TeeOStream::Buffer::operator=
Buffer & operator=(const Buffer &)=delete
OTest2::TeeOStream::Buffer::~Buffer
virtual ~Buffer()
Definition: teeostream.cpp:59
utils.h
OTest2
Definition: assertbean.h:25
OTest2::TeeOStream::Buffer::appendSink
void appendSink(std::ostream *sink_)
Definition: teeostream.cpp:63
OTest2::TeeOStream::Buffer
Definition: teeostream.cpp:30
OTest2::TeeOStream::appendSink
void appendSink(std::ostream *sink_)
Append another sink stream.
Definition: teeostream.cpp:94
OTest2::TeeOStream::TeeOStream
TeeOStream()
Ctor - no sinks.
Definition: teeostream.cpp:79
OTest2::TeeOStream::Buffer::Buffer
Buffer(std::initializer_list< std::ostream * > sinks_)
Definition: teeostream.cpp:53
OTest2::TeeOStream::~TeeOStream
virtual ~TeeOStream()
Ctor.
Definition: teeostream.cpp:90
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34