Boolean.cppm 4.0 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: 2025-12-24
  7. *
  8. * */
  9. module;
  10. #include <algorithm>
  11. #include <ls-std/core/Class.hpp>
  12. #include <ls-std/core/interface/IBoxing.hpp>
  13. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  14. #include <ls-std/os/dynamic-goal.hpp>
  15. #include <string>
  16. export module ls.std.boxing.boolean;
  17. using ls::std::core::Class;
  18. using ls::std::core::IllegalArgumentException;
  19. using ls::std::core::interface_type::IBoxing;
  20. using std::string;
  21. using std::transform;
  22. export namespace ls::std::boxing
  23. {
  24. class LS_STD_DYNAMIC_GOAL Boolean : public Class, public IBoxing
  25. {
  26. public:
  27. explicit Boolean(const bool _value) : Class(_fetchClassName()), value(_value)
  28. {}
  29. Boolean() : Class(_fetchClassName())
  30. {}
  31. ~Boolean() noexcept override = default;
  32. Boolean &operator=(const int _value)
  33. {
  34. this->value = _value;
  35. return *this;
  36. }
  37. Boolean &operator=(const bool _value)
  38. {
  39. this->value = _value;
  40. return *this;
  41. }
  42. bool operator&&(const Boolean &_boolean) const
  43. {
  44. return this->value && _boolean.getValue();
  45. }
  46. bool operator&&(const bool _value) const
  47. {
  48. return this->value && _value;
  49. }
  50. bool operator&&(const int _value) const
  51. {
  52. return this->value && _value;
  53. }
  54. bool operator||(const Boolean &_boolean) const
  55. {
  56. return this->value || _boolean.getValue();
  57. }
  58. bool operator||(const bool _value) const
  59. {
  60. return this->value || _value;
  61. }
  62. bool operator||(const int _value) const
  63. {
  64. return this->value || _value;
  65. }
  66. void parse(const string &_parseText) override
  67. {
  68. string parseText = _parseText;
  69. transform(parseText.begin(), parseText.end(), parseText.begin(), ::tolower);
  70. if (parseText != this->TRUE_STRING && parseText != this->FALSE_STRING)
  71. {
  72. throw IllegalArgumentException{parseText + " is not a valid string representation"};
  73. }
  74. else
  75. {
  76. if (parseText == this->TRUE_STRING)
  77. {
  78. this->value = true;
  79. }
  80. if (parseText == this->FALSE_STRING)
  81. {
  82. this->value = false;
  83. }
  84. }
  85. }
  86. [[nodiscard]] string toString() override
  87. {
  88. return this->_toString();
  89. }
  90. [[nodiscard]] bool getValue() const
  91. {
  92. return this->value;
  93. }
  94. [[nodiscard]] static bool XOR(const Boolean &_leftExpression, const Boolean &_rightExpression)
  95. {
  96. return (_leftExpression.getValue() && !_rightExpression.getValue()) || (!_leftExpression.getValue() && _rightExpression.getValue());
  97. }
  98. [[nodiscard]] static bool XOR(const Boolean &_leftExpression, const bool _rightExpression)
  99. {
  100. return (_leftExpression.getValue() && !_rightExpression) || (!_leftExpression.getValue() && _rightExpression);
  101. }
  102. [[nodiscard]] static bool XOR(const bool _leftExpression, const Boolean &_rightExpression)
  103. {
  104. return (_leftExpression && !_rightExpression.getValue()) || (!_leftExpression && _rightExpression.getValue());
  105. }
  106. [[nodiscard]] static bool XOR(const bool _leftExpression, const bool _rightExpression)
  107. {
  108. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  109. }
  110. private:
  111. bool value{};
  112. const string FALSE_STRING = "false";
  113. const string TRUE_STRING = "true";
  114. [[nodiscard]] static string _fetchClassName()
  115. {
  116. static const string className = "Boolean";
  117. return className;
  118. }
  119. [[nodiscard]] string _toString() const
  120. {
  121. string booleanString{};
  122. if (this->value)
  123. {
  124. booleanString = this->TRUE_STRING;
  125. }
  126. else
  127. {
  128. booleanString = this->FALSE_STRING;
  129. }
  130. return booleanString;
  131. }
  132. };
  133. }