TBTK
Need a break? Support the development by playing Polarity Puzzles
Real.h
1 /* Copyright 2019 Kristofer Bj√∂rnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_REAL
24 #define COM_DAFER45_TBTK_REAL
25 
27 
28 namespace TBTK{
29 
30 #if TBTK_WRAP_PRIMITIVE_TYPES
31 
34 public:
36  Real(){};
37 
41  constexpr Real(double value) : value(value) {}
42 
49  Real(const std::string &serialization, Serializable::Mode mode);
50 
52  constexpr operator double() const{ return value; };
53 
59  Real& operator=(double rhs){
60  value = rhs;
61 
62  return *this;
63  }
64 
70  Real& operator+=(const Real &rhs){
71  value += rhs.value;
72 
73  return *this;
74  }
75 
81  Real& operator-=(const Real &rhs){
82  value -= rhs.value;
83 
84  return *this;
85  }
86 
92  Real& operator*=(const Real &rhs){
93  value *= rhs.value;
94 
95  return *this;
96  }
97 
103  Real& operator/=(const Real &rhs){
104  value /= rhs.value;
105 
106  return *this;
107  }
108 
116  std::string serialize(Serializable::Mode mode) const;
117 private:
119  double value;
120 };
121 
122 inline Real::Real(const std::string &serialization, Serializable::Mode mode){
123  switch(mode){
124  case Serializable::Mode::JSON:
125  value = stod(serialization);
126  break;
127  default:
128  TBTKExit(
129  "Real::Real()",
130  "Only Serializable::Mode::JSON is supported yet.",
131  ""
132  );
133  }
134 }
135 
136 inline std::string Real::serialize(Serializable::Mode mode) const{
137  switch(mode){
138  case Serializable::Mode::JSON:
139  return std::to_string(value);
140  default:
141  TBTKExit(
142  "Real::serialize()",
143  "Only Serializable::Mode::JSON is supported yet.",
144  ""
145  );
146  }
147 }
148 
149 #else
150  typedef double Real;
151 #endif
152 
153 }; //End of namespace TBTK
154 
155 #endif
Real & operator/=(const Real &rhs)
Definition: Real.h:103
Real()
Definition: Real.h:36
Real & operator+=(const Real &rhs)
Definition: Real.h:70
constexpr Real(double value)
Definition: Real.h:41
Base class for psudo-serializable objects.
Definition: PseudoSerializable.h:31
Real & operator*=(const Real &rhs)
Definition: Real.h:92
Real number.
Definition: Real.h:33
Definition: Boolean.h:32
Real & operator=(double rhs)
Definition: Real.h:59
Mode
Definition: Serializable.h:47
Real & operator-=(const Real &rhs)
Definition: Real.h:81
std::string serialize(Serializable::Mode mode) const
Definition: Real.h:136