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