Boolean.cppm 4.0 KB

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