/*
 * The UDS Collection
 * Copyright (C) 2000, 2001  Julian v. Bock
 *
 * 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 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	(See file COPYING for more information)
 */

#ifndef _uds_strstream_hh
#define _uds_strstream_hh


// includes

#include <uds/stdexcpt.hh>
#include <string>
#include <strstream>

using namespace std;


namespace uds
{

/** strstream that provides an operator string and deletes the buffer
	always automatically
	\author Julian v. Bock */
class StrStream : public strstream
{
public:
	/** destructor */
	virtual
	~StrStream();
	
	/** provides access to the buffer. (The buffer is not freezed)
		\return string buffer */
	char*
	str();
	
	/** provides access to the buffer. (The buffer is not freezed)
		\return string buffer */
	string
	String();
	
	friend ostream& operator << ( ostream&, StrStream& );
};

ostream&
operator << ( ostream& out, uds::StrStream& s );


/** converts a variable to a string
	\param v value
	\return string representation of v */
template < class Tp >
string
StringVar( const Tp& v )
{
	StrStream s;
	s<<v;
	return s.String();
}

/** converts one type to another. If the 'strict' flag is set and not the
	whole string could be converted, an exception is thrown.
	\param v value
	\param strict 'strict' flag
	\return converted value
	\arg throws runtime_error */
template < class Ret, class Tp >
Ret
VarConv( const Tp& v, bool strict = true )
{
	StrStream s;
	s<<v;
	Ret ret;
	s>>ret;
	
	if ( strict && s.tellg() != s.tellp() )
		Throw( runtime_error( "unable to convert whole string to type" ) );
	
	return ret;
}

}

#endif // _uds_strstream_hh
