CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
DataStream.cc
Go to the documentation of this file.
2 #include "casm/misc/CASM_math.hh"
3 
4 namespace CASM {
5  namespace DataStream_impl {
6 
7  /*template<typename OutType> template<>
8  OutType DataStreamPromoter<OutType>::promote(OutType a){
9  return a;
10  }*/
11 
12  // PROMOTE TO DOUBLE
13  template<>
14  double DataStreamPromoter<double>::promote(std::string a) {
15  return std::stod(a);
16  }
17  //\End of Double promoters
18 
19  // PROMOTE TO LONG
20  template<>
22  return round(a);
23  }
24 
25  template<>
26  long DataStreamPromoter<long>::promote(std::string a) {
27  return std::stol(a);
28  }
29  //\End of long promoters
30 
31  // PROMOTE TO CHAR
32  template<>
34  throw std::runtime_error(std::string("No viable conversion to type 'char' from double ") + std::to_string(a) + "\n");
35  return round(a);
36  }
37 
38  template<>
39  char DataStreamPromoter<char>::promote(std::string a) {
40  if(a.size())
41  return a[0];
42  return (char)0;
43  }
44 
45  template<>
47  if(toupper(a))
48  return 'T';
49  else
50  return 'F';
51  }
52  //\End of char promoters
53 
54  // PROMOTE TO BOOL
55  template<>
57  return !almost_zero(a);
58  }
59 
60  template<>
61  bool DataStreamPromoter<bool>::promote(std::string a) {
62  //in-place convert 'a' to lower case
63  std::transform(a.begin(), a.end(), a.begin(), tolower);
64 
65  if(a == "true")
66  return true;
67  if(a == "false")
68  return false;
69  throw std::runtime_error("No viable convertion to type 'bool' from string '" + a + "\n");
70  }
71 
72  template<>
74  if(toupper(a) == 'F')
75  return false;
76  if(toupper(a) == 'T')
77  return true;
78  throw std::runtime_error("No viable convertion to type 'bool' from char '" + 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  }
108 }
bool almost_zero(const T &val, double tol=TOL)
If T is not integral, use std::abs(val) < tol;.
Definition: CASM_math.hh:41
Main CASM namespace.
Definition: complete.cpp:8
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: EnumIO.hh:83
int round(double val)
Definition: CASM_math.cc:6