Boolean.cppm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. export module ls.std.boxing.boolean;
  2. /*
  3. * Author: Patrick-Christopher Mattulat
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2020-08-09
  7. * Changed: 2025-12-25
  8. *
  9. * */
  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. /*
  17. * @doc: class(name: 'Boolean', package: 'boxing')
  18. * @doc: boxing.Boolean.description('This class represents the primitive datatype bool and provides functionalities for boolean expressions and string representation.')
  19. * */
  20. using ls::std::core::Class;
  21. using ls::std::core::IllegalArgumentException;
  22. using ls::std::core::interface_type::IBoxing;
  23. using std::string;
  24. using std::transform;
  25. export namespace ls::std::boxing
  26. {
  27. class LS_STD_DYNAMIC_GOAL Boolean : public Class, public IBoxing
  28. {
  29. public:
  30. explicit Boolean(const bool _value) : Class(_fetchClassName()), value(_value)
  31. {}
  32. Boolean() : Class(_fetchClassName())
  33. {}
  34. ~Boolean() noexcept override = default;
  35. Boolean &operator=(const int _value)
  36. {
  37. this->value = _value;
  38. return *this;
  39. }
  40. Boolean &operator=(const bool _value)
  41. {
  42. this->value = _value;
  43. return *this;
  44. }
  45. bool operator&&(const Boolean &_boolean) const
  46. {
  47. return this->value && _boolean.getValue();
  48. }
  49. bool operator&&(const bool _value) const
  50. {
  51. return this->value && _value;
  52. }
  53. bool operator&&(const int _value) const
  54. {
  55. return this->value && _value;
  56. }
  57. bool operator||(const Boolean &_boolean) const
  58. {
  59. return this->value || _boolean.getValue();
  60. }
  61. bool operator||(const bool _value) const
  62. {
  63. return this->value || _value;
  64. }
  65. bool operator||(const int _value) const
  66. {
  67. return this->value || _value;
  68. }
  69. void parse(const string &_parseText) override
  70. {
  71. string parseText = _parseText;
  72. transform(parseText.begin(), parseText.end(), parseText.begin(), ::tolower);
  73. if (parseText != this->TRUE_STRING && parseText != this->FALSE_STRING)
  74. {
  75. throw IllegalArgumentException{parseText + " is not a valid string representation"};
  76. }
  77. else
  78. {
  79. if (parseText == this->TRUE_STRING)
  80. {
  81. this->value = true;
  82. }
  83. if (parseText == this->FALSE_STRING)
  84. {
  85. this->value = false;
  86. }
  87. }
  88. }
  89. [[nodiscard]] string toString() override
  90. {
  91. return this->_toString();
  92. }
  93. [[nodiscard]] bool getValue() const
  94. {
  95. return this->value;
  96. }
  97. [[nodiscard]] static bool XOR(const Boolean &_leftExpression, const Boolean &_rightExpression)
  98. {
  99. return (_leftExpression.getValue() && !_rightExpression.getValue()) || (!_leftExpression.getValue() && _rightExpression.getValue());
  100. }
  101. [[nodiscard]] static bool XOR(const Boolean &_leftExpression, const bool _rightExpression)
  102. {
  103. return (_leftExpression.getValue() && !_rightExpression) || (!_leftExpression.getValue() && _rightExpression);
  104. }
  105. [[nodiscard]] static bool XOR(const bool _leftExpression, const Boolean &_rightExpression)
  106. {
  107. return (_leftExpression && !_rightExpression.getValue()) || (!_leftExpression && _rightExpression.getValue());
  108. }
  109. [[nodiscard]] static bool XOR(const bool _leftExpression, const bool _rightExpression)
  110. {
  111. return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
  112. }
  113. private:
  114. bool value{};
  115. const string FALSE_STRING = "false";
  116. const string TRUE_STRING = "true";
  117. [[nodiscard]] static string _fetchClassName()
  118. {
  119. static const string className = "Boolean";
  120. return className;
  121. }
  122. [[nodiscard]] string _toString() const
  123. {
  124. string booleanString{};
  125. if (this->value)
  126. {
  127. booleanString = this->TRUE_STRING;
  128. }
  129. else
  130. {
  131. booleanString = this->FALSE_STRING;
  132. }
  133. return booleanString;
  134. }
  135. };
  136. }