TBTK
Need a break? Support the development by playing Polarity Puzzles
Integer.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_INTEGER
24 #define COM_DAFER45_TBTK_INTEGER
25 
27 
28 #ifndef TBTK_DISABLE_NLOHMANN_JSON
29 # include "TBTK/json.hpp"
30 #endif
31 
32 namespace TBTK{
33 
34 #if TBTK_WRAP_PRIMITIVE_TYPES
35 
38 public:
40  Integer(){};
41 
45  constexpr Integer(int value) : value(value) {}
46 
53  Integer(const std::string &serialization, Serializable::Mode mode);
54 
56  constexpr operator int() const{ return value; };
57 
63  Integer& operator=(int rhs){
64  value = rhs;
65 
66  return *this;
67  }
68 
74  Integer& operator+=(const Integer &rhs){
75  value += rhs.value;
76 
77  return *this;
78  }
79 
85  Integer& operator-=(const Integer &rhs){
86  value -= rhs.value;
87 
88  return *this;
89  }
90 
96  Integer& operator*=(const Integer &rhs){
97  value *= rhs.value;
98 
99  return *this;
100  }
101 
107  Integer& operator/=(const Integer &rhs){
108  value /= rhs.value;
109 
110  return *this;
111  }
112 
117  value++;
118 
119  return *this;
120  }
121 
126  Integer previous(*this);
127  operator++();
128 
129  return previous;
130  }
131 
136  value--;
137 
138  return *this;
139  }
140 
145  Integer previous(*this);
146  operator--();
147 
148  return previous;
149  }
150 
158  std::string serialize(Serializable::Mode mode) const;
159 
160 #ifndef TBTK_DISABLE_NLOHMANN_JSON
161 
165  friend void to_json(nlohmann::json &j, const Integer &integer){
166  nlohmann::to_json(j, integer.value);
167  }
168 
173  friend void from_json(const nlohmann::json &j, Integer &integer){
174  nlohmann::from_json(j, integer.value);
175  }
176 #endif
177 private:
179  int value;
180 };
181 
183  const std::string &serialization,
184  Serializable::Mode mode
185 ){
186  switch(mode){
187  case Serializable::Mode::JSON:
188  value = stoi(serialization);
189  break;
190  default:
191  TBTKExit(
192  "Integer::Integer()",
193  "Only Serializable::Mode::JSON is supported yet.",
194  ""
195  );
196  }
197 }
198 
199 inline std::string Integer::serialize(Serializable::Mode mode) const{
200  switch(mode){
201  case Serializable::Mode::JSON:
202  return std::to_string(value);
203  default:
204  TBTKExit(
205  "Integer::serialize()",
206  "Only Serializable::Mode::JSON is supported yet.",
207  ""
208  );
209  }
210 }
211 
212 #else
213  typedef int Integer;
214 #endif
215 
216 }; //End of namespace TBTK
217 
218 #endif
Integer & operator*=(const Integer &rhs)
Definition: Integer.h:96
Integer number.
Definition: Integer.h:37
Integer()
Definition: Integer.h:40
friend void from_json(const nlohmann::json &j, Integer &integer)
Definition: Integer.h:173
Integer & operator+=(const Integer &rhs)
Definition: Integer.h:74
Base class for psudo-serializable objects.
Integer & operator--()
Definition: Integer.h:135
Integer & operator=(int rhs)
Definition: Integer.h:63
Integer & operator/=(const Integer &rhs)
Definition: Integer.h:107
Integer & operator-=(const Integer &rhs)
Definition: Integer.h:85
Definition: PseudoSerializable.h:31
Integer operator--(int)
Definition: Integer.h:144
Integer & operator++()
Definition: Integer.h:116
Definition: Boolean.h:32
Integer operator++(int)
Definition: Integer.h:125
Mode
Definition: Serializable.h:47
std::string serialize(Serializable::Mode mode) const
Definition: Integer.h:199
friend void to_json(nlohmann::json &j, const Integer &integer)
Definition: Integer.h:165
constexpr Integer(int value)
Definition: Integer.h:45