GeNN  3.3.0
GPU enhanced Neuronal Networks (GeNN)
codeStream.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <ostream>
5 #include <streambuf>
6 #include <string>
7 #include <vector>
8 
9 //----------------------------------------------------------------------------
10 // CodeStream
11 //----------------------------------------------------------------------------
13 
14 class CodeStream : public std::ostream
15 {
16 private:
17  //------------------------------------------------------------------------
18  // IndentBuffer
19  //------------------------------------------------------------------------
20  class IndentBuffer : public std::streambuf
21  {
22  public:
23  IndentBuffer() : m_Sink(NULL), m_NewLine(false), m_IndentLevel(0){}
24 
25  //--------------------------------------------------------------------
26  // Public API
27  //--------------------------------------------------------------------
28  void indent()
29  {
30  m_IndentLevel++;
31  }
32 
33  void deindent()
34  {
35  m_IndentLevel--;
36  }
37 
38  void setSink(std::streambuf *sink)
39  {
40  m_Sink = sink;
41  }
42 
43  private:
44  //--------------------------------------------------------------------
45  // Streambuf overrides
46  //--------------------------------------------------------------------
47  virtual int overflow(int c) override;
48 
49  //----------------------------------------------------------------------------
50  // Members
51  //----------------------------------------------------------------------------
52  std::streambuf *m_Sink;
53  bool m_NewLine;
54  unsigned int m_IndentLevel;
55  };
56 
57 public:
58  //------------------------------------------------------------------------
59  // OB
60  //------------------------------------------------------------------------
62 
64  struct OB
65  {
66  OB(unsigned int level) : Level(level){}
67 
68  const unsigned int Level;
69  };
70 
71  //------------------------------------------------------------------------
72  // CB
73  //------------------------------------------------------------------------
75 
77  struct CB
78  {
79  CB(unsigned int level) : Level(level){}
80 
81  const unsigned int Level;
82  };
83 
84  //------------------------------------------------------------------------
85  // Scope
86  //------------------------------------------------------------------------
87  class Scope
88  {
89  public:
90  Scope(CodeStream &codeStream)
91  : m_CodeStream(codeStream), m_Level(s_NextLevel++)
92  {
93  m_CodeStream << CodeStream::OB(m_Level);
94  }
95 
97  {
98  m_CodeStream << CodeStream::CB(m_Level);
99  }
100 
101  private:
102  //------------------------------------------------------------------------
103  // Static members
104  //------------------------------------------------------------------------
105  static unsigned int s_NextLevel;
106 
107  //------------------------------------------------------------------------
108  // Members
109  //------------------------------------------------------------------------
110  CodeStream &m_CodeStream;
111  const unsigned int m_Level;
112  };
113 
114  CodeStream(): std::ostream(&m_Buffer) {
115  m_Braces.push_back(0);
116  }
117 
118  CodeStream(std::ostream &stream): CodeStream() {
119  setSink(stream);
120  }
121 
122  void setSink(std::ostream &stream)
123  {
124  m_Buffer.setSink(stream.rdbuf());
125  }
126 
127 private:
128  //------------------------------------------------------------------------
129  // Friends
130  //------------------------------------------------------------------------
131  friend std::ostream& operator << (std::ostream& s, const OB &ob);
132  friend std::ostream& operator << (std::ostream& s, const CB &cb);
133 
134  //------------------------------------------------------------------------
135  // Members
136  //------------------------------------------------------------------------
137  IndentBuffer m_Buffer;
138  std::vector<unsigned int> m_Braces;
139 };
140 
141 //------------------------------------------------------------------------
142 // Operators
143 //------------------------------------------------------------------------
144 std::ostream& operator << (std::ostream& s, const CodeStream::OB &ob);
145 std::ostream& operator << (std::ostream& s, const CodeStream::CB &cb);
~Scope()
Definition: codeStream.h:96
A close bracket marker.
Definition: codeStream.h:77
An open bracket marker.
Definition: codeStream.h:64
const unsigned int Level
Definition: codeStream.h:68
const unsigned int Level
Definition: codeStream.h:81
Helper class for generating code - automatically inserts brackets, indents etc.
Definition: codeStream.h:14
friend std::ostream & operator<<(std::ostream &s, const OB &ob)
Definition: codeStream.cc:46
OB(unsigned int level)
Definition: codeStream.h:66
CodeStream()
Definition: codeStream.h:114
Definition: codeStream.h:87
CB(unsigned int level)
Definition: codeStream.h:79
void setSink(std::ostream &stream)
Definition: codeStream.h:122
Scope(CodeStream &codeStream)
Definition: codeStream.h:90
CodeStream(std::ostream &stream)
Definition: codeStream.h:118