Integer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-07
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <ls_std/boxing/Integer.hpp>
  10. #include <ls_std/exception/IllegalArithmeticOperationException.hpp>
  11. ls_std::Integer::Integer(int _value)
  12. : ls_std::Class("Integer"),
  13. value(_value)
  14. {}
  15. ls_std::Integer::Integer() : ls_std::Class("Integer")
  16. {}
  17. ls_std::Integer::operator int() const
  18. {
  19. return this->value;
  20. }
  21. ls_std::Integer &ls_std::Integer::operator=(int _value)
  22. {
  23. this->value = _value;
  24. return *this;
  25. }
  26. int ls_std::Integer::operator-() const
  27. {
  28. return -this->value;
  29. }
  30. int ls_std::Integer::operator+(const ls_std::Integer &_integer) const
  31. {
  32. return this->value + _integer;
  33. }
  34. int ls_std::Integer::operator+(int _value) const
  35. {
  36. return this->value + _value;
  37. }
  38. int ls_std::Integer::operator*(const ls_std::Integer &_integer) const
  39. {
  40. return this->value * _integer;
  41. }
  42. int ls_std::Integer::operator*(int _value) const
  43. {
  44. return this->value * _value;
  45. }
  46. int ls_std::Integer::operator-(const ls_std::Integer &_integer) const
  47. {
  48. return this->value - _integer;
  49. }
  50. int ls_std::Integer::operator-(int _value) const
  51. {
  52. return this->value - _value;
  53. }
  54. int ls_std::Integer::operator/(const ls_std::Integer &_integer) const
  55. {
  56. if (_integer == 0)
  57. {
  58. throw ls_std::IllegalArithmeticOperationException{};
  59. }
  60. return this->value / _integer;
  61. }
  62. int ls_std::Integer::operator/(int _value) const
  63. {
  64. if (_value == 0)
  65. {
  66. throw ls_std::IllegalArithmeticOperationException{};
  67. }
  68. return this->value / _value;
  69. }
  70. int ls_std::Integer::operator%(const ls_std::Integer &_integer) const
  71. {
  72. return this->value % _integer;
  73. }
  74. int ls_std::Integer::operator%(int _value) const
  75. {
  76. return this->value % _value;
  77. }
  78. ls_std::Integer &ls_std::Integer::operator+=(const ls_std::Integer &_integer)
  79. {
  80. this->value += _integer;
  81. return *this;
  82. }
  83. ls_std::Integer &ls_std::Integer::operator+=(int _value)
  84. {
  85. this->value += _value;
  86. return *this;
  87. }
  88. ls_std::Integer &ls_std::Integer::operator-=(const ls_std::Integer &_integer)
  89. {
  90. this->value -= _integer;
  91. return *this;
  92. }
  93. ls_std::Integer &ls_std::Integer::operator-=(int _value)
  94. {
  95. this->value -= _value;
  96. return *this;
  97. }
  98. ls_std::Integer &ls_std::Integer::operator*=(const ls_std::Integer &_integer)
  99. {
  100. this->value *= _integer;
  101. return *this;
  102. }
  103. ls_std::Integer &ls_std::Integer::operator*=(int _value)
  104. {
  105. this->value *= _value;
  106. return *this;
  107. }
  108. ls_std::Integer &ls_std::Integer::operator/=(const ls_std::Integer &_integer)
  109. {
  110. if (_integer == 0)
  111. {
  112. throw ls_std::IllegalArithmeticOperationException{};
  113. }
  114. this->value /= _integer;
  115. return *this;
  116. }
  117. ls_std::Integer &ls_std::Integer::operator/=(int _value)
  118. {
  119. if (_value == 0)
  120. {
  121. throw ls_std::IllegalArithmeticOperationException{};
  122. }
  123. this->value /= _value;
  124. return *this;
  125. }
  126. bool ls_std::Integer::operator==(const ls_std::Integer &_integer) const
  127. {
  128. return this->value == _integer;
  129. }
  130. bool ls_std::Integer::operator==(int _value) const
  131. {
  132. return this->value == _value;
  133. }
  134. bool ls_std::Integer::operator!=(const ls_std::Integer &_integer) const
  135. {
  136. return this->value != _integer;
  137. }
  138. bool ls_std::Integer::operator!=(int _value) const
  139. {
  140. return this->value != _value;
  141. }
  142. bool ls_std::Integer::operator>(const ls_std::Integer &_integer) const
  143. {
  144. return this->value > _integer;
  145. }
  146. bool ls_std::Integer::operator>(int _value) const
  147. {
  148. return this->value > _value;
  149. }
  150. bool ls_std::Integer::operator>=(const ls_std::Integer &_integer) const
  151. {
  152. return this->value >= _integer;
  153. }
  154. bool ls_std::Integer::operator>=(int _value) const
  155. {
  156. return this->value >= _value;
  157. }
  158. bool ls_std::Integer::operator<(const ls_std::Integer &_integer) const
  159. {
  160. return this->value < _integer;
  161. }
  162. bool ls_std::Integer::operator<(int _value) const
  163. {
  164. return this->value < _value;
  165. }
  166. bool ls_std::Integer::operator<=(const ls_std::Integer &_integer) const
  167. {
  168. return this->value <= _integer;
  169. }
  170. bool ls_std::Integer::operator<=(int _value) const
  171. {
  172. return this->value <= _value;
  173. }
  174. bool ls_std::Integer::operator&&(const ls_std::Integer &_integer) const
  175. {
  176. return this->value && _integer;
  177. }
  178. bool ls_std::Integer::operator&&(int _value) const
  179. {
  180. return this->value && _value;
  181. }
  182. bool ls_std::Integer::operator&&(bool _expression) const
  183. {
  184. return this->value && _expression;
  185. }
  186. bool ls_std::Integer::operator||(const ls_std::Integer &_integer) const
  187. {
  188. return this->value || _integer;
  189. }
  190. bool ls_std::Integer::operator||(int _value) const
  191. {
  192. return this->value || _value;
  193. }
  194. bool ls_std::Integer::operator||(bool _expression) const
  195. {
  196. return this->value || _expression;
  197. }
  198. void ls_std::Integer::operator++()
  199. {
  200. this->value += 1;
  201. }
  202. void ls_std::Integer::operator--()
  203. {
  204. this->value -= 1;
  205. }
  206. ls_std::byte_field ls_std::Integer::load()
  207. {
  208. ls_std::byte_field data{};
  209. if (this->storable != nullptr && this->serializable != nullptr)
  210. {
  211. data = this->storable->load();
  212. this->serializable->unmarshal(data);
  213. }
  214. return data;
  215. }
  216. ls_std::byte_field ls_std::Integer::marshal()
  217. {
  218. ls_std::byte_field data{};
  219. if (this->serializable != nullptr)
  220. {
  221. data = this->serializable->marshal();
  222. }
  223. return data;
  224. }
  225. void ls_std::Integer::parse(std::string _parseText)
  226. {
  227. this->value = std::stoi(_parseText);
  228. }
  229. void ls_std::Integer::save(const ls_std::byte_field &_data)
  230. {
  231. if (this->serializable != nullptr)
  232. {
  233. if (_data.empty())
  234. {
  235. this->storable->save(this->serializable->marshal());
  236. }
  237. else
  238. {
  239. this->storable->save(_data);
  240. }
  241. }
  242. }
  243. std::string ls_std::Integer::toString()
  244. {
  245. return std::to_string(this->value);
  246. }
  247. void ls_std::Integer::unmarshal(const ls_std::byte_field &_data)
  248. {
  249. if (this->serializable != nullptr)
  250. {
  251. this->serializable->unmarshal(_data);
  252. }
  253. }
  254. int ls_std::Integer::getValue() const
  255. {
  256. return this->value;
  257. }
  258. void ls_std::Integer::setSerializable(std::shared_ptr<ISerializable> _serializable)
  259. {
  260. this->serializable = std::move(_serializable);
  261. }
  262. void ls_std::Integer::setStorable(std::shared_ptr<IStorable> _storable)
  263. {
  264. this->storable = std::move(_storable);
  265. }