Double.cpp 5.8 KB

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