Boolean.cpp 4.3 KB

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