-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use own wrapper class DeferredFuture
- Loading branch information
1 parent
5fc0bab
commit 43890f9
Showing
5 changed files
with
104 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include "openPMD/auxiliary/TypeTraits.hpp" | ||
|
||
#include <future> | ||
|
||
namespace openPMD::auxiliary | ||
{ | ||
template <typename T> | ||
class DeferredFuture | ||
{ | ||
using task_type = std::packaged_task<T()>; | ||
using future_type = std::future<T>; | ||
future_type m_future; | ||
task_type m_task; | ||
|
||
public: | ||
DeferredFuture(task_type); | ||
|
||
auto get() -> T; | ||
|
||
[[nodiscard]] auto valid() const noexcept -> bool; | ||
|
||
auto wait() -> void; | ||
}; | ||
} // namespace openPMD::auxiliary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "openPMD/auxiliary/Future.hpp" | ||
#include "openPMD/LoadStoreChunk.hpp" | ||
|
||
#include <memory> | ||
|
||
// comment | ||
|
||
#include "openPMD/DatatypeMacros.hpp" | ||
|
||
namespace openPMD::auxiliary | ||
{ | ||
|
||
template <typename T> | ||
DeferredFuture<T>::DeferredFuture(task_type task) | ||
: m_future(task.get_future()), m_task(std::move(task)) | ||
{} | ||
|
||
template <typename T> | ||
auto DeferredFuture<T>::get() -> T | ||
{ | ||
if (m_future.valid()) | ||
{ | ||
m_task(); | ||
} // else get() was already called, propagate the std::future behavior | ||
return m_future.get(); | ||
} | ||
|
||
template <typename T> | ||
auto DeferredFuture<T>::valid() const noexcept -> bool | ||
{ | ||
return m_future.valid(); | ||
} | ||
|
||
template <typename T> | ||
auto DeferredFuture<T>::wait() -> void | ||
{ | ||
if (!m_task.valid()) | ||
{ | ||
m_task(); | ||
} | ||
} | ||
|
||
template class DeferredFuture<void>; | ||
template class DeferredFuture<auxiliary::detail::shared_ptr_dataset_types>; | ||
#define INSTANTIATE_FUTURE(dtype) \ | ||
template class DeferredFuture<std::shared_ptr<dtype>>; | ||
OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_FUTURE) | ||
#undef INSTANTIATE_FUTURE | ||
} // namespace openPMD::auxiliary | ||
|
||
#include "openPMD/UndefDatatypeMacros.hpp" |