Boolean.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <algorithm>
  10. #include <ls-std/boxing/Boolean.hpp>
  11. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  12. using ls::std::boxing::Boolean;
  13. using ls::std::core::Class;
  14. using ls::std::core::IllegalArgumentException;
  15. using std::string;
  16. using std::transform;
  17. Boolean::Boolean(bool _value) : Boolean()
  18. {
  19. this->value = _value;
  20. }
  21. Boolean::Boolean() : Class("Boolean")
  22. {}
  23. Boolean::~Boolean() noexcept = default;
  24. Boolean &Boolean::operator=(int _value)
  25. {
  26. this->value = _value;
  27. return *this;
  28. }
  29. Boolean &Boolean::operator=(bool _value)
  30. {
  31. this->value = _value;
  32. return *this;
  33. }
  34. bool Boolean::operator&&(const Boolean &_boolean) const
  35. {
  36. return this->value && _boolean.getValue();
  37. }
  38. bool Boolean::operator&&(bool _value) const
  39. {
  40. return this->value && _value;
  41. }
  42. bool Boolean::operator&&(int _value) const
  43. {
  44. return this->value && _value;
  45. }
  46. bool Boolean::operator||(const Boolean &_boolean) const
  47. {
  48. return this->value || _boolean.getValue();
  49. }
  50. bool Boolean::operator||(bool _value) const
  51. {
  52. return this->value || _value;
  53. }
  54. bool Boolean::operator||(int _value) const
  55. {
  56. return this->value || _value;
  57. }
  58. void Boolean::parse(string _parseText)
  59. {
  60. transform(_parseText.begin(), _parseText.end(), _parseText.begin(), ::tolower);
  61. if (_parseText != this->TRUE_STRING && _parseText != this->FALSE_STRING)
  62. {
  63. throw IllegalArgumentException{_parseText + " is not a valid string representation"};
  64. }
  65. else
  66. {
  67. if (_parseText == this->TRUE_STRING)
  68. {
  69. this->value = true;
  70. }
  71. if (_parseText == this->FALSE_STRING)
  72. {
  73. this->value = false;
  74. }
  75. }
  76. }
  77. string Boolean::toString()
  78. {
  79. return this->_toString();
  80. }
  81. bool Boolean::getValue() const
  82. {
  83. return this->value;
  84. }
  85. bool Boolean::XOR(const Boolean &_leftExpression, const Boolean &_rightExpression)
  86. {
  87. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression.getValue());
  88. }
  89. bool Boolean::XOR(const Boolean &_leftExpression, bool _rightExpression)
  90. {
  91. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  92. }
  93. bool Boolean::XOR(bool _leftExpression, const Boolean &_rightExpression)
  94. {
  95. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression.getValue());
  96. }
  97. bool Boolean::XOR(bool _leftExpression, bool _rightExpression)
  98. {
  99. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  100. }
  101. string Boolean::_toString() const
  102. {
  103. string booleanString{};
  104. if (this->value)
  105. {
  106. booleanString = this->TRUE_STRING;
  107. }
  108. else
  109. {
  110. booleanString = this->FALSE_STRING;
  111. }
  112. return booleanString;
  113. }