Float.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2020-09-04
  7. *
  8. * */
  9. #include <cmath>
  10. #include "Float.hpp"
  11. ls_std::Float::Float() : Class("Float"),
  12. epsilon(0.00001f)
  13. {}
  14. ls_std::Float::Float(float _value) : Class("Float"),
  15. epsilon(0.00001f),
  16. value(_value)
  17. {}
  18. ls_std::Float::operator float() const {
  19. return this->value;
  20. }
  21. ls_std::Float & ls_std::Float::operator=(float _value) {
  22. this->value = _value;
  23. return *this;
  24. }
  25. float ls_std::Float::operator-() const {
  26. return -this->value;
  27. }
  28. float ls_std::Float::operator+(const Float &_float) const {
  29. return this->value + _float;
  30. }
  31. float ls_std::Float::operator+(float _value) const {
  32. return this->value + _value;
  33. }
  34. float ls_std::Float::operator*(const Float &_float) const {
  35. return this->value * _float;
  36. }
  37. float ls_std::Float::operator*(float _value) const {
  38. return this->value * _value;
  39. }
  40. float ls_std::Float::operator-(const Float &_float) const {
  41. return this->value - _float;
  42. }
  43. float ls_std::Float::operator-(float _value) const {
  44. return this->value - _value;
  45. }
  46. float ls_std::Float::operator/(const Float &_float) const {
  47. return this->value / _float;
  48. }
  49. float ls_std::Float::operator/(float _value) const {
  50. return this->value / _value;
  51. }
  52. ls_std::Float & ls_std::Float::operator+=(const Float &_float) {
  53. this->value += _float;
  54. return *this;
  55. }
  56. ls_std::Float & ls_std::Float::operator+=(float _value) {
  57. this->value += _value;
  58. return *this;
  59. }
  60. ls_std::Float & ls_std::Float::operator-=(const Float &_float) {
  61. this->value -= _float;
  62. return *this;
  63. }
  64. ls_std::Float & ls_std::Float::operator-=(float _value) {
  65. this->value -= _value;
  66. return *this;
  67. }
  68. ls_std::Float & ls_std::Float::operator*=(const Float &_float) {
  69. this->value *= _float;
  70. return *this;
  71. }
  72. ls_std::Float & ls_std::Float::operator*=(float _value) {
  73. this->value *= _value;
  74. return *this;
  75. }
  76. ls_std::Float & ls_std::Float::operator/=(const Float &_float) {
  77. this->value /= _float;
  78. return *this;
  79. }
  80. ls_std::Float & ls_std::Float::operator/=(float _value) {
  81. this->value /= _value;
  82. return *this;
  83. }
  84. bool ls_std::Float::operator==(const Float &_float) const {
  85. return std::fabs(this->value - _float) < this->epsilon;
  86. }
  87. bool ls_std::Float::operator==(float _value) const {
  88. return std::fabs(this->value - _value) < this->epsilon;
  89. }
  90. bool ls_std::Float::operator!=(const Float &_float) const {
  91. return std::fabs(this->value - _float) >= this->epsilon;
  92. }
  93. bool ls_std::Float::operator!=(float _value) const {
  94. return std::fabs(this->value - _value) >= this->epsilon;
  95. }
  96. bool ls_std::Float::operator>(const Float &_float) const {
  97. return this->value > _float;
  98. }
  99. bool ls_std::Float::operator>(float _value) const {
  100. return this->value > _value;
  101. }
  102. bool ls_std::Float::operator>=(const Float &_float) const {
  103. return this->value >= _float;
  104. }
  105. bool ls_std::Float::operator>=(float _value) const {
  106. return this->value >= _value;
  107. }
  108. bool ls_std::Float::operator<(const Float &_float) const {
  109. return this->value < _float;
  110. }
  111. bool ls_std::Float::operator<(float _value) const {
  112. return this->value < _value;
  113. }
  114. bool ls_std::Float::operator<=(const Float &_float) const {
  115. return this->value <= _float;
  116. }
  117. bool ls_std::Float::operator<=(float _value) const {
  118. return this->value <= _value;
  119. }
  120. void ls_std::Float::operator++() {
  121. this->value += 1.0f;
  122. }
  123. void ls_std::Float::operator--() {
  124. this->value -= 1.0f;
  125. }
  126. ls_std::byte_field ls_std::Float::load()
  127. {
  128. ls_std::byte_field data {};
  129. if(this->storable != nullptr && this->serializable != nullptr) {
  130. data = this->storable->load();
  131. this->serializable->unmarshal(data);
  132. }
  133. return data;
  134. }
  135. ls_std::byte_field ls_std::Float::marshal()
  136. {
  137. ls_std::byte_field data {};
  138. if(this->serializable != nullptr) {
  139. data = this->serializable->marshal();
  140. }
  141. return data;
  142. }
  143. void ls_std::Float::parse(std::string _parseText) {
  144. this->value = std::stof(_parseText);
  145. }
  146. void ls_std::Float::save(const ls_std::byte_field& _data)
  147. {
  148. if(this->serializable != nullptr) {
  149. if(_data.empty()) {
  150. this->storable->save(this->serializable->marshal());
  151. } else {
  152. this->storable->save(_data);
  153. }
  154. }
  155. }
  156. std::string ls_std::Float::toString() {
  157. return std::to_string(this->value);
  158. }
  159. void ls_std::Float::unmarshal(const ls_std::byte_field& _data)
  160. {
  161. if(this->serializable != nullptr) {
  162. this->serializable->unmarshal(_data);
  163. }
  164. }
  165. float ls_std::Float::getEpsilon() {
  166. return this->epsilon;
  167. }
  168. float ls_std::Float::getValue() {
  169. return this->value;
  170. }
  171. void ls_std::Float::setEpsilon(float _epsilon) {
  172. this->epsilon = _epsilon;
  173. }
  174. void ls_std::Float::setSerializable(std::shared_ptr<ISerializable> _serializable) {
  175. this->serializable = std::move(_serializable);
  176. }
  177. void ls_std::Float::setStorable(std::shared_ptr<IStorable> _storable) {
  178. this->storable = std::move(_storable);
  179. }