/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program 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 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program 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.
*/

#ifndef _POLYMAKE_PIPESTREAM_H
#define _POLYMAKE_PIPESTREAM_H "$Project: polymake $$Id: pipestream.h,v 1.3 2004/06/22 16:22:42 gawrilow Exp $"

#if defined(__GNUC__) && __GNUC_MINOR__<3 && !defined(__APPLE__) && !defined(__COMO__)
#pragma interface
#endif

#include <socketstream.h>
#include <sys/types.h>

namespace std {

class pipestream_base {
public:
   pipestream_base(int fd_arg)
      : _pid(0), _fd(fd_arg) { }

   pipestream_base(const char* progname, char* const command[])
   {
      open(progname, command);
   }

   pipestream_base(char* const command[])
   {
      open(command[0], command);
   }

   ~pipestream_base();
protected:
   pid_t _pid;
   int _fd;

   void open(const char* progname, char* const command[]);
};

class ipipestream
   : protected pipestream_base, public istream {
public:
   ipipestream(const char* progname, char* const command[])
      : pipestream_base(progname,command),
	istream(new socketbuf(_fd)) { }

   ipipestream(char* const command[])
      : pipestream_base(command),
	istream(new socketbuf(_fd)) { }

   ~ipipestream() { delete rdbuf(); }
};

class opipestream
   : protected pipestream_base, public ostream {
public:
   opipestream(const char* progname, char* const command[])
      : pipestream_base(progname,command),
	ostream(new socketbuf(_fd)) { }

   opipestream(char* const command[])
      : pipestream_base(command),
	ostream(new socketbuf(_fd)) { }

   ~opipestream() { delete rdbuf(); }
};

class pipestream
   : protected pipestream_base, public iostream {
public:
   pipestream(const char* progname, char* const command[])
      : pipestream_base(progname,command),
	iostream(new socketbuf(_fd)) { }

   pipestream(char* const command[])
      : pipestream_base(command),
	iostream(new socketbuf(_fd)) { }

   pipestream(int fd_arg)
      : pipestream_base(fd_arg),
	iostream(new socketbuf(_fd)) { }

   ~pipestream() { delete rdbuf(); }
};

} // end namespace std

#endif // _POLYMAKE_PIPESTREAM_H

// Local Variables:
// mode:C++
// End:
