Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglOStream.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglOStream.hpp
00021  *   Author: Tom Stimson
00022  *  Created: 8 April 2000
00023  *  Summary: Output stream class with various levels of verbosity.
00024  *****************************************************************************/
00025 
00026 #ifndef __SGL_OSTREAM_HPP
00027 #define __SGL_OSTREAM_HPP
00028 
00029 #include <sgl.h>
00030 
00031 class sglNullStreamBuf;
00032 
00033 //----------------------------------------------------------------------------
00034 
00038 class SGL_DLL_API sglOStream
00039 {
00040 public: // Types
00042    enum PrintLevelEnum
00043    {
00044       eALWAYS,   // These messages will always print.
00045       eWARNING,  // These messages print if threshold => eWARNING.
00046       eINFO,     // These messages print if threshold => eINFO.
00047       eDEBUG     // These messages print if threshold = eDEBUG.
00048    };
00049 
00051    typedef void (*callback)(void);
00052 
00053 public: // Static Methods
00057    static void setStream(ostream* stream);
00058 
00062    static ostream* getStream();
00063 
00069    static void setThreshold(PrintLevelEnum threshold);
00070 
00074    static PrintLevelEnum getThreshold();
00075 
00080    static void setPreFunc(callback func);
00081 
00085    static callback getPreFunc();
00086 
00091    static void setPostFunc(callback func);
00092 
00096    static callback getPostFunc();
00097 
00098 #ifdef WIN32
00099 
00103    static string formatWIN32Error(int error_number);
00104 #endif
00105 
00106 public: // Instance Methods
00108    sglOStream(PrintLevelEnum level = eALWAYS)
00109       : m_print_level(level)
00110       {
00111          if (s_pre_func && m_print_level <= s_threshold) (*s_pre_func)();
00112       }
00113 
00115    virtual ~sglOStream()
00116       {
00117          if (s_post_func && m_print_level <= s_threshold) (*s_post_func)();
00118       }
00119 
00121    ostream& getOStream()
00122       {
00123          if (s_stream && m_print_level <= s_threshold)
00124             return *s_stream;
00125          else
00126             return s_null_ostream;
00127       }
00128 
00129 private:
00130    // Not Implemented.  Do not use.
00131    sglOStream(const sglOStream &);
00132    sglOStream& operator=(const sglOStream &);
00133 
00134 private:
00135    static PrintLevelEnum    s_threshold;
00136    static ostream          *s_stream;
00137    static callback          s_pre_func;
00138    static callback          s_post_func;
00139 
00140    static sglNullStreamBuf *s_null_buff;
00141    static ostream           s_null_ostream;
00142 
00143    PrintLevelEnum           m_print_level;
00144 };
00145 
00146 
00147 // This is a dummy class designed to allow the optimizer to
00148 // eliminate debug prints.
00149 class SGL_DLL_API sglNullStream
00150 {
00151 public:
00152    sglNullStream() {};
00153    ~sglNullStream() {};
00154 
00155    // This is where the voodoo occurs.  The typical print
00156    // line "sglPrintDebug() << "blah" << endl;" will be expanded
00157    // to a series of calls to sglNullStream::operator<<(T).
00158    // These calls don't do anything, so the optimizer should
00159    // realize that the entire line does nothing (and eliminate it).
00160    template <class T>
00161    inline const sglNullStream& operator<<(const T&) const
00162       {
00163          return *this;
00164       }
00165 #ifdef WIN32
00166    // Windows is stupid.  This is for the endl.
00167    inline const sglNullStream& operator<<(
00168       ostream::_Myt& (__cdecl *_F)(ostream::_Myt&)) const
00169       {
00170          return *this;
00171       }
00172 #endif
00173 #if defined(vxw)
00174    // The compiler that comes with tornado 2 is brain damaged.
00175    inline const sglNullStream& operator<<(char* rhs) const
00176       {
00177          return *this;
00178       }
00179 #endif
00180 #if (defined(__GNUC__) && (__GNUC__==2) && ((__GNUC_MINOR__<=91) || (__GNUC_MINOR__==96)))
00181    // Old G++ is stupid.  This is for the endl.
00182    inline const sglNullStream& operator<<(ostream& (*_F)(ostream&)) const
00183       {
00184          return *this;
00185       }
00186    // Old G++ is stupid.  This is for the hex, dec, etc...
00187    inline const sglNullStream& operator<<(ios& (*_F)(ios&)) const
00188       {
00189          return *this;
00190       }
00191 #endif
00192 #if (defined(sgi) && defined(_STANDARD_C_PLUS_PLUS)) || defined(__ghs__)
00193    inline const sglNullStream& operator<<(ostream& (*_F)(ostream&)) const
00194       {
00195          return *this;
00196       }
00197 #endif
00198 
00199 private:  // not implemented
00200    sglNullStream(const sglNullStream &);
00201    sglNullStream &operator=(const sglNullStream &);
00202 };
00203 
00204 
00205 // Initiate a print stream that always prints.
00206 #define sglPrint() (sglOStream(sglOStream::eALWAYS).getOStream())
00207 
00208 // Create a global const null stream visible to the optimizer.
00209 const sglNullStream sglNullStreamInst;
00210 
00211 
00212 // When compiling a debug version, a call to sglPrint??? will return
00213 // a temporary sglOStream.  The constructor will be called at the
00214 // beginning of the stream, and the destructor will mark the end.
00215 // When compiling an optimized version, a call to sglPrint??? will
00216 // return a NullStream that should allow the optimizer to eliminate
00217 // the code.
00218 
00219 #ifdef NO_SGL_WARNING_PRINTS
00220 inline const sglNullStream &sglPrintWarning() { return sglNullStreamInst; }
00221 #else
00222 # define sglPrintWarning() (sglOStream(sglOStream::eWARNING).getOStream())
00223 #endif
00224 
00225 #ifdef NO_SGL_INFO_PRINTS
00226 inline const sglNullStream &sglPrintInfo() { return sglNullStreamInst; }
00227 #else
00228 # define sglPrintInfo() (sglOStream(sglOStream::eINFO).getOStream())
00229 #endif
00230 
00231 #ifdef NO_SGL_DEBUG_PRINTS
00232 inline const sglNullStream &sglPrintDebug() { return sglNullStreamInst; }
00233 #else
00234 # define sglPrintDebug() (sglOStream(sglOStream::eDEBUG).getOStream())
00235 #endif
00236 
00237 
00238 #endif

Generated at Mon Jul 1 18:00:05 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001