callbk_reportstream.h.html mathcode2html   
 Source file:   callbk_reportstream.h
 Converted:   Sat May 9 2015 at 14:44:17
 This documentation file will not reflect any later changes in the source file.

$$\phantom{******** If you see this on the webpage then the browser could not locate *********}$$
$$\phantom{******** jsMath/easy/load.js or the variable root is set wrong in this file *********}$$
$$\newcommand{\vector}[1]{\left[\begin{array}{c} #1 \end{array}\right]}$$ $$\newenvironment{matrix}{\left[\begin{array}{cccccccccc}} {\end{array}\right]}$$ $$\newcommand{\A}{{\cal A}}$$ $$\newcommand{\W}{{\cal W}}$$

/* Copyright 2008-2011 Research Foundation State University of New York   */

/* This file is part of QUB Express.                                      */

/* QUB Express is free software; you can redistribute it and/or modify    */
/* it under the terms of the GNU General Public License as published by   */
/* the Free Software Foundation, either version 3 of the License, or      */
/* (at your option) any later version.                                    */

/* QUB Express is distributed in the hope that it will be useful,         */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
/* GNU General Public License for more details.                           */

/* You should have received a copy of the GNU General Public License,     */
/* named LICENSE.txt, in the QUB Express program directory.  If not, see  */
/* <http://www.gnu.org/licenses/>.                                        */

#ifndef CALLBACK_REPORTSTREAM_H
#define CALLBACK_REPORTSTREAM_H

 
// This header wraps a callbk_reportfun and its data pointer up in a std::ostream. //

Up: Index

#include <iostream>
#include <sstream>
#include <fstream>
#include "callbk_reportfun.h"
#include "qubfast.h"

class QUBFAST_API_CLASS callbk_reportbuf : public std::streambuf
{
 public:
 callbk_reportbuf() : lineout( new std::ostringstream )
    { setp(0,0), setg(0,0,0); }
  
  ~callbk_reportbuf() { delete lineout; }
  
  void setTarget( callbk_reportfun cb, void *_data )
  {
    callback = cb;
    data = _data;
  }
  
 protected:
  virtual int overflow( int ch )
  {
    if ( ch != EOF ) {
      if ( ch == '\n' ) {
	if ( callback )
	  callback( lineout->str().c_str(), data );
	else
	  std::cout << lineout->str().c_str() << std::endl;
	delete lineout;
	lineout = new std::ostringstream;
      }
      else {
	(*lineout) << (char) ch;
      }
    }
    return ch;
  }
  
 private:
  callbk_reportfun callback;
  void *data;
  std::ostringstream *lineout;
};

class QUBFAST_API_CLASS callbk_reportstream : public std::ostream
{
 public:
 callbk_reportstream( callbk_reportfun cb, void *data )
   : std::basic_ostream<char,std::char_traits<char> >( new callbk_reportbuf )
    {
      ((callbk_reportbuf *) rdbuf())->setTarget( cb, data );
    }
  virtual ~callbk_reportstream()
    {
      delete rdbuf();
    }
};

#endif