A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SafeResult.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 #ifndef PDB_UTILITIES_SAFERESULT_H
19 #define PDB_UTILITIES_SAFERESULT_H
20 
21 #include <functional>
22 #include <memory>
23 #include <string>
24 #include <stdlib.h>
25 
26 using std::function;
27 using std::string;
28 using std::shared_ptr;
29 
30 namespace pdb {
31 
32 template <typename P>
33 class SafeResult {
34 
35 public:
36  virtual void apply(function<void(P)> forSuccessCase,
37  function<void(string errorMsg)> forErrorCase) = 0;
38 
44  P getResultOrExit(int exitCode) {
45  P* result;
46  this->apply([&](P successResult) { result = &successResult; },
47 
48  [&](string errorMessage) { exit(exitCode); });
49 
50  return *result;
51  }
52 };
53 
54 template <typename P>
55 class SafeResultSuccess : public SafeResult<P> {
56 
57 public:
58  SafeResultSuccess(P result) : _result(result) {}
59 
60  void apply(function<void(P)> forSuccessCase, function<void(string)> /*forFailureCase*/) {
61  return forSuccessCase(_result);
62  }
63 
64 private:
66 };
67 
68 template <typename P>
69 class SafeResultFailure : public SafeResult<P> {
70 public:
71  SafeResultFailure(string errorMessage) {
72  _errorMessage = errorMessage;
73  }
74 
75  void apply(function<void(P)> /*forSuccessCase*/, function<void(string)> forFailureCase) {
76  return forFailureCase(_errorMessage);
77  }
78 
79 private:
80  string _errorMessage;
81 };
82 }
83 
84 #endif // ALLUNITTESTS_SAFERESULT_H
void apply(function< void(P)> forSuccessCase, function< void(string)>)
Definition: SafeResult.h:60
P getResultOrExit(int exitCode)
Definition: SafeResult.h:44
SafeResultSuccess(P result)
Definition: SafeResult.h:58
SafeResultFailure(string errorMessage)
Definition: SafeResult.h:71
virtual void apply(function< void(P)> forSuccessCase, function< void(string errorMsg)> forErrorCase)=0
void apply(function< void(P)>, function< void(string)> forFailureCase)
Definition: SafeResult.h:75