Integer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-07
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <ls-std/boxing/Integer.hpp>
  10. #include <ls-std/core/exception/IllegalArithmeticOperationException.hpp>
  11. using ls::std::boxing::Integer;
  12. using ls::std::core::Class;
  13. using ls::std::core::IllegalArithmeticOperationException;
  14. using std::stoi;
  15. using std::string;
  16. using std::to_string;
  17. Integer::Integer(int _value) : Integer()
  18. {
  19. this->value = _value;
  20. }
  21. Integer::Integer() : Class("Integer")
  22. {}
  23. Integer::~Integer() noexcept = default;
  24. Integer &Integer::operator=(int _value)
  25. {
  26. this->value = _value;
  27. return *this;
  28. }
  29. int Integer::operator-() const
  30. {
  31. return -this->value;
  32. }
  33. int Integer::operator+(const Integer &_integer) const
  34. {
  35. return this->value + _integer.getValue();
  36. }
  37. int Integer::operator+(int _value) const
  38. {
  39. return this->value + _value;
  40. }
  41. int Integer::operator*(const Integer &_integer) const
  42. {
  43. return this->value * _integer.getValue();
  44. }
  45. int Integer::operator*(int _value) const
  46. {
  47. return this->value * _value;
  48. }
  49. int Integer::operator-(const Integer &_integer) const
  50. {
  51. return this->value - _integer.getValue();
  52. }
  53. int Integer::operator-(int _value) const
  54. {
  55. return this->value - _value;
  56. }
  57. int Integer::operator/(const Integer &_integer) const
  58. {
  59. if (_integer == 0)
  60. {
  61. throw IllegalArithmeticOperationException{"division by zero is not allowed"};
  62. }
  63. return this->value / _integer.getValue();
  64. }
  65. int Integer::operator/(int _value) const
  66. {
  67. if (_value == 0)
  68. {
  69. throw IllegalArithmeticOperationException{"division by zero is not allowed"};
  70. }
  71. return this->value / _value;
  72. }
  73. int Integer::operator%(const Integer &_integer) const
  74. {
  75. return this->value % _integer.getValue();
  76. }
  77. int Integer::operator%(int _value) const
  78. {
  79. return this->value % _value;
  80. }
  81. Integer &Integer::operator+=(const Integer &_integer)
  82. {
  83. this->value += _integer.getValue();
  84. return *this;
  85. }
  86. Integer &Integer::operator+=(int _value)
  87. {
  88. this->value += _value;
  89. return *this;
  90. }
  91. Integer &Integer::operator-=(const Integer &_integer)
  92. {
  93. this->value -= _integer.getValue();
  94. return *this;
  95. }
  96. Integer &Integer::operator-=(int _value)
  97. {
  98. this->value -= _value;
  99. return *this;
  100. }
  101. Integer &Integer::operator*=(const Integer &_integer)
  102. {
  103. this->value *= _integer.getValue();
  104. return *this;
  105. }
  106. Integer &Integer::operator*=(int _value)
  107. {
  108. this->value *= _value;
  109. return *this;
  110. }
  111. Integer &Integer::operator/=(const Integer &_integer)
  112. {
  113. if (_integer == 0)
  114. {
  115. throw IllegalArithmeticOperationException{"division by zero is not allowed"};
  116. }
  117. this->value /= _integer.getValue();
  118. return *this;
  119. }
  120. Integer &Integer::operator/=(int _value)
  121. {
  122. if (_value == 0)
  123. {
  124. throw IllegalArithmeticOperationException{"division by zero is not allowed"};
  125. }
  126. this->value /= _value;
  127. return *this;
  128. }
  129. bool Integer::operator==(const Integer &_integer) const
  130. {
  131. return this->value == _integer.getValue();
  132. }
  133. bool Integer::operator==(int _value) const
  134. {
  135. return this->value == _value;
  136. }
  137. bool Integer::operator!=(const Integer &_integer) const
  138. {
  139. return this->value != _integer.getValue();
  140. }
  141. bool Integer::operator!=(int _value) const
  142. {
  143. return this->value != _value;
  144. }
  145. bool Integer::operator>(const Integer &_integer) const
  146. {
  147. return this->value > _integer.getValue();
  148. }
  149. bool Integer::operator>(int _value) const
  150. {
  151. return this->value > _value;
  152. }
  153. bool Integer::operator>=(const Integer &_integer) const
  154. {
  155. return this->value >= _integer.getValue();
  156. }
  157. bool Integer::operator>=(int _value) const
  158. {
  159. return this->value >= _value;
  160. }
  161. bool Integer::operator<(const Integer &_integer) const
  162. {
  163. return this->value < _integer.getValue();
  164. }
  165. bool Integer::operator<(int _value) const
  166. {
  167. return this->value < _value;
  168. }
  169. bool Integer::operator<=(const Integer &_integer) const
  170. {
  171. return this->value <= _integer.getValue();
  172. }
  173. bool Integer::operator<=(int _value) const
  174. {
  175. return this->value <= _value;
  176. }
  177. bool Integer::operator&&(const Integer &_integer) const
  178. {
  179. return this->value && _integer.getValue();
  180. }
  181. bool Integer::operator&&(int _value) const
  182. {
  183. return this->value && _value;
  184. }
  185. bool Integer::operator&&(bool _expression) const
  186. {
  187. return this->value && _expression;
  188. }
  189. bool Integer::operator||(const Integer &_integer) const
  190. {
  191. return this->value || _integer.getValue();
  192. }
  193. bool Integer::operator||(int _value) const
  194. {
  195. return this->value || _value;
  196. }
  197. bool Integer::operator||(bool _expression) const
  198. {
  199. return this->value || _expression;
  200. }
  201. void Integer::operator++()
  202. {
  203. this->value += 1;
  204. }
  205. void Integer::operator--()
  206. {
  207. this->value -= 1;
  208. }
  209. void Integer::parse(string _parseText)
  210. {
  211. this->value = stoi(_parseText);
  212. }
  213. string Integer::toString()
  214. {
  215. return to_string(this->value);
  216. }
  217. int Integer::getValue() const
  218. {
  219. return this->value;
  220. }