You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.9KB

  1. /* "True" vs "False" vs "Unknown".
  2. Copyright (C) 2019-2020 Free Software Foundation, Inc.
  3. Contributed by David Malcolm <dmalcolm@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_TRISTATE_H
  17. #define GCC_TRISTATE_H
  18. /* "True" vs "False" vs "Unknown". */
  19. class tristate {
  20. public:
  21. enum value {
  22. TS_UNKNOWN,
  23. TS_TRUE,
  24. TS_FALSE
  25. };
  26. tristate (enum value val) : m_value (val) {}
  27. tristate (bool val) : m_value (val ? TS_TRUE : TS_FALSE) {}
  28. static tristate unknown () { return tristate (TS_UNKNOWN); }
  29. const char *as_string () const;
  30. bool is_known () const { return m_value != TS_UNKNOWN; }
  31. bool is_true () const { return m_value == TS_TRUE; }
  32. bool is_false () const { return m_value == TS_FALSE; }
  33. tristate not_ () const;
  34. tristate or_ (tristate other) const;
  35. tristate and_ (tristate other) const;
  36. bool operator== (const tristate &other) const
  37. {
  38. return m_value == other.m_value;
  39. }
  40. bool operator!= (const tristate &other) const
  41. {
  42. return m_value != other.m_value;
  43. }
  44. private:
  45. enum value m_value;
  46. };
  47. /* Overloaded boolean operators on tristates. */
  48. inline tristate
  49. operator ! (tristate t)
  50. {
  51. return t.not_ ();
  52. }
  53. inline tristate
  54. operator || (tristate a, tristate b)
  55. {
  56. return a.or_ (b);
  57. }
  58. inline tristate
  59. operator && (tristate a, tristate b)
  60. {
  61. return a.and_ (b);
  62. }
  63. #endif /* GCC_TRISTATE_H */