atom.h
00001 #ifndef _FMATVEC_ATOM_H
00002 #define _FMATVEC_ATOM_H
00003 
00004 #include <ostream>
00005 #include <iostream>
00006 #include <boost/shared_ptr.hpp>
00007 #include <boost/make_shared.hpp>
00008 #include <boost/array.hpp>
00009 #include <boost/static_assert.hpp>
00010 
00011 namespace fmatvec {
00012 
00020 class Atom {
00021   public:
00022 
00024     // When adding new message type a stream and a initial active flag must be provided in atom.cc (see NEW TYPES HERE)
00025     enum MsgType {
00026       Info,  // Informational messages
00027       Warn,  // Warning messages
00028       Debug, // Debugging messages
00029       SIZE   // Must be the last enum in this list
00030     };
00031     #define FMATVEC_ATOM_MSGTYPE_SIZE 3
00032 #ifndef SWIG // swig can not parse this however it is not needed for swig
00033     BOOST_STATIC_ASSERT_MSG(SIZE==FMATVEC_ATOM_MSGTYPE_SIZE, "The proprocessor define FMATVEC_ATOM_MSGTYPE_SIZE must be equal Atom::SIZE.");
00034 #endif
00035 
00037     Atom();
00039     Atom(const Atom &src);
00041     virtual ~Atom();
00042 #ifndef SWIG // no assignment operator for swig
00043 
00044     Atom& operator=(const Atom &);
00045 #endif
00046 
00052     static void setCurrentMessageStream(MsgType type,
00053                   const boost::shared_ptr<std::ostream> &s=boost::make_shared<std::ostream>(std::cout.rdbuf()));
00054 
00056     static void setCurrentMessageStreamActive(MsgType type, bool active);
00057 
00059     void setMessageStreamActive(MsgType type, bool active);
00060 
00064     void adoptMessageStreams(const Atom *src=NULL);
00065 
00069     std::ostream &msg(MsgType type) {
00070       return *_msg[type];
00071     }
00075     bool msgAct(MsgType type) {
00076       return *_msgAct[type];
00077     }
00078 
00081     static std::ostream &msgStatic(MsgType type) {
00082       return *_msgStatic[type];
00083     }
00086     static bool msgActStatic(MsgType type) {
00087       return *_msgActStatic[type];
00088     }
00089 
00090   private:
00091 
00092     // A stream which prints to null.
00093     static boost::shared_ptr<std::ostream> _nullStream;
00094 
00095     // Static pointer arrays of streams and active flags which were used for newly created objects.
00096     // These can be changed using setCurrentMessageStream(...)
00097     static boost::array<boost::shared_ptr<bool        >, SIZE> _msgActStatic;
00098     static boost::array<boost::shared_ptr<std::ostream>, SIZE> _msgSavedStatic;
00099     static boost::array<boost::shared_ptr<std::ostream>, SIZE> _msgStatic;
00100 
00101     // Pointer arrays to streams and active flags this object uses.
00102     // (these have a life-time at least as long as the object itself, ensured by reference counting)
00103     boost::array<boost::shared_ptr<bool        >, SIZE> _msgAct;
00104     boost::array<boost::shared_ptr<std::ostream>, SIZE> _msgSaved;
00105     boost::array<boost::shared_ptr<std::ostream>, SIZE> _msg;
00106 };
00107 
00108 }
00109 
00110 #endif