GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
teeStream.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <ostream>
5 #include <streambuf>
6 #include <vector>
7 
8 //--------------------------------------------------------------------------
9 // CodeGenerator::TeeBuf
10 //--------------------------------------------------------------------------
11 // A stream buffer to support 'Teeing' streams - curtesy of http://wordaligned.org/articles/cpp-streambufs
12 namespace CodeGenerator
13 {
14 class TeeBuf: public std::streambuf
15 {
16 public:
17  // Construct a streambuf which tees output to multiple streambufs
18  template<typename... T>
19  TeeBuf(T&&... streamBufs) : m_StreamBufs({{streamBufs.rdbuf()...}})
20  {
21  }
22 
23 private:
24  //--------------------------------------------------------------------------
25  // std::streambuf virtuals
26  //--------------------------------------------------------------------------
27  virtual int overflow(int c) override
28  {
29  if (c == EOF) {
30  return !EOF;
31  }
32  else {
33  bool anyEOF = false;
34  for(auto &s: m_StreamBufs) {
35  if(s->sputc(c) == EOF) {
36  anyEOF = true;
37  }
38  }
39  return anyEOF ? EOF : c;
40  }
41  }
42 
43  // Sync all teed buffers.
44  virtual int sync() override
45  {
46  bool anyNonZero = false;
47  for(auto &s: m_StreamBufs) {
48  if(s->pubsync() != 0) {
49  anyNonZero = true;
50  }
51  }
52 
53  return anyNonZero ? -1 : 0;
54  }
55 private:
56  //--------------------------------------------------------------------------
57  // Members
58  //--------------------------------------------------------------------------
59  const std::vector<std::streambuf*> m_StreamBufs;
60 };
61 
62 //--------------------------------------------------------------------------
63 // CodeGenerator::TeeStream
64 //--------------------------------------------------------------------------
65 class TeeStream : public std::ostream
66 {
67 public:
68  template<typename... T>
69  TeeStream(T&&... streamBufs)
70  : std::ostream(&m_TeeBuf), m_TeeBuf(std::forward<T>(streamBufs)...)
71  {
72  }
73 
74 private:
75  //--------------------------------------------------------------------------
76  // Members
77  //--------------------------------------------------------------------------
78  TeeBuf m_TeeBuf;
79 };
80 } // namespace CodeGenerator
STL namespace.
Helper class for generating code - automatically inserts brackets, indents etc.
Definition: backendBase.h:30
Definition: teeStream.h:14
TeeStream(T &&... streamBufs)
Definition: teeStream.h:69
TeeBuf(T &&... streamBufs)
Definition: teeStream.h:19
Definition: teeStream.h:65