Boolean.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2020-09-04
  7. *
  8. * */
  9. #include <algorithm>
  10. #include "Boolean.hpp"
  11. #include "../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::marshal() {
  56. ls_std::byte_field data {};
  57. if(this->serializable != nullptr) {
  58. data = this->serializable->marshal();
  59. }
  60. return data;
  61. }
  62. void ls_std::Boolean::parse(std::string _parseText)
  63. {
  64. std::transform(_parseText.begin(), _parseText.end(), _parseText.begin(), ::tolower);
  65. if(_parseText != this->TRUE_STRING && _parseText != this->FALSE_STRING) {
  66. throw ls_std::IllegalArgumentException {};
  67. } else {
  68. if(_parseText == this->TRUE_STRING) {
  69. this->value = true;
  70. }
  71. if(_parseText == this->FALSE_STRING) {
  72. this->value = false;
  73. }
  74. }
  75. }
  76. std::string ls_std::Boolean::toString()
  77. {
  78. return this->_toString();
  79. }
  80. void ls_std::Boolean::unmarshal(const ls_std::byte_field &_data) {
  81. if(this->serializable != nullptr) {
  82. this->serializable->unmarshal(_data);
  83. }
  84. }
  85. bool ls_std::Boolean::getValue() {
  86. return this->value;
  87. }
  88. void ls_std::Boolean::setSerializable(std::shared_ptr<ISerializable> _serializable) {
  89. this->serializable = std::move(_serializable);
  90. }
  91. bool ls_std::Boolean::XOR(const Boolean &_leftExpression, const Boolean &_rightExpression) {
  92. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  93. }
  94. bool ls_std::Boolean::XOR(const Boolean &_leftExpression, bool _rightExpression) {
  95. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  96. }
  97. bool ls_std::Boolean::XOR(bool _leftExpression, const Boolean &_rightExpression) {
  98. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  99. }
  100. bool ls_std::Boolean::XOR(bool _leftExpression, bool _rightExpression) {
  101. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  102. }
  103. std::string ls_std::Boolean::_toString() const
  104. {
  105. std::string booleanString {};
  106. if(this->value) {
  107. booleanString = this->TRUE_STRING;
  108. } else {
  109. booleanString = this->FALSE_STRING;
  110. }
  111. return booleanString;
  112. }