Integer.cpp 4.4 KB

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