Virtual Testbed
Ship dynamics simulator for extreme conditions
CircularLineBuffer.hh
1 #ifndef VTESTBED_GUI_CIRCULARLINEBUFFER_HH
2 #define VTESTBED_GUI_CIRCULARLINEBUFFER_HH
3 
4 #include <mutex>
5 #include <string>
6 #include <vector>
7 
8 #include <vtestbed/core/circular_buffer.hh>
9 
10 namespace vtb {
11 
12  namespace gui {
13 
14  enum class LogLineMarker {Output, Error, Log};
15 
16  class LogLine {
17 
18  private:
19  std::string _string;
20  LogLineMarker _marker = static_cast<LogLineMarker>(0);
21 
22  public:
23 
24  LogLine() { this->_string.reserve(512); }
25 
26  inline
27  LogLine(std::string s, LogLineMarker m):
28  _string(s), _marker(m) {}
29 
30  inline void
31  append(char ch) {
32  this->_string += ch;
33  }
34 
35  inline void
36  marker(LogLineMarker m) {
37  this->_marker = m;
38  }
39 
40  inline LogLineMarker
41  marker() const {
42  return this->_marker;
43  }
44 
45  inline bool
46  hasMarker() const {
47  return this->_marker != LogLineMarker(0);
48  }
49 
50  inline bool
51  isError() const {
52  return this->_marker == LogLineMarker::Error;
53  }
54 
55  inline const std::string&
56  string() const {
57  return this->_string;
58  }
59 
60  inline bool
61  matches(const char* keyword) const {
62  return this->_string.find(keyword) != std::string::npos;
63  }
64 
65  inline void
66  clear() {
67  this->_string.clear();
68  this->_marker = static_cast<LogLineMarker>(0);
69  }
70 
71  friend class CircularLineBuffer;
72 
73  };
74 
75  template <class T>
76  struct pointer_pair {
77 
78  typedef const T* const_pointer;
79 
80  const_pointer first, last;
81 
82  inline const_pointer begin() const { return first; }
83  inline const_pointer end() const { return last; }
84  inline size_t size() const { return last-first; }
85 
86  };
87 
89 
90  public:
95  typedef LogLine* pointer;
96  typedef const LogLine* const_pointer;
98 
99  private:
100  static constexpr const size_t max_lines = 500;
101  mutex_t _mutex;
102  line_array _lines{max_lines};
103  LogLine _currentLine;
104  bool _dirty = false;
105 
106  public:
107 
108  void
109  put(const char* s, LogLineMarker marker, std::streamsize n=1);
110 
111  void
112  clear();
113 
114  inline void
115  lock() {
116  this->_mutex.lock();
117  }
118 
119  inline void
120  unlock() {
121  this->_mutex.unlock();
122  }
123 
124  inline const line_array&
125  lines() const {
126  return this->_lines;
127  }
128 
129  inline bool
130  sync() {
131  bool old = this->_dirty;
132  this->_dirty = false;
133  return old;
134  }
135 
136  private:
137 
138  void newLine();
139 
140  };
141 
142  }
143 
144 }
145 
146 #endif // vim:filetype=cpp
Main namespace.
Definition: convert.hh:9