/*
 * @file  lexical_cast.h
 *
 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
 * Copyright (C) 2008  NTT COMWARE Corporation.
 *
 * This program 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 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
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *      
 **********************************************************************/

#ifndef __LEXICAL_CAST_H__
#define __LEXICAL_CAST_H__

#include <string>
#include <sstream>
#include <typeinfo>

namespace l7vs
{
	class bad_lexical_cast : public std::bad_cast
	{
	public:
		bad_lexical_cast() {}
	};

	template <typename T>
	T lexical_cast(const std::string& s)
	{
		std::stringstream ss;
		T d;
		ss << s.c_str();
		while( !ss.eof() ){
			ss >> d;
			if(ss.fail()) {
				throw bad_lexical_cast();
			}
		}
		return d;
	}
}

#endif	//__LEXICAL_CAST_H__
