fmatvec 0.0.0

atom.h
1 #ifndef _FMATVEC_ATOM_H
2 #define _FMATVEC_ATOM_H
3
4 #include <ostream>
5 #include <iostream>
6 #include <functional>
7 #include <memory>
8 #include <array>
9 #include <sstream>
10 #include "stream.h"
11
12 namespace fmatvec {
13
14 class AdoptCurrentMessageStreamsUntilScopeExit;
15
23 class FMATVEC_EXPORT Atom {
24 friend class AdoptCurrentMessageStreamsUntilScopeExit;
25 public:
26
28 // When adding new message type a stream and a initial active flag must be provided in atom.cc (see NEW TYPES HERE)
29 enum MsgType {
30 Info, // Informational messages
31 Warn, // Warning messages
32 Debug, // Debugging messages
33 Error, // Error messages
34 Deprecated, // Error messages
35 Status, // Status messages (only the last message is relevant)
36 SIZE // Must be the last enum in this list
37 };
38 #define FMATVEC_ATOM_MSGTYPE_SIZE 6
39 #ifndef SWIG // swig can not parse this however it is not needed for swig
40 static_assert(SIZE==FMATVEC_ATOM_MSGTYPE_SIZE, "The proprocessor define FMATVEC_ATOM_MSGTYPE_SIZE must be equal Atom::SIZE.");
41 #endif
42
43 protected:
45 Atom();
47 Atom(const Atom &src);
48 public:
50 virtual ~Atom();
51 #ifndef SWIG // no assignment operator for swig
52 Atom& operator=(const Atom &);
54 #endif
55
61 static void setCurrentMessageStream(MsgType type,
62 const std::shared_ptr<bool> &a=std::make_shared<bool>(true),
63 const std::shared_ptr<std::ostream> &s=std::make_shared<std::ostream>(std::cout.rdbuf()));
64
66 void setMessageStreamActive(MsgType type, bool activeFlag);
67
69 void getMessageStream(MsgType type,
70 std::shared_ptr<bool> &a,
71 std::shared_ptr<std::ostream> &s) const;
72
76 void adoptMessageStreams(const Atom *src=nullptr);
77
81 std::ostream &msg(MsgType type) const {
82 return *_msg[type];
83 }
87 bool msgAct(MsgType type) const {
88 return *_msgAct[type];
89 }
90
93 static std::ostream &msgStatic(MsgType type) {
94 return *_msgStatic[type];
95 }
98 static bool msgActStatic(MsgType type) {
99 return *_msgActStatic[type];
100 }
101
102 private:
103
104 // A stream which prints to null.
105 static std::shared_ptr<std::ostream> _nullStream;
106
107 // Static pointer arrays of streams and active flags which were used for newly created objects.
108 // These can be changed using setCurrentMessageStream(...)
109 static std::array<std::shared_ptr<bool >, SIZE> _msgActStatic;
110 static std::array<std::shared_ptr<std::ostream>, SIZE> _msgSavedStatic;
111 static std::array<std::shared_ptr<std::ostream>, SIZE> _msgStatic;
112
113 // Pointer arrays to streams and active flags this object uses.
114 // (these have a life-time at least as long as the object itself, ensured by reference counting)
115 std::array<std::shared_ptr<bool >, SIZE> _msgAct;
116 std::array<std::shared_ptr<std::ostream>, SIZE> _msgSaved;
117 std::array<std::shared_ptr<std::ostream>, SIZE> _msg;
118 };
119
120 #ifndef SWIG
121 class FMATVEC_EXPORT PrePostfixedStream : public std::ostream {
123 public:
125 PrePostfixedStream(const std::string &prefix_, const std::string &postfix_, const std::function<void(const std::string &)> &f);
127 PrePostfixedStream(const std::string &prefix_, const std::string &postfix_, std::ostream &outstr_);
128 private:
129 class StringBuf : public std::stringbuf {
130 public:
131 StringBuf(std::string prefix_, std::string postfix_, const std::function<void(const std::string &)> &f_);
132 protected:
133 int sync() override;
134 std::string prefix;
135 std::string postfix;
136 const std::function<void(const std::string &)> f;
137 };
138 StringBuf buffer;
139 };
140 #endif
141
144 class FMATVEC_EXPORT AdoptCurrentMessageStreamsUntilScopeExit {
145 public:
148 AdoptCurrentMessageStreamsUntilScopeExit(const Atom* src);
149 ~AdoptCurrentMessageStreamsUntilScopeExit();
150 AdoptCurrentMessageStreamsUntilScopeExit(const AdoptCurrentMessageStreamsUntilScopeExit& other) = delete;
151 AdoptCurrentMessageStreamsUntilScopeExit(AdoptCurrentMessageStreamsUntilScopeExit&& other) noexcept = delete;
152 AdoptCurrentMessageStreamsUntilScopeExit& operator=(const AdoptCurrentMessageStreamsUntilScopeExit& other) = delete;
153 AdoptCurrentMessageStreamsUntilScopeExit& operator=(AdoptCurrentMessageStreamsUntilScopeExit&& other) noexcept = delete;
154 private:
155 std::array<std::pair<std::shared_ptr<bool>, std::shared_ptr<std::ostream>>, Atom::SIZE> savedStreams;
156 };
157
158 }
159
160 #endif
fmatvec::Atom::msgStatic
static std::ostream & msgStatic(MsgType type)
Definition: atom.h:93
fmatvec::Atom::msgAct
bool msgAct(MsgType type) const
Definition: atom.h:87
fmatvec::Atom
Definition: atom.h:23
std::shared_ptr
Definition: types.h:81
fmatvec::Atom::msg
std::ostream & msg(MsgType type) const
Definition: atom.h:81
fmatvec::Atom::msgActStatic
static bool msgActStatic(MsgType type)
Definition: atom.h:98
fmatvec
Namespace fmatvec.
Definition: _memory.cc:28
fmatvec::PrePostfixedStream::StringBuf
Definition: atom.h:129
fmatvec::AdoptCurrentMessageStreamsUntilScopeExit
Definition: atom.h:144
fmatvec::Atom::MsgType
MsgType
Messages can be printed to different message types named here.
Definition: atom.h:29