Float.cpp 5.7 KB

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