Remove SingletonVault C bindings
[folly.git] / folly / poly / Nullable.h
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <folly/Poly.h>
20 #include <folly/poly/Regular.h>
21
22 namespace folly {
23 namespace poly {
24 /**
25  * A `Poly` interface that can be used to make Poly objects initializable from
26  * `nullptr` (to create an empty `Poly`) and equality comparable to `nullptr`
27  * (to test for emptiness).
28  */
29 struct INullablePointer : PolyExtends<IEqualityComparable> {
30   template <class Base>
31   struct Interface : Base {
32     Interface() = default;
33     using Base::Base;
34
35     /* implicit */ Interface(std::nullptr_t) : Base{} {
36       static_assert(
37           std::is_default_constructible<PolySelf<Base>>::value,
38           "Cannot initialize a non-default constructible Poly with nullptr");
39     }
40
41     PolySelf<Base>& operator=(std::nullptr_t) {
42       static_assert(
43           std::is_default_constructible<PolySelf<Base>>::value,
44           "Cannot initialize a non-default constructible Poly with nullptr");
45       auto& self = static_cast<PolySelf<Base>&>(*this);
46       self = PolySelf<Base>();
47       return self;
48     }
49
50     friend bool operator==(
51         std::nullptr_t,
52         PolySelf<Base> const& self) noexcept {
53       return poly_empty(self);
54     }
55     friend bool operator==(
56         PolySelf<Base> const& self,
57         std::nullptr_t) noexcept {
58       return poly_empty(self);
59     }
60     friend bool operator!=(
61         std::nullptr_t,
62         PolySelf<Base> const& self) noexcept {
63       return !poly_empty(self);
64     }
65     friend bool operator!=(
66         PolySelf<Base> const& self,
67         std::nullptr_t) noexcept {
68       return !poly_empty(self);
69     }
70   };
71 };
72
73 /**
74  * A `Poly` interface that can be used to make `Poly` objects contextually
75  * convertible to `bool` (`true` if and only if non-empty). It also gives
76  * `Poly` objects a unary logical negation operator.
77  */
78 struct IBooleanTestable : PolyExtends<> {
79   template <class Base>
80   struct Interface : Base {
81     constexpr bool operator!() const noexcept {
82       return poly_empty(*this);
83     }
84     constexpr explicit operator bool() const noexcept {
85       return !!*this;
86     }
87   };
88 };
89 } // namespace poly
90 } // namespace folly