TBTK
Need a break? Support the development by playing Polarity Puzzles
Real.h
Go to the documentation of this file.
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
TBTK::Real::operator*=
Real & operator*=(const Real &rhs)
Definition: Real.h:92
TBTK::Real::serialize
std::string serialize(Serializable::Mode mode) const
Definition: Real.h:136
TBTK::Real
Real number.
Definition: Real.h:33
TBTK::Real::operator/=
Real & operator/=(const Real &rhs)
Definition: Real.h:103
TBTK::Real::operator+=
Real & operator+=(const Real &rhs)
Definition: Real.h:70
TBTK::Real::operator-=
Real & operator-=(const Real &rhs)
Definition: Real.h:81
TBTK::Real::Real
constexpr Real(double value)
Definition: Real.h:41
PseudoSerializable.h
Base class for psudo-serializable objects.
TBTK::Serializable::Mode
Mode
Definition: Serializable.h:47
TBTK::Real::operator=
Real & operator=(double rhs)
Definition: Real.h:59
TBTK::PseudoSerializable
Definition: PseudoSerializable.h:31
TBTK::Real::Real
Real()
Definition: Real.h:36