forb
code_ostream.hpp
1 //
2 // Created by gabriele on 14/11/18.
3 //
4 
5 #ifndef FORBCC_CODE_OSSTREAM_H
6 #define FORBCC_CODE_OSSTREAM_H
7 
8 #include <ostream>
9 #include <sstream>
10 
11 namespace forbcc {
12 
14  class code_ostream : public std::ostream {
15 
16  /* ********************************************** INNER CLASS *********************************************** */
17 
19  class code_stringbuf : public std::stringbuf {
20 
21  /* ********************************************* ATTRIBUTES ********************************************* */
23  std::ostream &output;
24 
26  int indentation = 0;
27 
28  /* ******************************************** CONSTRUCTORS ******************************************** */
29  public:
31  explicit code_stringbuf(std::ostream &output) : output(output) {};
32 
33  /**********************************************************************************************************/
34 
36  code_stringbuf(code_stringbuf &&) = default;
37 
39  code_stringbuf &operator=(code_stringbuf &&) = delete;
40 
42  code_stringbuf(const code_stringbuf &) = delete;
43 
45  code_stringbuf &operator=(const code_stringbuf &) = delete;
46 
47  /**********************************************************************************************************/
48 
50  ~code_stringbuf() override {
51  if (pbase() != pptr()) {
52  putOutput();
53  }
54  };
55 
61  int sync() override {
62  putOutput();
63  return 0;
64  };
65 
67  void increment_indentation() { ++indentation; };
68 
70  void decrement_indentation() { --indentation; };
71 
72  private:
74  void putOutput();
75 
76  };
77 
78  /* *********************************************** ATTRIBUTES *********************************************** */
79  private:
81  code_stringbuf buffer;
82 
83  public:
84  /* ********************************************** CONSTRUCTORS ********************************************** */
85 
87  explicit code_ostream(std::ostream &output) : std::ostream(&buffer), buffer(output) {};
88 
89  /**************************************************************************************************************/
90 
92  ~code_ostream() override = default;
93 
95  code_ostream(code_ostream &&) = delete;
96 
98  code_ostream &operator=(code_ostream &&) = delete;
99 
101  code_ostream(const code_ostream &) = delete;
102 
104  code_ostream &operator=(const code_ostream &) = delete;
105 
106  /**************************************************************************************************************/
107 
109  void increment_indentation() { buffer.increment_indentation(); };
110 
112  void decrement_indentation() { buffer.decrement_indentation(); };
113  };
114 
115 }
116 
117 
118 #endif //FORBCC_CODE_OSSTREAM_H
void increment_indentation()
Proxy to code_ostream::code_stringbuf::increment_indentation.
Definition: code_ostream.hpp:109
~code_ostream() override=default
This class is virtual, so it requires a virtual destructor.
void decrement_indentation()
Proxy to code_ostream::code_stringbuf::decrement_indentation.
Definition: code_ostream.hpp:112
This output stream supports indentation of code lines, which are formatted at each std::endl...
Definition: code_ostream.hpp:14
Definition: code_ostream.hpp:11
code_ostream & operator=(code_ostream &&)=delete
This class does not support move-assignment.
code_ostream(std::ostream &output)
Creates a new code_ostream starting from the given one.
Definition: code_ostream.hpp:87