Boolean.hpp 2.6 KB

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