CommonIO.h

Go to the documentation of this file.
00001 /*  _________________________________________________________________________
00002  *
00003  *  UTILIB: A utility library for developing portable C++ codes.
00004  *  Copyright (c) 2001, Sandia National Laboratories.
00005  *  This software is distributed under the GNU Lesser General Public License.
00006  *  For more information, see the README file in the top UTILIB directory.
00007  *  _________________________________________________________________________
00008  */
00009 
00101 #ifndef __CommonIO_h
00102 #define __CommonIO_h
00103 
00104 #ifdef __GNUC__
00105 #pragma interface
00106 #endif
00107 
00108 #define USING_COMMONIO
00109 #ifdef USING_COMMONIO
00110 
00111 #ifdef  NON_ANSI
00112 #include <iostream.h>
00113 #include <fstream.h>
00114 #include <iomanip.h>
00115 #else
00116 #include <iostream>
00117 #include <fstream>
00118 #include <iomanip>
00119 using namespace std;
00120 #endif
00121 #include "_generic.h"
00122 
00123 #ifdef _MSC_VER
00124 #include <strstrea.h>
00125 #else
00126 #ifdef NON_ANSI
00127 #include <strstream.h>
00128 #else
00129 #include <strstream>
00130 #endif // NON_ANSI
00131 #endif // _MSC_VER
00132 #include "utilib_dll.h"
00133 
00134 
00135 extern "C" {
00141 void cout_print(const char* str);
00147 void cerr_print(const char* str);
00148 };
00149 
00150 #define DEBUG_COMMONIO
00151 
00152 #ifndef SWIG
00153 
00154 //
00155 // Macros for overriding the C++ streams
00156 //
00157 class CommonIO;
00163 extern CommonIO global_CommonIO;
00168 #define ucout    (**CommonIO::MapCout)
00169 
00173 #define ucerr    (**CommonIO::MapCerr)
00174 
00178 #define ucin    (**CommonIO::MapCin)
00179 
00182 #define stdcout (*CommonIO::std_cout)
00183 
00186 #define stdcerr (*CommonIO::std_cerr)
00187 
00190 #define stdcin  (*CommonIO::std_cin)
00191 
00192 
00193 //
00194 // Used for syncronizing output on MIMD machines
00195 //
00197 UTILIB_API void clear_print_marker_queue();
00199 UTILIB_API void set_sync_print(const int flag);
00201 UTILIB_API void get_print_marker();
00203 UTILIB_API void return_print_marker();
00204 
00205 
00206 #ifdef  DEBUGGING
00207   #ifdef DEBUGPR
00208   #undef DEBUGPR
00209   #endif
00210 
00211   #define DEBUGPR(dlevel,cmd)  if (verbosity(dlevel)) {cmd; CommonIO::flush();}
00212 
00213   #define DEBUGPRP(dlevel,cmd) if (verbosity(dlevel)) { cmd; }
00214 
00220   #define DEBUGPRX(dlevel,ptr,stuff) \
00221      if (ptr->verbosity(dlevel)) { ucout << stuff; CommonIO::flush(); }
00222 
00225   #define DEBUGPRXP(dlevel,ptr,stuff) \
00226      if (ptr->verbosity(dlevel)) { ucout << stuff; }
00227 
00231   #define REFER_DEBUG(pointer) \
00232     virtual int verbosity(int level) { return pointer->verbosity(level); };
00233 #else
00234   #define DEBUGPR(dlevel,cmd)          /* Nothing */
00235   #define DEBUGPRP(dlevel,cmd)         /* Nothing */
00236   #define DEBUGPRX(ptr,dlevel,stuff)   /* Nothing */
00237   #define DEBUGPRXP(ptr,dlevel,stuff)  /* Nothing */
00238   #define REFER_DEBUG(pointer)         /* Nothing */
00239 #endif
00240 
00241 #endif // SWIG
00242 
00243 
00244 #ifndef SWIG
00245 class UTILIB_API CommonIO {
00246 #else
00247 class CommonIO {
00248 #endif
00249 
00250   friend void cppMessage_abort();
00251   friend UTILIB_API ostream& Flush(ostream& outstr);
00252 
00253 public:
00254 
00256   CommonIO();
00258   virtual ~CommonIO();
00260   static void begin();
00262   static void begin_buffered();
00264   static void end();
00265 
00267   static void map_off()
00268                 {flush(); io_mapping = OFF; reset_map();}
00270   static void map_on()
00271                 { io_mapping = ON; reset_map();}
00272 
00274   static void reset();
00275 
00277   int debug;
00279   static int numDigits;
00280 
00282   virtual int  verbosity(const int level)
00283                 {
00284                 if ((IO_Rank != -1) && (IO_Rank != Rank))
00285                    return 0;
00286                 else
00287                    return (debug >= level);
00288                 }
00289 
00291   void  setDebug(int level)             { debug = level; }
00293   static void  setIORank(int Rank_)     { IO_Rank = Rank_; }
00295   static void  setIOFlush(int flush_)   { flush_flag = flush_; };
00296 
00298   static int rank()     { return Rank; };
00300   static int io_rank()     { return IO_Rank; };
00302   static int numProcs() { return Size; };
00303 
00305   static ostream& outs()
00306                 {return o_stream(0);}
00308   static ostream& errs()
00309                 {return o_stream(1);}
00311   static istream& ins()
00312                 {return (*common_cin);}
00314   static void set_streams(ostream* cout_, ostream* cerr_, istream* cin_)
00315                 {
00316                 if (cout_) common_cout = cout_; 
00317                 if (cerr_) common_cerr = cerr_; 
00318                 if (cin_) common_cin = cin_;
00319                 }
00321   static void set_cout(ostream* cout_)
00322                 { if (cout_) common_cout = cout_; }
00324   static void set_cerr(ostream* cerr_)
00325                 { if (cerr_) common_cerr = cerr_; }
00327   static void set_cin(istream* cin_)
00328                 { if (cin_) common_cin = cin_; }
00330   static void set_ofile(const char* ofile);
00331 
00333   static void begin_tagging()
00334                 {IOflush(); tagging=ON;}
00336   static void begin_tagging(const int numDigits_)
00337                 {IOflush(); numDigits = numDigits_; tagging=ON;}
00339   static void end_tagging()
00340                 {IOflush(); tagging=OFF;}
00342   static void flush()
00343                 {IOflush(); common_cout->flush(); common_cerr->flush();}
00344 
00346   static void sync_stdio()
00347                 {
00348 #ifndef COUGAR
00349                 ios::sync_with_stdio();
00350 #endif
00351                 }
00352 
00354   static ostream* std_cout;
00356   static ostream* std_cerr;
00358   static istream* std_cin;
00359 
00361   static ostream** MapCout;
00363   static ostream** MapCerr;
00365   static istream** MapCin;
00366 
00367 protected:
00369   static void end_all();
00370 
00371 private:
00372 
00374   static int io_mapping;
00376   static int io_buffering;
00378   static int begin_end_counter;
00379 
00381   static int IO_Rank;
00383   static int tagging;
00385   static int nref;
00387   static int flush_flag;
00389   static int seqNum;
00390 
00392   static int Rank;
00394   static int Size;
00395 
00397   static ostream& o_stream(const int flag);
00399   static int stream_flag;
00401   static int header_flag;
00403   static void IOflush();
00405   static void flush_stream(strstream& stream, ostream* tmp);
00407 #ifdef SGI
00408   static std::ofstream* common_ofstr;
00409 #else
00410   static ofstream* common_ofstr;
00411 #endif
00412 
00414   static void reset_map();
00416   static ostream* common_cout;
00418   static ostream* common_cerr;
00420   static istream* common_cin;
00421 
00423   static strstream* pStrCout;
00425   static strstream* pStrCerr;
00426 };
00427 
00428 
00429 
00430 #ifndef SWIG
00431 
00434 inline UTILIB_API ostream& Flush(ostream& outstr)
00435 {
00436 CommonIO::flush();
00437 if ((&outstr != CommonIO::pStrCout) && (&outstr != CommonIO::pStrCerr))
00438    outstr.flush();
00439 return outstr;
00440 }
00441 #endif
00442 
00443 
00444 #else   // COMMONIO
00445 
00446 #define ucout cout
00447 #define ucerr cerr
00448 #define ucin  cin
00449 #define Flush flush
00450 
00451 class CommonIO
00452 {
00453 public:
00454 
00455   CommonIO() {debug=0;}
00456   int debug;
00457 };
00458 #endif // COMMONIO
00459 
00460 #endif