Boolean.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2021-05-01
  7. *
  8. * */
  9. #ifndef LS_STD_BOOLEAN_HPP
  10. #define LS_STD_BOOLEAN_HPP
  11. #include <memory>
  12. #include <ls_std/base/Class.hpp>
  13. #include "IBoxing.hpp"
  14. #include <ls_std/serialization/ISerializable.hpp>
  15. #include <ls_std/io/IStorable.hpp>
  16. namespace ls_std
  17. {
  18. class Boolean : public ls_std::Class, public ls_std::IBoxing, public ls_std::ISerializable, public ls_std::IStorable
  19. {
  20. public:
  21. explicit Boolean(bool _value);
  22. Boolean();
  23. ~Boolean() override = default;
  24. // conversion operator
  25. operator bool() const;
  26. // assignment operators
  27. ls_std::Boolean &operator=(int _value);
  28. ls_std::Boolean &operator=(bool _value);
  29. // stream operators
  30. friend std::ostream &operator<<(std::ostream &_outputStream, const ls_std::Boolean &_boolean)
  31. {
  32. _outputStream << _boolean._toString();
  33. return _outputStream;
  34. }
  35. // logical operators
  36. friend bool operator!(const ls_std::Boolean &_boolean)
  37. {
  38. return !_boolean.value;
  39. }
  40. bool operator&&(const ls_std::Boolean &_boolean) const;
  41. bool operator&&(bool _value) const;
  42. bool operator&&(int _value) const;
  43. bool operator||(const ls_std::Boolean &_boolean) const;
  44. bool operator||(bool _value) const;
  45. bool operator||(int _value) const;
  46. // INFO: operator ^ can not be taken for XOR, since it's not possible to implement it respecting commutative law
  47. // implementation
  48. ls_std::byte_field load() override;
  49. ls_std::byte_field marshal() override;
  50. void parse(std::string _parseText) override;
  51. void save(const ls_std::byte_field &_data) override;
  52. std::string toString() override;
  53. void unmarshal(const ls_std::byte_field &_data) override;
  54. // additional functionality
  55. bool getValue() const;
  56. void setSerializable(std::shared_ptr<ls_std::ISerializable> _serializable);
  57. void setStorable(std::shared_ptr<ls_std::IStorable> _storable);
  58. static bool XOR(const ls_std::Boolean &_leftExpression, const ls_std::Boolean &_rightExpression);
  59. static bool XOR(const ls_std::Boolean &_leftExpression, bool _rightExpression);
  60. static bool XOR(bool _leftExpression, const ls_std::Boolean &_rightExpression);
  61. static bool XOR(bool _leftExpression, bool _rightExpression);
  62. private:
  63. std::shared_ptr<ls_std::ISerializable> serializable{};
  64. std::shared_ptr<ls_std::IStorable> storable{};
  65. bool value{};
  66. const std::string FALSE_STRING = "false";
  67. const std::string TRUE_STRING = "true";
  68. std::string _toString() const;
  69. };
  70. }
  71. #endif