| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- export module ls.std.boxing.boolean;
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-09
- * Changed: 2025-12-25
- *
- * */
- #include <algorithm>
- #include <ls-std/core/Class.hpp>
- #include <ls-std/core/interface/IBoxing.hpp>
- #include <ls-std/core/exception/IllegalArgumentException.hpp>
- #include <ls-std/os/dynamic-goal.hpp>
- #include <string>
- /*
- * @doc: class(name: 'Boolean', package: 'boxing')
- * @doc: boxing.Boolean.description('This class represents the primitive datatype bool and provides functionalities for boolean expressions and string representation.')
- * */
- using ls::std::core::Class;
- using ls::std::core::IllegalArgumentException;
- using ls::std::core::interface_type::IBoxing;
- using std::string;
- using std::transform;
- export namespace ls::std::boxing
- {
- class LS_STD_DYNAMIC_GOAL Boolean : public Class, public IBoxing
- {
- public:
- explicit Boolean(const bool _value) : Class(_fetchClassName()), value(_value)
- {}
- Boolean() : Class(_fetchClassName())
- {}
- ~Boolean() noexcept override = default;
- Boolean &operator=(const int _value)
- {
- this->value = _value;
- return *this;
- }
- Boolean &operator=(const bool _value)
- {
- this->value = _value;
- return *this;
- }
- bool operator&&(const Boolean &_boolean) const
- {
- return this->value && _boolean.getValue();
- }
- bool operator&&(const bool _value) const
- {
- return this->value && _value;
- }
- bool operator&&(const int _value) const
- {
- return this->value && _value;
- }
- bool operator||(const Boolean &_boolean) const
- {
- return this->value || _boolean.getValue();
- }
- bool operator||(const bool _value) const
- {
- return this->value || _value;
- }
- bool operator||(const int _value) const
- {
- return this->value || _value;
- }
- void parse(const string &_parseText) override
- {
- string parseText = _parseText;
- transform(parseText.begin(), parseText.end(), parseText.begin(), ::tolower);
- if (parseText != this->TRUE_STRING && parseText != this->FALSE_STRING)
- {
- throw IllegalArgumentException{parseText + " is not a valid string representation"};
- }
- else
- {
- if (parseText == this->TRUE_STRING)
- {
- this->value = true;
- }
- if (parseText == this->FALSE_STRING)
- {
- this->value = false;
- }
- }
- }
- [[nodiscard]] string toString() override
- {
- return this->_toString();
- }
- [[nodiscard]] bool getValue() const
- {
- return this->value;
- }
- [[nodiscard]] static bool XOR(const Boolean &_leftExpression, const Boolean &_rightExpression)
- {
- return (_leftExpression.getValue() && !_rightExpression.getValue()) || (!_leftExpression.getValue() && _rightExpression.getValue());
- }
- [[nodiscard]] static bool XOR(const Boolean &_leftExpression, const bool _rightExpression)
- {
- return (_leftExpression.getValue() && !_rightExpression) || (!_leftExpression.getValue() && _rightExpression);
- }
- [[nodiscard]] static bool XOR(const bool _leftExpression, const Boolean &_rightExpression)
- {
- return (_leftExpression && !_rightExpression.getValue()) || (!_leftExpression && _rightExpression.getValue());
- }
- [[nodiscard]] static bool XOR(const bool _leftExpression, const bool _rightExpression)
- {
- return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
- }
- private:
- bool value{};
- const string FALSE_STRING = "false";
- const string TRUE_STRING = "true";
- [[nodiscard]] static string _fetchClassName()
- {
- static const string className = "Boolean";
- return className;
- }
- [[nodiscard]] string _toString() const
- {
- string booleanString{};
- if (this->value)
- {
- booleanString = this->TRUE_STRING;
- }
- else
- {
- booleanString = this->FALSE_STRING;
- }
- return booleanString;
- }
- };
- }
|