Boolean.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2023-03-21
  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. /*
  18. * @doku("method", "Boolean::Boolean")
  19. *
  20. * @description("This is an overloaded constructor of the Boolean class.")
  21. * @parameter("bool", "_value", "This is the value, which you'd like to box. It can be either true or false.")
  22. *
  23. * @!doku
  24. * */
  25. Boolean::Boolean(bool _value) : Boolean()
  26. {
  27. this->value = _value;
  28. }
  29. /*
  30. * @doku("method", "Boolean::Boolean")
  31. *
  32. * @description("This is the default constructor of the Boolean class.")
  33. *
  34. * @!doku
  35. * */
  36. Boolean::Boolean() : Class("Boolean")
  37. {}
  38. Boolean::~Boolean() noexcept = default;
  39. Boolean &Boolean::operator=(int _value)
  40. {
  41. this->value = _value;
  42. return *this;
  43. }
  44. Boolean &Boolean::operator=(bool _value)
  45. {
  46. this->value = _value;
  47. return *this;
  48. }
  49. bool Boolean::operator&&(const Boolean &_boolean) const
  50. {
  51. return this->value && _boolean.getValue();
  52. }
  53. bool Boolean::operator&&(bool _value) const
  54. {
  55. return this->value && _value;
  56. }
  57. bool Boolean::operator&&(int _value) const
  58. {
  59. return this->value && _value;
  60. }
  61. bool Boolean::operator||(const Boolean &_boolean) const
  62. {
  63. return this->value || _boolean.getValue();
  64. }
  65. bool Boolean::operator||(bool _value) const
  66. {
  67. return this->value || _value;
  68. }
  69. bool Boolean::operator||(int _value) const
  70. {
  71. return this->value || _value;
  72. }
  73. /*
  74. * @doku("method", "Boolean::parse")
  75. *
  76. * @description("This method parses a string and converts it to a boolean value. This boolean value will be assigned to the internal boolean member of this class, which can then be accessed via getter method.")
  77. * @parameter("::std::string", "_parseText", "This is the boolean string representation and should be valid by being either "true" or "false" (non case-sensitive). If the string representation is invalid an IllegalArgumentException will be thrown.")
  78. * @return("void", "nothing")
  79. *
  80. * @!doku
  81. * */
  82. void Boolean::parse(string _parseText)
  83. {
  84. transform(_parseText.begin(), _parseText.end(), _parseText.begin(), ::tolower);
  85. if (_parseText != this->TRUE_STRING && _parseText != this->FALSE_STRING)
  86. {
  87. throw IllegalArgumentException{_parseText + " is not a valid string representation"};
  88. }
  89. else
  90. {
  91. if (_parseText == this->TRUE_STRING)
  92. {
  93. this->value = true;
  94. }
  95. if (_parseText == this->FALSE_STRING)
  96. {
  97. this->value = false;
  98. }
  99. }
  100. }
  101. string Boolean::toString()
  102. {
  103. return this->_toString();
  104. }
  105. bool Boolean::getValue() const
  106. {
  107. return this->value;
  108. }
  109. bool Boolean::XOR(const Boolean &_leftExpression, const Boolean &_rightExpression)
  110. {
  111. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression.getValue());
  112. }
  113. bool Boolean::XOR(const Boolean &_leftExpression, bool _rightExpression)
  114. {
  115. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  116. }
  117. bool Boolean::XOR(bool _leftExpression, const Boolean &_rightExpression)
  118. {
  119. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression.getValue());
  120. }
  121. bool Boolean::XOR(bool _leftExpression, bool _rightExpression)
  122. {
  123. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  124. }
  125. string Boolean::_toString() const
  126. {
  127. string booleanString{};
  128. if (this->value)
  129. {
  130. booleanString = this->TRUE_STRING;
  131. }
  132. else
  133. {
  134. booleanString = this->FALSE_STRING;
  135. }
  136. return booleanString;
  137. }