File tree Expand file tree Collapse file tree
include/openPMD/auxiliary Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -124,7 +124,7 @@ class DeferredComputation
124124
125125 /* * Check if the computation is valid
126126 *
127- * @return true if the computation has not been forgotten
127+ * @return true if the computation has not been invalidated
128128 */
129129 [[nodiscard]] auto valid () const noexcept -> bool;
130130};
Original file line number Diff line number Diff line change 11#include " openPMD/auxiliary/Future.hpp"
2+ #include " openPMD/Error.hpp"
23#include " openPMD/RecordComponent.hpp"
34
45#include < iostream>
@@ -39,13 +40,13 @@ auto OneTimeTask<T>::operator()() -> T
3940{
4041 if (!members.m_task_valid )
4142 {
42- throw std::runtime_error (
43+ throw error::WrongAPIUsage (
4344 " [DeferredComputation] No valid state. Probably already "
4445 " computed." );
4546 }
4647 if (!members.m_task )
4748 {
48- throw std::runtime_error (
49+ throw error::WrongAPIUsage (
4950 " [DeferredComputation] No valid task was specified." );
5051 }
5152 members.m_task_valid = false ;
@@ -168,6 +169,7 @@ auto DeferredComputation<T>::valid() const noexcept -> bool
168169
169170template class DeferredComputation <void >;
170171template class DeferredComputation <RecordComponent::shared_ptr_dataset_types>;
172+ template class DeferredComputation <std::string>; // used in tests
171173
172174// need this for clang-tidy
173175#define OPENPMD_ARRAY (type ) type[]
Original file line number Diff line number Diff line change 1919 * If not, see <http://www.gnu.org/licenses/>.
2020 */
2121// expose private and protected members for invasive testing
22+ #include " openPMD/Error.hpp"
23+ #include " openPMD/auxiliary/Future.hpp"
2224#if openPMD_USE_INVASIVE_TESTS
2325#define OPENPMD_private public:
2426#define OPENPMD_protected public:
@@ -538,3 +540,31 @@ TEST_CASE("filesystem_test", "[auxiliary]")
538540 REQUIRE (!remove_file (" ./nonexistent_file_in_cmake_bin_directory" ));
539541#endif
540542}
543+
544+ TEST_CASE (" future_test" , " [auxiliary]" )
545+ {
546+ using task_type = auxiliary::DeferredComputation<std::string>;
547+ size_t counter = 0 ;
548+
549+ auto make_task = [&counter]() {
550+ counter = 0 ;
551+ return task_type{[&counter]() {
552+ ++counter;
553+ return " success" ;
554+ }};
555+ };
556+
557+ auto move_construct = make_task ();
558+ task_type move_constructed (std::move (move_construct));
559+ REQUIRE (counter == 0 );
560+ REQUIRE (move_constructed () == " success" );
561+ REQUIRE (counter == 1 );
562+ REQUIRE_THROWS_AS (move_constructed (), error::WrongAPIUsage);
563+
564+ auto move_assign = make_task ();
565+ task_type move_assigned = std::move (move_assign);
566+ REQUIRE (counter == 0 );
567+ REQUIRE (move_assigned () == " success" );
568+ REQUIRE (counter == 1 );
569+ REQUIRE_THROWS_AS (move_assigned (), error::WrongAPIUsage);
570+ }
You can’t perform that action at this time.
0 commit comments