Subversion
future.hpp
Go to the documentation of this file.
1 /**
2  * @file svnxx/detail/future.hpp
3  * @copyright
4  * ====================================================================
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  * ====================================================================
22  * @endcopyright
23  */
24 
25 #ifndef SVNXX_DETAIL_FUTURE_HPP
26 #define SVNXX_DETAIL_FUTURE_HPP
27 
28 #include <future>
29 #include <memory>
30 
31 namespace apache {
32 namespace subversion {
33 namespace svnxx {
34 namespace detail {
35 namespace future_ {
36 
37 // Forward delcaration of the future result context.
38 class result;
39 using shared_ptr = std::shared_ptr<result>;
40 using unique_ptr = std::unique_ptr<result>;
41 
42 
43 // Base class for template shared_future.
45 {
46 protected:
47  shared_future_base() noexcept {}
48 
50  : shared_result(that.shared_result)
51  {}
52 
54  : shared_result(std::move(that.shared_result))
55  {}
56 
57  explicit shared_future_base(shared_ptr shared_result_)
58  : shared_result(shared_result_)
59  {}
60 
61 private:
62  shared_ptr shared_result;
63 };
64 
65 // Template forward declaration for shared_future constructor.
66 template<typename T> class future;
67 
68 /**
69  * @ingroup svnxx_detail
70  * @brief like <tt>std::shared_future</tt>, but also maintains
71  * internal state relevant to the asynchronous SVN++ operation.
72  */
73 template<typename T>
74 class shared_future : private std::shared_future<T>,
75  private shared_future_base
76 {
77 protected:
78  using inherited = std::shared_future<T>;
79 
80  shared_future(inherited&& that, shared_ptr shared_result_) noexcept
81  : inherited(that), shared_future_base(shared_result_)
82  {}
83 
84 public:
85  shared_future() noexcept {}
86 
87  shared_future(const shared_future& that) noexcept
88  : inherited(that), shared_future_base(that)
89  {}
90 
91  shared_future(shared_future&& that) noexcept
92  : inherited(std::move(that)), shared_future_base(std::move(that))
93  {}
94 
95  shared_future(future<T>&& that) noexcept;
96 
97  using inherited::get;
98  using inherited::valid;
99  using inherited::wait;
100  using inherited::wait_for;
101  using inherited::wait_until;
102 };
103 
104 
105 // Base class for template future.
107 {
108 protected:
109  future_base() noexcept;
110  ~future_base() noexcept;
111  future_base(future_base&& that) noexcept;
112  future_base(const future_base&) = delete;
113  explicit future_base(unique_ptr&& unique_result_) noexcept;
114 
115  shared_ptr share() noexcept;
116 
117 private:
118  unique_ptr unique_result;
119 };
120 
121 /**
122  * @ingroup svnxx_detail
123  * @brief like <tt>std::future</tt>, but also maintains internal
124  * state relevant to the asynchronous SVN++ operation.
125  */
126 template<typename T>
127 class future : private std::future<T>,
128  private future_base
129 {
130  // shared_future constructor must be able to access our base classes.
131  friend class shared_future<T>;
132 
133 protected:
134  using inherited = std::future<T>;
135 
136  future(inherited&& that, unique_ptr&& unique_result_) noexcept
137  : inherited(std::move(that)), future_base(std::move(unique_result_))
138  {}
139 
140 public:
141  future() noexcept {}
142 
143  future(future&& that) noexcept
144  : inherited(std::move(that)), future_base(std::move(that))
145  {}
146 
147  shared_future<T> share() noexcept
148  {
149  return shared_future<T>(std::move(*this));
150  }
151 
152  using inherited::get;
153  using inherited::valid;
154  using inherited::wait;
155  using inherited::wait_for;
156  using inherited::wait_until;
157 };
158 
159 // Implement the constructor here since it has to see the whole future class.
160 template<typename T>
161 inline shared_future<T>::shared_future(future<T>&& that) noexcept
162  : inherited(std::move(that)), shared_future_base(that.future_base::share())
163 {}
164 
165 } // namespace future_
166 
167 template<typename T> using future = future_::future<T>;
168 template<typename T> using shared_future = future_::shared_future<T>;
169 
170 } // namespace detail
171 } // namespace svnxx
172 } // namespace subversion
173 } // namespace apache
174 
175 #endif // SVNXX_DETAIL_FUTURE_HPP
apache::subversion::svnxx::detail::future_::shared_future_base
Definition: future.hpp:44
apache::subversion::svnxx::detail::future_::future
like std::future, but also maintains internal state relevant to the asynchronous SVN++ operation.
Definition: future.hpp:66
apache::subversion::svnxx::detail::future_::shared_future
like std::shared_future, but also maintains internal state relevant to the asynchronous SVN++ operati...
Definition: future.hpp:74
apache::subversion::svnxx::detail::future_::future_base
Definition: future.hpp:106