diff options
Diffstat (limited to 'task1/utils.h')
| -rw-r--r-- | task1/utils.h | 53 |
1 files changed, 53 insertions, 0 deletions
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 @@ | |||
| 1 | /** | ||
| 2 | * $Id: utils.h 2 2009-10-31 02:48:23Z l0728348 $ | ||
| 3 | * | ||
| 4 | * Copyright 2009 | ||
| 5 | * | ||
| 6 | * @author Manuel Mausz (0728348) | ||
| 7 | * @brief implements some common utility functions, methods and templates | ||
| 8 | * available in namespace Utils. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #ifndef UTILS_H | ||
| 12 | #define UTILS_H | ||
| 13 | |||
| 14 | #include <sstream> | ||
| 15 | #include <stdexcept> | ||
| 16 | |||
| 17 | namespace Utils | ||
| 18 | { | ||
| 19 | /** | ||
| 20 | * @brief exception thrown by lexical_cast | ||
| 21 | */ | ||
| 22 | class bad_lexical_cast : public std::runtime_error { | ||
| 23 | public: | ||
| 24 | /** | ||
| 25 | * @brief Default exception ctor | ||
| 26 | * @param what message to pass along | ||
| 27 | */ | ||
| 28 | bad_lexical_cast(const std::string& what) | ||
| 29 | : std::runtime_error(what) | ||
| 30 | {} | ||
| 31 | }; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * @brief simple implementation of boost::lexical_cast | ||
| 35 | * can be used to convert string to number | ||
| 36 | * mainly uses operator<< of stringstream | ||
| 37 | * @param source source value | ||
| 38 | * @return target target value | ||
| 39 | **/ | ||
| 40 | template <typename Target, typename Source> | ||
| 41 | Target lexical_cast(const Source& source) | ||
| 42 | { | ||
| 43 | Target target; | ||
| 44 | std::stringstream interpreter; | ||
| 45 | if(!(interpreter << source && interpreter >> target && interpreter.get() == std::char_traits<char>::eof())) | ||
| 46 | throw bad_lexical_cast("bad lexical cast"); | ||
| 47 | return target; | ||
| 48 | }; | ||
| 49 | }; | ||
| 50 | |||
| 51 | #endif | ||
| 52 | |||
| 53 | /* vim: set et sw=2 ts=2: */ | ||
