From 1d8445b8461f558987067d870f0f11cdc84b4f35 Mon Sep 17 00:00:00 2001 From: manuel Date: Sat, 31 Oct 2009 16:11:26 +0100 Subject: pushing task1 to repo --- task1/utils.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 task1/utils.h (limited to 'task1/utils.h') diff --git a/task1/utils.h b/task1/utils.h new file mode 100644 index 0000000..fefa24e --- /dev/null +++ b/task1/utils.h @@ -0,0 +1,53 @@ +/** + * $Id: utils.h 2 2009-10-31 02:48:23Z l0728348 $ + * + * Copyright 2009 + * + * @author Manuel Mausz (0728348) + * @brief implements some common utility functions, methods and templates + * available in namespace Utils. + */ + +#ifndef UTILS_H +#define UTILS_H + +#include +#include + +namespace Utils +{ + /** + * @brief exception thrown by lexical_cast + */ + class bad_lexical_cast : public std::runtime_error { + public: + /** + * @brief Default exception ctor + * @param what message to pass along + */ + bad_lexical_cast(const std::string& what) + : std::runtime_error(what) + {} + }; + + /** + * @brief simple implementation of boost::lexical_cast + * can be used to convert string to number + * mainly uses operator<< of stringstream + * @param source source value + * @return target target value + **/ + template + Target lexical_cast(const Source& source) + { + Target target; + std::stringstream interpreter; + if(!(interpreter << source && interpreter >> target && interpreter.get() == std::char_traits::eof())) + throw bad_lexical_cast("bad lexical cast"); + return target; + }; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ -- cgit v1.2.3