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