Belle II Software development
Conversion.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/utilities/Conversion.h>
10#include <stdexcept>
11
12namespace Belle2 {
23 template<>
24 float convertString(const std::string& str)
25 {
26 std::string::size_type n;
27 float number = std::stof(str, &n);
28 if (n != str.size()) {
29 throw std::invalid_argument("Could only parse a part of the given string " + str);
30 }
31 return number;
32 }
33
39 template<>
40 double convertString(const std::string& str)
41 {
42 std::string::size_type n;
43 double number = std::stod(str, &n);
44 if (n != str.size()) {
45 throw std::invalid_argument("Could only parse a part of the given string " + str);
46 }
47 return number;
48 }
49
55 template<>
56 long double convertString(const std::string& str)
57 {
58 std::string::size_type n;
59 long double number = std::stold(str, &n);
60 if (n != str.size()) {
61 throw std::invalid_argument("Could only parse a part of the given string " + str);
62 }
63 return number;
64 }
65
71 template<>
72 int convertString(const std::string& str)
73 {
74 std::string::size_type n;
75 int number = std::stoi(str, &n);
76 if (n != str.size()) {
77 throw std::invalid_argument("Could only parse a part of the given string " + str);
78 }
79 return number;
80 }
81
87 template<>
88 long int convertString(const std::string& str)
89 {
90 std::string::size_type n;
91 long int number = std::stol(str, &n);
92 if (n != str.size()) {
93 throw std::invalid_argument("Could only parse a part of the given string " + str);
94 }
95 return number;
96 }
97
103 template<>
104 unsigned long int convertString(const std::string& str)
105 {
106 std::string::size_type n;
107 unsigned long int number = std::stoul(str, &n);
108 if (n != str.size()) {
109 throw std::invalid_argument("Could only parse a part of the given string " + str);
110 }
111 return number;
112 }
113
115}
116
117
118
T convertString(const std::string &str)
Converts a string to type T (one of float, double, long double, int, long int, unsigned long int).
Abstract base class for different kinds of events.