Evo C++ Library v0.5.1
iothread.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_iothread_h
9 #define INCL_evo_iothread_h
10 
11 #include "io.h"
12 #include "thread.h"
13 #include "file.h"
14 
16 
45 #define EVO_CONSOLE_MT static evo::ConsoleMT& c = evo::con_mt()
46 
70 #define EVO_IO_MT(STREAM, CODE) { Mutex::Lock evo_streamlock_(STREAM .mutex); STREAM.stream CODE; }
71 
92 #if EVO_CATCH_DEBUG == 0
93  #define EVO_CATCH_MT(CODE) catch (const evo::Exception& e) { \
94  { \
95  evo::ConsoleMT& c = evo::con_mt(); \
96  Mutex::Lock lock(c.err.mutex); \
97  c.err.stream << evo::NL << e.msg() << " -- "; \
98  evo::errormsg_out(c.err.stream, e.error()); \
99  c.err.stream << evo::NL; \
100  } \
101  CODE; \
102  }
103 #else
104  #define EVO_CATCH_MT(CODE) catch (const evo::Exception& e) { \
105  { \
106  evo::ConsoleMT& c = evo::con_mt(); \
107  Mutex::Lock lock(c.err.mutex); \
108  c.err.stream << evo::NL; \
109  if (e.file()) \
110  c.err.stream << e.file() << '(' << e.line() << evo::SubString("): ", 3); \
111  c.err.stream << e.msg() << evo::SubString(" -- ", 4); \
112  evo::errormsg_out(c.err.stream, e.error()); \
113  c.err.stream << evo::NL; \
114  } \
115  CODE; \
116  }
117 #endif
118 
120 
121 // Namespace: evo
122 namespace evo {
123 
126 
128 
132 template<class T>
133 struct StreamMT {
134  typedef StreamMT<T> This;
135 
137  T stream;
138 
142  StreamMT(bool exceptions=EVO_EXCEPTIONS) : stream(exceptions)
143  { }
144 
145 private:
146  StreamMT(This&);
147  This& operator=(This&);
148 };
149 
154 template<>
155 struct StreamMT<File> {
157 
160 
162  StreamMT(Newline nl=NL, bool exceptions=EVO_EXCEPTIONS) : stream(nl, exceptions)
163  { }
164 
166  StreamMT(const char* path, Open mode=oREAD, bool flushlines=false, Newline nl=NL_SYS, bool exceptions=EVO_EXCEPTIONS) :
167  stream(path, mode, flushlines, nl, exceptions)
168  { }
169 
170 private:
171  StreamMT(This&);
172  This& operator=(This&);
173 };
174 
178 
180 
206 struct ConsoleMT {
210  typedef Mutex::Lock Lock;
211 
212  InT in;
213  OutT out;
214  OutT err;
215 
219  static ConsoleMT& get()
220  { static ConsoleMT console(con()); return console; }
221 
222 private:
223  ConsoleMT(Console& c) : in(c.in), out(c.out), err(c.err)
224  { }
225 
226  ConsoleMT(const ConsoleMT&);
227  ConsoleMT& operator=(const ConsoleMT&);
228 };
229 
233 inline ConsoleMT& con_mt()
234  { return ConsoleMT::get(); }
235 
237 
238 }
239 #endif
StreamMT< T > This
This type.
Definition: iothread.h:134
Mutex mutex
Stream mutex.
Definition: iothread.h:158
File stream
Stream object.
Definition: iothread.h:159
OutT err
Write to console, error output.
Definition: iothread.h:214
StreamMT(bool exceptions=1)
Constructor.
Definition: iothread.h:142
Console & con()
Shortcut for Console::get().
Definition: io.h:1015
Console I/O.
Definition: io.h:972
StreamMT< PipeOut > PipeOutMT
Output pipe with mutex for synchonization – see StreamMT.
Definition: iothread.h:176
Multithreaded console I/O.
Definition: iothread.h:206
StreamMT(const char *path, Open mode=oREAD, bool flushlines=false, Newline nl=NL_SYS, bool exceptions=1)
Constructor to open file.
Definition: iothread.h:166
Open
Open mode for files and streams.
Definition: sysio.h:190
Newline
Newline type.
Definition: sys.h:748
Evo File I/O stream class.
StreamMT< File > This
This type.
Definition: iothread.h:156
Evo I/O streams and Console I/O.
PipeOut err
Write to console, error output.
Definition: io.h:979
Mutex for thread synchronization.
Definition: thread.h:104
InT in
Read console input.
Definition: iothread.h:212
StreamMT(Newline nl=NL, bool exceptions=1)
Constructor.
Definition: iothread.h:162
Evo threads implementation.
#define EVO_EXCEPTIONS
Whether to throw exceptions on error by default.
Definition: evo_config.h:35
ConsoleMT & con_mt()
Shortcut for ConsoleMT::get().
Definition: iothread.h:233
Mutex mutex
Stream mutex.
Definition: iothread.h:136
Read only.
Definition: sysio.h:191
Smart locking for synchronization.
Definition: lock.h:28
Stream output formatter with state.
Definition: iobase.h:406
File I/O stream.
Definition: file.h:77
PipeOut::Format Format
Stream output formatter with state.
Definition: iothread.h:207
StreamExtMT< PipeOut, Mutex > OutT
Output type.
Definition: iothread.h:209
static const NewlineDefault & NL
Default newline type.
Definition: sys.h:785
T stream
Stream object.
Definition: iothread.h:137
Mutex::Lock Lock
Mutex lock type.
Definition: iothread.h:210
PipeIn in
Read console input.
Definition: io.h:977
Evo C++ Library namespace.
Definition: alg.h:11
PipeOut out
Write to console, normal output.
Definition: io.h:978
StreamExtMT< PipeIn, Mutex > InT
Input type.
Definition: iothread.h:208
OutT out
Write to console, normal output.
Definition: iothread.h:213
static ConsoleMT & get()
Get console instance to use.
Definition: iothread.h:219
static const Newline NL_SYS
Current system newline type.
Definition: sys.h:763
Stream with mutex for multithreaded synchronization.
Definition: iothread.h:133
StreamMT< PipeIn > PipeInMT
Input pipe with mutex for synchonization – see StreamMT.
Definition: iothread.h:175
StreamMT< File > FileMT
File with mutex for synchonization – see File and StreamMT<File>
Definition: iothread.h:177
Stream with mutex for multithreaded synchronization, specialized for File.
Definition: iothread.h:155