summaryrefslogtreecommitdiffstats
path: root/task1/utils.h
blob: fefa24ed06886c0e7c22e7da2915498d841a1a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 <sstream>
#include <stdexcept>

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 <typename Target, typename Source>
  Target lexical_cast(const Source& source)
  {
      Target target;
      std::stringstream interpreter;
      if(!(interpreter << source && interpreter >> target && interpreter.get() == std::char_traits<char>::eof()))
        throw bad_lexical_cast("bad lexical cast");
      return target;
  };
};

#endif

/* vim: set et sw=2 ts=2: */