LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
from_string.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_UTILS_FROM_STRING_INCLUDED
28 #define LBANN_UTILS_FROM_STRING_INCLUDED
29 
30 #include <algorithm>
31 #include <string>
32 
33 namespace lbann {
34 namespace utils {
35 
50 template <typename T>
51 T from_string(std::string const& str);
52 
53 inline std::string from_string(std::string&& str) { return std::move(str); }
54 
55 template <>
56 inline std::string from_string<std::string>(std::string const& str)
57 {
58  return str;
59 }
60 
61 template <>
62 inline int from_string<int>(std::string const& str)
63 {
64  return std::stoi(str);
65 }
66 
67 template <>
68 inline long from_string<long>(std::string const& str)
69 {
70  return std::stol(str);
71 }
72 
73 template <>
74 inline long long from_string<long long>(std::string const& str)
75 {
76  return std::stoll(str);
77 }
78 
79 template <>
80 inline unsigned long from_string<unsigned long>(std::string const& str)
81 {
82  return std::stoul(str);
83 }
84 
85 template <>
86 inline unsigned long long
87 from_string<unsigned long long>(std::string const& str)
88 {
89  return std::stoull(str);
90 }
91 
92 template <>
93 inline float from_string<float>(std::string const& str)
94 {
95  return std::stof(str);
96 }
97 
98 template <>
99 inline double from_string<double>(std::string const& str)
100 {
101  return std::stod(str);
102 }
103 
104 template <>
105 inline long double from_string<long double>(std::string const& str)
106 {
107  return std::stold(str);
108 }
109 
110 template <>
111 inline bool from_string<bool>(std::string const& str)
112 {
113  auto upcase = [](std::string s) {
114  std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
115  return std::toupper(c);
116  });
117  return s;
118  };
119  auto upper = upcase(str);
120  if (upper == "TRUE")
121  return true;
122  else if (upper == "FALSE")
123  return false;
124  else
125  return from_string<int>(str);
126 }
127 
128 } // namespace utils
129 } // namespace lbann
130 #endif // LBANN_UTILS_FROM_STRING_INCLUDED
long from_string< long >(std::string const &str)
Definition: from_string.hpp:68
double from_string< double >(std::string const &str)
Definition: from_string.hpp:99
long double from_string< long double >(std::string const &str)
T from_string(std::string const &str)
An exceedingly simple implementation of boost::lexical_cast, e.g.
bool from_string< bool >(std::string const &str)
int from_string< int >(std::string const &str)
Definition: from_string.hpp:62
unsigned long long from_string< unsigned long long >(std::string const &str)
Definition: from_string.hpp:87
long long from_string< long long >(std::string const &str)
Definition: from_string.hpp:74
float from_string< float >(std::string const &str)
Definition: from_string.hpp:93
unsigned long from_string< unsigned long >(std::string const &str)
Definition: from_string.hpp:80