Boolean.cpp 3.8 KB

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