Boolean.cpp 2.9 KB

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