All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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 
11 namespace fmatvec {
12 
20 class Atom {
21  public:
22 
24  // When adding new message type a stream and a initial active flag must be provided in atom.cc (see NEW TYPES HERE)
25  enum MsgType {
26  Info, // Informational messages
27  Warn, // Warning messages
28  Debug, // Debugging messages
29  Error, // Error messages
30  Deprecated, // Error messages
31  Status, // Status messages (only the last message is relevant)
32  SIZE // Must be the last enum in this list
33  };
34  #define FMATVEC_ATOM_MSGTYPE_SIZE 6
35 #ifndef SWIG // swig can not parse this however it is not needed for swig
36  static_assert(SIZE==FMATVEC_ATOM_MSGTYPE_SIZE, "The proprocessor define FMATVEC_ATOM_MSGTYPE_SIZE must be equal Atom::SIZE.");
37 #endif
38 
39  protected:
41  Atom();
43  Atom(const Atom &src);
44  public:
46  virtual ~Atom();
47 #ifndef SWIG // no assignment operator for swig
48  Atom& operator=(const Atom &);
50 #endif
51 
57  static void setCurrentMessageStream(MsgType type,
58  const std::shared_ptr<bool> &a=std::make_shared<bool>(true),
59  const std::shared_ptr<std::ostream> &s=std::make_shared<std::ostream>(std::cout.rdbuf()));
60 
62  void setMessageStreamActive(MsgType type, bool activeFlag);
63 
65  void getMessageStream(MsgType type,
66  std::shared_ptr<bool> &a,
67  std::shared_ptr<std::ostream> &s) const;
68 
72  void adoptMessageStreams(const Atom *src=nullptr);
73 
77  std::ostream &msg(MsgType type) const {
78  return *_msg[type];
79  }
83  bool msgAct(MsgType type) const {
84  return *_msgAct[type];
85  }
86 
89  static std::ostream &msgStatic(MsgType type) {
90  return *_msgStatic[type];
91  }
94  static bool msgActStatic(MsgType type) {
95  return *_msgActStatic[type];
96  }
97 
98  private:
99 
100  // A stream which prints to null.
101  static std::shared_ptr<std::ostream> _nullStream;
102 
103  // Static pointer arrays of streams and active flags which were used for newly created objects.
104  // These can be changed using setCurrentMessageStream(...)
105  static std::array<std::shared_ptr<bool >, SIZE> _msgActStatic;
106  static std::array<std::shared_ptr<std::ostream>, SIZE> _msgSavedStatic;
107  static std::array<std::shared_ptr<std::ostream>, SIZE> _msgStatic;
108 
109  // Pointer arrays to streams and active flags this object uses.
110  // (these have a life-time at least as long as the object itself, ensured by reference counting)
111  std::array<std::shared_ptr<bool >, SIZE> _msgAct;
112  std::array<std::shared_ptr<std::ostream>, SIZE> _msgSaved;
113  std::array<std::shared_ptr<std::ostream>, SIZE> _msg;
114 };
115 
116 #ifndef SWIG
117 class PrePostfixedStream : public std::ostream {
119  public:
121  PrePostfixedStream(const std::string &prefix_, const std::string &postfix_, const std::function<void(const std::string &)> &f);
123  PrePostfixedStream(const std::string &prefix_, const std::string &postfix_, std::ostream &outstr_);
124  private:
125  class StringBuf : public std::stringbuf {
126  public:
127  StringBuf(const std::string &prefix_, const std::string &postfix_, const std::function<void(const std::string &)> &f_);
128  protected:
129  int sync() override;
130  std::string prefix;
131  std::string postfix;
132  const std::function<void(const std::string &)> f;
133  };
134  StringBuf buffer;
135 };
136 #endif
137 
138 }
139 
140 #endif
static std::ostream & msgStatic(MsgType type)
Definition: atom.h:89
static void setCurrentMessageStream(MsgType type, const std::shared_ptr< bool > &a=std::make_shared< bool >(true), const std::shared_ptr< std::ostream > &s=std::make_shared< std::ostream >(std::cout.rdbuf()))
Definition: atom.cc:58
Definition: atom.h:20
void adoptMessageStreams(const Atom *src=nullptr)
Definition: atom.cc:76
void setMessageStreamActive(MsgType type, bool activeFlag)
Set the active flag of this object and all objects which were created using the same message stream a...
Definition: atom.cc:64
bool msgAct(MsgType type) const
Definition: atom.h:83
PrePostfixedStream(const std::string &prefix_, const std::string &postfix_, const std::function< void(const std::string &)> &f)
Call f with every message of the stream, prefixed/postfixed.
Atom()
When a Atom is default constructed use the current statically set message streams.
Definition: atom.cc:40
void getMessageStream(MsgType type, std::shared_ptr< bool > &a, std::shared_ptr< std::ostream > &s) const
Get the shared message stream active flag and the shared message stream of this object.
Definition: atom.cc:69
static bool msgActStatic(MsgType type)
Definition: atom.h:94
std::ostream & msg(MsgType type) const
Definition: atom.h:77
virtual ~Atom()
dtor.
MsgType
Messages can be printed to different message types named here.
Definition: atom.h:25
Atom & operator=(const Atom &)
When a Atom is assinged do not change the messsage streams since we always use the message streams be...
Definition: atom.cc:54

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home