//-------------------------------------------------------------------------------------------
// <copyright file="LqrSMACPipes.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// Declares the Pipes class, which creates a set of pipes and handles closing of the pipes on
// destruction.
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------

#ifndef __LqrSMACPipes_h__
#define __LqrSMACPipes_h__

#include "LqrSMACDeclareException.h"
#include "LqrSMACLockableConcept.h"
#include "LqrSMACSharedLib.h"
#include <stdexcept>
#include <boost/shared_ptr.hpp>

namespace LiquidRobotics {
namespace SMACFramework {

class SHARED_LIB_PUBLIC Pipes : public LockableConceptMutex
{
public:

	typedef boost::shared_ptr<Pipes> Ptr;

	DECLARE_EXCEPTION(Interrupted, std::runtime_error);
	DECLARE_EXCEPTION(PipeIsClosed, std::runtime_error);
	DECLARE_EXCEPTION(SystemError, std::runtime_error);

	Pipes(bool create = true);
	Pipes(const Pipes& other);

	virtual ~Pipes();

	Pipes& operator=(const Pipes& other);

	void Close();
	void CloseReadPipe();
	void CloseWritePipe();
	void Create();
	int ReadPipe() const;
	bool ReadPipeOpen() const;
	int WritePipe() const;
	bool WritePipeOpen() const;

	static void MakeCloseOnExec(int fd, bool set = true);
	static void MakeNonBlocking(int fd, bool set = true);

private:

	int DupFd(int fd);
	void Init();
	bool IsOpen(int idx) const;
	static void CloseIfOpen(int& fd);
	void Copy(int idx, const Pipes& other);
	void DefaultPipes();

	static int CheckedFcntl(int fd, int cmd);
	static int CheckedFcntl(int fd, int cmd, long arg);

	int fds[2];
};

}
}

#endif
