CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DataStream.cc
Go to the documentation of this file.
2 
3 #include "casm/misc/CASM_math.hh"
4 
5 namespace CASM {
6 namespace DataStream_impl {
7 
8 /*template<typename OutType> template<>
9 OutType DataStreamPromoter<OutType>::promote(OutType a){
10  return a;
11  }*/
12 
13 // PROMOTE TO DOUBLE
14 template <>
15 double DataStreamPromoter<double>::promote(std::string a) {
16  return std::stod(a);
17 }
18 //\End of Double promoters
19 
20 // PROMOTE TO LONG
21 template <>
23  return round(a);
24 }
25 
26 template <>
27 long DataStreamPromoter<long>::promote(std::string a) {
28  return std::stol(a);
29 }
30 //\End of long promoters
31 
32 // PROMOTE TO CHAR
33 template <>
35  throw std::runtime_error(
36  std::string("No viable conversion to type 'char' from double ") +
37  std::to_string(a) + "\n");
38  return round(a);
39 }
40 
41 template <>
42 char DataStreamPromoter<char>::promote(std::string a) {
43  if (a.size()) return a[0];
44  return (char)0;
45 }
46 
47 template <>
49  if (toupper(a))
50  return 'T';
51  else
52  return 'F';
53 }
54 //\End of char promoters
55 
56 // PROMOTE TO BOOL
57 template <>
59  return !almost_zero(a);
60 }
61 
62 template <>
63 bool DataStreamPromoter<bool>::promote(std::string a) {
64  // in-place convert 'a' to lower case
65  std::transform(a.begin(), a.end(), a.begin(), tolower);
66 
67  if (a == "true") return true;
68  if (a == "false") return false;
69  throw std::runtime_error("No viable convertion to type 'bool' from string '" +
70  a + "\n");
71 }
72 
73 template <>
75  if (toupper(a) == 'F') return false;
76  if (toupper(a) == 'T') return true;
77  throw std::runtime_error("No viable convertion to type 'bool' from char '" +
78  std::string(1, a) + "\n");
79 }
80 //\End of bool promoters
81 
82 // PROMOTE TO STRING
83 template <>
85  return std::to_string(a);
86 }
87 
88 template <>
90  return std::to_string(a);
91 }
92 
93 template <>
95  return std::string(1, a);
96 }
97 
98 template <>
100  if (a)
101  return "true";
102  else
103  return "false";
104 }
105 //\End of String promoters
106 
107 } // namespace DataStream_impl
108 } // namespace CASM
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
Eigen::CwiseUnaryOp< decltype(Local::round_l< typename Derived::Scalar >), const Derived > round(const Eigen::MatrixBase< Derived > &val)
Round Eigen::MatrixXd.
Main CASM namespace.
Definition: APICommand.hh:8
bool almost_zero(const T &val, double tol=TOL)
If T is not integral, use std::abs(val) < tol;.
Definition: CASM_math.hh:104