Tribool.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef TRIBOOL_H
  2. #define TRIBOOL_H
  3. #include <iostream>
  4. namespace Tribool {
  5. class Tribool
  6. {
  7. public:
  8. struct IndeterminateType
  9. {
  10. };
  11. typedef bool (*IndeterminateKeywordType)(Tribool, IndeterminateType);
  12. public:
  13. Tribool();
  14. Tribool(bool value);
  15. Tribool(const IndeterminateKeywordType& value);
  16. explicit operator bool() const;
  17. Tribool operator!() const;
  18. Tribool operator&&(const Tribool& other) const;
  19. friend Tribool operator&&(const Tribool& lhs, bool rhs);
  20. friend Tribool operator&&(bool lhs, const Tribool& rhs);
  21. friend Tribool operator&&(const IndeterminateKeywordType& lhs, const Tribool& rhs);
  22. friend Tribool operator&&(const Tribool& lhs, const IndeterminateKeywordType& rhs);
  23. Tribool operator||(const Tribool& other) const;
  24. friend Tribool operator||(const Tribool& lhs, bool rhs);
  25. friend Tribool operator||(bool lhs, const Tribool& rhs);
  26. friend Tribool operator||(const IndeterminateKeywordType& lhs, const Tribool& rhs);
  27. friend Tribool operator||(const Tribool& lhs, const IndeterminateKeywordType& rhs);
  28. Tribool operator==(const Tribool& other) const;
  29. friend Tribool operator==(const Tribool& lhs, bool rhs);
  30. friend Tribool operator==(bool lhs, const Tribool& rhs);
  31. friend Tribool operator==(const IndeterminateKeywordType& lhs, const Tribool& rhs);
  32. friend Tribool operator==(const Tribool& lhs, const IndeterminateKeywordType& rhs);
  33. Tribool operator!=(const Tribool& other) const;
  34. friend Tribool operator!=(const Tribool& lhs, bool rhs);
  35. friend Tribool operator!=(bool lhs, const Tribool& rhs);
  36. friend Tribool operator!=(const IndeterminateKeywordType& lhs, const Tribool& rhs);
  37. friend Tribool operator!=(const Tribool& lhs, const IndeterminateKeywordType& rhs);
  38. static inline bool Indeterminate(Tribool x, Tribool::IndeterminateType = Tribool::IndeterminateType())
  39. {
  40. return x.m_value == Tribool::TriboolValue::Indeterminate;
  41. }
  42. private:
  43. class TriboolValue
  44. {
  45. public:
  46. enum type
  47. {
  48. True,
  49. False,
  50. Indeterminate
  51. };
  52. };
  53. private:
  54. TriboolValue::type m_value;
  55. };
  56. template<typename CharT, typename Traits>
  57. inline std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, Tribool x)
  58. {
  59. if (!Tribool::Indeterminate(x)) {
  60. out << static_cast<bool>(x);
  61. }
  62. else
  63. {
  64. typename std::basic_ostream<CharT, Traits>::sentry cerberus(out);
  65. if (cerberus)
  66. {
  67. if (out.flags() & std::ios_base::boolalpha)
  68. out << "indeterminate";
  69. else
  70. out << 2;
  71. }
  72. }
  73. return out;
  74. }
  75. template<typename CharT, typename Traits>
  76. inline std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, bool (*)(Tribool, Tribool::IndeterminateType))
  77. {
  78. return out << Tribool(Tribool::Indeterminate);
  79. }
  80. template<typename CharT, typename Traits>
  81. std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& in, Tribool& x)
  82. {
  83. if (in.flags() & std::ios_base::boolalpha)
  84. {
  85. typename std::basic_istream<CharT, Traits>::sentry cerberus(in);
  86. if (cerberus)
  87. {
  88. typedef std::basic_string<CharT> string_type;
  89. string_type falsename = "false";
  90. string_type truename = "true";
  91. string_type indeterminatename = "indeterminate";
  92. typename string_type::size_type pos = 0;
  93. bool falsename_ok = true;
  94. bool truename_ok = true;
  95. bool indeterminatename_ok = true;
  96. while ((falsename_ok && pos < falsename.size()) || (truename_ok && pos < truename.size()) || (indeterminatename_ok && pos < indeterminatename.size()))
  97. {
  98. typename Traits::int_type c = in.get();
  99. if (c == Traits::eof())
  100. return in;
  101. bool matched = false;
  102. if (falsename_ok && pos < falsename.size())
  103. {
  104. if (Traits::eq(Traits::to_char_type(c), falsename[pos]))
  105. matched = true;
  106. else
  107. falsename_ok = false;
  108. }
  109. if (truename_ok && pos < truename.size())
  110. {
  111. if (Traits::eq(Traits::to_char_type(c), truename[pos]))
  112. matched = true;
  113. else
  114. truename_ok = false;
  115. }
  116. if (indeterminatename_ok && pos < indeterminatename.size())
  117. {
  118. if (Traits::eq(Traits::to_char_type(c), indeterminatename[pos]))
  119. matched = true;
  120. else
  121. indeterminatename_ok = false;
  122. }
  123. if (matched)
  124. ++pos;
  125. if (pos > falsename.size())
  126. falsename_ok = false;
  127. if (pos > truename.size())
  128. truename_ok = false;
  129. if (pos > indeterminatename.size())
  130. indeterminatename_ok = false;
  131. }
  132. if (pos == 0)
  133. {
  134. in.setstate(std::ios_base::failbit);
  135. }
  136. else
  137. {
  138. if (falsename_ok)
  139. x = false;
  140. else if (truename_ok)
  141. x = true;
  142. else if (indeterminatename_ok)
  143. x = Tribool::Indeterminate;
  144. else
  145. in.setstate(std::ios_base::failbit);
  146. }
  147. }
  148. }
  149. else
  150. {
  151. long value;
  152. if (in >> value)
  153. {
  154. switch (value)
  155. {
  156. case 0:
  157. x = false;
  158. break;
  159. case 1:
  160. x = true;
  161. break;
  162. case 2:
  163. x = Tribool::Indeterminate;
  164. break;
  165. default:
  166. in.setstate(std::ios_base::failbit);
  167. break;
  168. }
  169. }
  170. }
  171. return in;
  172. }
  173. } // namespace Tribool
  174. #endif // TRIBOOL_H