Toplevel entrypoints for classes/traits/interfaces
[hiphop-php.git] / hphp / util / tribool.h
blob617e435fc143e6d50fe2bec786f9917003348f0f
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #pragma once
18 #include <cstdint>
20 namespace HPHP {
22 //////////////////////////////////////////////////////////////////////
25 * Represents definitely yes, definitely no, or maybe.
28 enum class TriBool : uint8_t {
29 Yes = (1 << 0),
30 No = (1 << 1),
31 Maybe = Yes | No
34 // Union represents "either", so values become Maybe unless both
35 // agree. The bit values of yes and no have been chosen to make this
36 // efficient.
37 inline TriBool operator|(TriBool a, TriBool b) {
38 return static_cast<TriBool>(
39 static_cast<std::underlying_type<TriBool>::type>(a) |
40 static_cast<std::underlying_type<TriBool>::type>(b)
44 // Intersection represents sequentiality, so Yes wins over everything,
45 // and No yields to anything else.
46 inline TriBool operator&(TriBool a, TriBool b) {
47 if (a == TriBool::No) return b;
48 if (a == TriBool::Yes) return a;
49 if (b == TriBool::Yes) return b;
50 return a;
53 inline TriBool& operator|=(TriBool& a, TriBool b) {
54 a = a | b;
55 return a;
58 inline TriBool& operator&=(TriBool& a, TriBool b) {
59 a = a & b;
60 return a;
63 inline TriBool yesOrNo(bool b) { return b ? TriBool::Yes : TriBool::No; }
64 inline TriBool yesOrMaybe(bool b) { return b ? TriBool::Yes : TriBool::Maybe; }
65 inline TriBool maybeOrNo(bool b) { return b ? TriBool::Maybe : TriBool::No; }
66 inline TriBool noOrMaybe(bool b) { return b ? TriBool::No : TriBool::Maybe; }
68 inline const char* show(TriBool b) {
69 if (b == TriBool::Yes) return "yes";
70 if (b == TriBool::No) return "no";
71 return "maybe";
74 //////////////////////////////////////////////////////////////////////