Make Try independent of Future
[folly.git] / folly / futures / FutureException.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 <exception>
20 #include <string>
21
22 namespace folly {
23
24 class FutureException : public std::exception {
25
26 public:
27
28   explicit FutureException(std::string message_arg)
29     : message(message_arg) {}
30
31   ~FutureException() throw(){}
32
33   virtual const char *what() const throw() {
34     return message.c_str();
35   }
36
37   bool operator==(const FutureException &other) const{
38     return other.message == this->message;
39   }
40
41   bool operator!=(const FutureException &other) const{
42     return !(*this == other);
43   }
44
45   protected:
46     std::string message;
47 };
48
49 class BrokenPromise : public FutureException {
50   public:
51     explicit BrokenPromise(std::string type) :
52       FutureException(
53           (std::string("Broken promise for type name `") + type) + '`') { }
54 };
55
56 class NoState : public FutureException {
57   public:
58     explicit NoState() : FutureException("No state") { }
59 };
60
61 class PromiseAlreadySatisfied : public FutureException {
62   public:
63     explicit PromiseAlreadySatisfied() :
64       FutureException("Promise already satisfied") { }
65 };
66
67 class FutureNotReady : public FutureException {
68   public:
69     explicit FutureNotReady() :
70       FutureException("Future not ready") { }
71 };
72
73 class FutureAlreadyRetrieved : public FutureException {
74   public:
75     explicit FutureAlreadyRetrieved () :
76       FutureException("Future already retrieved") { }
77 };
78
79 class FutureCancellation : public FutureException {
80  public:
81   FutureCancellation() : FutureException("Future was cancelled") {}
82 };
83
84 class TimedOut : public FutureException {
85  public:
86   TimedOut() : FutureException("Timed out") {}
87 };
88
89 class PredicateDoesNotObtain : public FutureException {
90  public:
91   PredicateDoesNotObtain() : FutureException("Predicate does not obtain") {}
92 };
93
94 }