add LockTraits
[folly.git] / folly / ConditionallyExistent.h
1 /*
2  * Copyright 2016 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 <glog/logging.h>
20 #include <folly/Portability.h>
21
22 namespace folly {
23
24 template <typename T, bool Debug>
25 class ConditionallyExistent;
26
27 template <typename T>
28 class ConditionallyExistent<T, false> {
29  public:
30   static constexpr bool present() { return false; }
31   explicit ConditionallyExistent(const T&) {}
32   explicit ConditionallyExistent(T&&) {}
33   ConditionallyExistent(const ConditionallyExistent&) = delete;
34   ConditionallyExistent(ConditionallyExistent&&) = delete;
35   ConditionallyExistent& operator=(const ConditionallyExistent&) = delete;
36   ConditionallyExistent& operator=(ConditionallyExistent&&) = delete;
37   template <typename F>
38   void with(const F&&) {}
39 };
40
41 template <typename T>
42 class ConditionallyExistent<T, true> {
43  public:
44   static constexpr bool present() { return true; }
45   explicit ConditionallyExistent(const T& v) : value_(v) {}
46   explicit ConditionallyExistent(T&& v) : value_(std::move(v)) {}
47   ConditionallyExistent(const ConditionallyExistent&) = delete;
48   ConditionallyExistent(ConditionallyExistent&&) = delete;
49   ConditionallyExistent& operator=(const ConditionallyExistent&) = delete;
50   ConditionallyExistent& operator=(ConditionallyExistent&&) = delete;
51   template <typename F>
52   void with(F&& f) {
53     f(value_);
54   }
55
56  private:
57   T value_;
58 };
59
60 template <typename T>
61 using ExistentIfDebug = ConditionallyExistent<T, kIsDebug>;
62 }