/*
 *
 *
 *    pipe.h
 *
 *    C++ header file for class 'pipeend.'
 *
 *    =========================================================================
 *
 *    Copyright (C) 2001-2003 by Matt Roberts, KK5JY.
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    =========================================================================
 *
 *    TODO:
 *       1.  Add a way to flush buffers to some base class
 *
 *
 */


#ifndef __PIPE_H
#define __PIPE_H

#include <my++/exception.h>
#include <my++/device.h>
#include <errno.h>

	namespace my {

		/*
		 *
		 *
		 *    class my::pipeend
		 *
		 *
		 */

		class pipeend : public my::iodevice {
			public:
				typedef my::exception_template<pipeend> exception;

				typedef enum {
					mode_none  = 0,
					mode_read  = 1,
					mode_write = 2
				} mode;

			private: // private data
				mutable int fd;
				mode p_mode;

			protected:
				virtual void assign(int f, mode m);

			public: // from iodevice
				int getfd (void) throw() { return fd; };

				void open (const std::string &name, int modes) { throw pipeend::exception(this, EINVAL); };
				void close (void) throw();

				ssize_t read_vp (void *buf, size_t len);
				ssize_t write_vp (const void *buf, size_t len);

			public:
				mode getmode(void) { return p_mode; };

			public: // public ctors / dtors
				pipeend (void);
				~pipeend (void) throw();

			private: // hidden or disabled functions
				pipeend (const pipeend &t);

			public:
				friend void pipe (my::pipeend &in, my::pipeend &out);
				friend void pipe (int &in, my::pipeend &out);
				friend void pipe (my::pipeend &in, int &out);
		};


		/*
		 *
		 *    pipe()
		 *
		 */

		extern void pipe (my::pipeend &in, my::pipeend &out);
		extern void pipe (int &in, my::pipeend &out);
		extern void pipe (my::pipeend &in, int &out);

	}

#endif /* __PIPE_H */
