fmatvec  0.0.0
Fast Matrix/Vector Library
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 
21 class FMATVEC_EXPORT Atom {
22  public:
23 
25  // When adding new message type a stream and a initial active flag must be provided in atom.cc (see NEW TYPES HERE)
26  enum MsgType {
27  Info, // Informational messages
28  Warn, // Warning messages
29  Debug, // Debugging messages
30  Error, // Error messages
31  Deprecated, // Error messages
32  Status, // Status messages (only the last message is relevant)
33  SIZE // Must be the last enum in this list
34  };
35  #define FMATVEC_ATOM_MSGTYPE_SIZE 6
36 #ifndef SWIG // swig can not parse this however it is not needed for swig
37  static_assert(SIZE==FMATVEC_ATOM_MSGTYPE_SIZE, "The proprocessor define FMATVEC_ATOM_MSGTYPE_SIZE must be equal Atom::SIZE.");
38 #endif
39 
40  protected:
42  Atom();
44  Atom(const Atom &src);
45  public:
47  virtual ~Atom();
48 #ifndef SWIG // no assignment operator for swig
49  Atom& operator=(const Atom &);
51 #endif
52 
58  static void setCurrentMessageStream(MsgType type,
59  const std::shared_ptr<bool> &a=std::make_shared<bool>(true),
60  const std::shared_ptr<std::ostream> &s=std::make_shared<std::ostream>(std::cout.rdbuf()));
61 
63  void setMessageStreamActive(MsgType type, bool activeFlag);
64 
66  void getMessageStream(MsgType type,
67  std::shared_ptr<bool> &a,
68  std::shared_ptr<std::ostream> &s) const;
69 
73  void adoptMessageStreams(const Atom *src=nullptr);
74 
78  std::ostream &msg(MsgType type) const {
79  return *_msg[type];
80  }
84  bool msgAct(MsgType type) const {
85  return *_msgAct[type];
86  }
87 
90  static std::ostream &msgStatic(MsgType type) {
91  return *_msgStatic[type];
92  }
95  static bool msgActStatic(MsgType type) {
96  return *_msgActStatic[type];
97  }
98 
99  private:
100 
101  // A stream which prints to null.
102  static std::shared_ptr<std::ostream> _nullStream;
103 
104  // Static pointer arrays of streams and active flags which were used for newly created objects.
105  // These can be changed using setCurrentMessageStream(...)
106  static std::array<std::shared_ptr<bool >, SIZE> _msgActStatic;
107  static std::array<std::shared_ptr<std::ostream>, SIZE> _msgSavedStatic;
108  static std::array<std::shared_ptr<std::ostream>, SIZE> _msgStatic;
109 
110  // Pointer arrays to streams and active flags this object uses.
111  // (these have a life-time at least as long as the object itself, ensured by reference counting)
112  std::array<std::shared_ptr<bool >, SIZE> _msgAct;
113  std::array<std::shared_ptr<std::ostream>, SIZE> _msgSaved;
114  std::array<std::shared_ptr<std::ostream>, SIZE> _msg;
115 };
116 
117 #ifndef SWIG
118 class FMATVEC_EXPORT PrePostfixedStream : public std::ostream {
120  public:
122  PrePostfixedStream(const std::string &prefix_, const std::string &postfix_, const std::function<void(const std::string &)> &f);
124  PrePostfixedStream(const std::string &prefix_, const std::string &postfix_, std::ostream &outstr_);
125  private:
126  class FMATVEC_EXPORT StringBuf : public std::stringbuf {
127  public:
128  StringBuf(const std::string &prefix_, const std::string &postfix_, const std::function<void(const std::string &)> &f_);
129  protected:
130  int sync() override;
131  std::string prefix;
132  std::string postfix;
133  const std::function<void(const std::string &)> f;
134  };
135  StringBuf buffer;
136 };
137 #endif
138 
139 }
140 
141 #endif
static std::ostream & msgStatic(MsgType type)
Definition: atom.h:90
bool msgAct(MsgType type) const
Definition: atom.h:84
Definition: atom.h:21
std::ostream & msg(MsgType type) const
Definition: atom.h:78
static bool msgActStatic(MsgType type)
Definition: atom.h:95
Namespace fmatvec.
Definition: _memory.cc:28
MsgType
Messages can be printed to different message types named here.
Definition: atom.h:26