ledger-core
CompletionBlock.hpp
1 /*
2  *
3  * CompletionBlock
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 01/03/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_COMPLETIONBLOCK_HPP
32 #define LEDGER_CORE_COMPLETIONBLOCK_HPP
33 
34 #include "../traits/completion_block_traits.hpp"
35 #include "../async/Promise.hpp"
36 #include "../async/Future.hpp"
37 
38 namespace ledger {
39  namespace core {
40 
41  template <typename T, class Class, bool usesPtrInComplete>
43 
44  };
45 
46  template <typename T, class Class>
47  class CompletionBlock<T, Class, true> : public Class {
48  public:
49  virtual void complete(const std::shared_ptr<T>& result, const std::experimental::optional<api::Error>& error) override {
50  if (error) {
51  _promise.failure(Exception(error.value().code, error.value().message));
52  } else {
53  _promise.success(result);
54  }
55  };
56 
57  Future<std::shared_ptr<T>> getFuture() const {
58  return _promise.getFuture();
59  }
60 
61  private:
63  };
64 
65  template <typename T, class Class>
66  class CompletionBlock<T, Class, false> : public Class {
67  public:
68  virtual void complete(const std::experimental::optional<T>& result, const std::experimental::optional<api::Error>& error) override {
69  if (error) {
70  _promise.failure(Exception(error.value().code, error.value().message));
71  } else {
72  _promise.success(result.value());
73  }
74  };
75 
76  Future<T> getFuture() const {
77  return _promise.getFuture();
78  }
79 
80  private:
81  Promise<T> _promise;
82  };
83 
84  template <typename T, class Class>
85  std::shared_ptr<CompletionBlock<T, Class, has_complete_method<Class, void (const std::shared_ptr<T>&, const std::experimental::optional<api::Error>&)>::value>>
86  make_api_completion_block() {
87  return std::make_shared<CompletionBlock<T, Class, has_complete_method<Class, void (const std::shared_ptr<T>&, const std::experimental::optional<api::Error>&)>::value>>();
88  };
89 
90  }
91 }
92 
93 
94 #endif //LEDGER_CORE_COMPLETIONBLOCK_HPP
Definition: CompletionBlock.hpp:42
Definition: Deffered.hpp:49
Definition: Deffered.hpp:52
Definition: Account.cpp:8
Definition: optional.hpp:177
Definition: Exception.hpp:45