Apache Mesos
check.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __PROCESS_CHECK_HPP__
14 #define __PROCESS_CHECK_HPP__
15 
16 #include <string>
17 
18 #include <stout/check.hpp>
19 #include <stout/option.hpp>
20 
21 #include <process/future.hpp>
22 
23 // Provides a CHECK_PENDING macro, akin to CHECK.
24 // This appends the error if possible to the end of the log message, so there's
25 // no need to append the error message explicitly.
26 #define CHECK_PENDING(expression) \
27  CHECK_STATE(CHECK_PENDING, _check_pending, expression)
28 
29 #define CHECK_READY(expression) \
30  CHECK_STATE(CHECK_READY, _check_ready, expression)
31 
32 #define CHECK_DISCARDED(expression) \
33  CHECK_STATE(CHECK_DISCARDED, _check_discarded, expression)
34 
35 #define CHECK_FAILED(expression) \
36  CHECK_STATE(CHECK_FAILED, _check_failed, expression)
37 
38 #define CHECK_ABANDONED(expression) \
39  CHECK_STATE(CHECK_ABANDONED, _check_abandoned, expression)
40 
41 // Private structs/functions used for CHECK_*.
42 
43 template <typename T>
45 {
46  if (f.isReady()) {
47  return Error("is READY");
48  } else if (f.isDiscarded()) {
49  return Error("is DISCARDED");
50  } else if (f.isFailed()) {
51  return Error("is FAILED: " + f.failure());
52  } else {
53  CHECK(f.isPending());
54  return None();
55  }
56 }
57 
58 
59 template <typename T>
61 {
62  if (f.isPending()) {
63  return Error("is PENDING");
64  } else if (f.isDiscarded()) {
65  return Error("is DISCARDED");
66  } else if (f.isFailed()) {
67  return Error("is FAILED: " + f.failure());
68  } else {
69  CHECK(f.isReady());
70  return None();
71  }
72 }
73 
74 
75 template <typename T>
77 {
78  if (f.isPending()) {
79  return Error("is PENDING");
80  } else if (f.isReady()) {
81  return Error("is READY");
82  } else if (f.isFailed()) {
83  return Error("is FAILED: " + f.failure());
84  } else {
85  CHECK(f.isDiscarded());
86  return None();
87  }
88 }
89 
90 
91 template <typename T>
93 {
94  if (f.isPending()) {
95  return Some("is PENDING");
96  } else if (f.isReady()) {
97  return Some("is READY");
98  } else if (f.isDiscarded()) {
99  return Some("is DISCARDED");
100  } else {
101  CHECK(f.isFailed());
102  return None();
103  }
104 }
105 
106 
107 template <typename T>
109 {
110  if (f.isReady()) {
111  return Some("is READY");
112  } else if (f.isDiscarded()) {
113  return Some("is DISCARDED");
114  } else if (f.isFailed()) {
115  return Some("is FAILED: " + f.failure());
116  } else if (!f.isAbandoned()) {
117  return Some("is not abandoned");
118  }
119  return None();
120 }
121 
122 // TODO(dhamon): CHECK_NPENDING, CHECK_NREADY, etc.
123 
124 #endif // __PROCESS_CHECK_HPP__
bool isReady() const
Definition: future.hpp:1231
Definition: errorbase.hpp:35
F && f
Definition: defer.hpp:270
Option< Error > _check_abandoned(const process::Future< T > &f)
Definition: check.hpp:108
Option< Error > _check_failed(const process::Future< T > &f)
Definition: check.hpp:92
bool isPending() const
Definition: future.hpp:1224
Option< Error > _check_pending(const process::Future< T > &f)
Definition: check.hpp:44
bool isDiscarded() const
Definition: future.hpp:1238
Option< Error > _check_discarded(const process::Future< T > &f)
Definition: check.hpp:76
_Some< typename std::decay< T >::type > Some(T &&t)
Definition: some.hpp:42
Definition: none.hpp:27
bool isAbandoned() const
Definition: future.hpp:1252
const std::string & failure() const
Definition: future.hpp:1336
Option< Error > _check_ready(const process::Future< T > &f)
Definition: check.hpp:60
bool isFailed() const
Definition: future.hpp:1245
Definition: future.hpp:57