//---------------------------------------------------------------------------------
// <copyright file="LqrSMACTempFile.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>
// 	Declaration of TempFile class, which creates a file with an arbitrary name
//  and makes it available to the caller.  The object lifespan is used to determine
//  the file life span: when the object is deleted, the file is also deleted.
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------

#ifndef __LQRSMACTEMPFILE_H__
#define __LQRSMACTEMPFILE_H__

#include <string>
#include <stdexcept>
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>
#include "LqrSMACByteArray.h"
#include "LqrSMACSharedLib.h"

namespace LiquidRobotics {
namespace SMACFramework {

class SHARED_LIB_PUBLIC TempFile : public boost::noncopyable
{
public:

	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef std::string string;
	typedef boost::shared_ptr<TempFile> Ptr;

	// The constructor creates a temp file, attempting to do so up to the number
	// of times specified.  If the operation fails, no exception is thrown; test
	// using the IsOpen() method or catch exceptions on subsequent actions.
	TempFile(unsigned maxTries = 10) throw();

	// The destructor confirms that the file is closed and deleted.
	~TempFile();

	// Close() closes the file and deletes it
	void Close() throw();

	// GetContent() returns the content of the file in the result byte array
	void GetContent(ByteArray& result) const;

	// returns the size of the file
	off_t GetSize(bool throwOnError = true) const;

	// IsOpen() returns true if the file is open, false if it has been closed or
	// was not able to be created
	bool IsOpen() const throw();

	// GetName() returns the name of the temp file (does not imply that the file
	// is open)
	const string& GetName() const;

	// Write() appends the content of the data byte array to the end of the temp
	// file, if it is open
	void Write(const ByteArray& data) throw(std::runtime_error);

private:

	int fd;
	string fileName;
};

typedef TempFile::Ptr TempFilePtr;

}
}

#endif /* LQRSMACTEMPFILE_H_ */
