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 
00036   protected:
00038     Atom();
00040     Atom(const Atom &src);
00041   public:
00043     virtual ~Atom();
00044 #ifndef SWIG // no assignment operator for swig
00045 
00046     Atom& operator=(const Atom &);
00047 #endif
00048 
00054     static void setCurrentMessageStream(MsgType type,
00055                   const boost::shared_ptr<std::ostream> &s=boost::make_shared<std::ostream>(std::cout.rdbuf()));
00056 
00058     static void setCurrentMessageStreamActive(MsgType type, bool active);
00059 
00061     void setMessageStreamActive(MsgType type, bool active);
00062 
00066     void adoptMessageStreams(const Atom *src=NULL);
00067 
00071     std::ostream &msg(MsgType type) {
00072       return *_msg[type];
00073     }
00077     bool msgAct(MsgType type) {
00078       return *_msgAct[type];
00079     }
00080 
00083     static std::ostream &msgStatic(MsgType type) {
00084       return *_msgStatic[type];
00085     }
00088     static bool msgActStatic(MsgType type) {
00089       return *_msgActStatic[type];
00090     }
00091 
00092   private:
00093 
00094     // A stream which prints to null.
00095     static boost::shared_ptr<std::ostream> _nullStream;
00096 
00097     // Static pointer arrays of streams and active flags which were used for newly created objects.
00098     // These can be changed using setCurrentMessageStream(...)
00099     static boost::array<boost::shared_ptr<bool        >, SIZE> _msgActStatic;
00100     static boost::array<boost::shared_ptr<std::ostream>, SIZE> _msgSavedStatic;
00101     static boost::array<boost::shared_ptr<std::ostream>, SIZE> _msgStatic;
00102 
00103     // Pointer arrays to streams and active flags this object uses.
00104     // (these have a life-time at least as long as the object itself, ensured by reference counting)
00105     boost::array<boost::shared_ptr<bool        >, SIZE> _msgAct;
00106     boost::array<boost::shared_ptr<std::ostream>, SIZE> _msgSaved;
00107     boost::array<boost::shared_ptr<std::ostream>, SIZE> _msg;
00108 };
00109 
00110 }
00111 
00112 #endif