Public Types | Public Member Functions | Friends | List of all members
Deferred< Rt, MakeConstReference > Class Template Reference

Provides deferred or lazy evaluation of a function or function object, as well as efficient storage of an object that is mutable. More...

#include <Deferred.hpp>

Public Types

using value_type = std::remove_const_t< std::remove_reference_t< Rt > >
 

Public Member Functions

template<typename Dummy = Rt, Requires< cpp17::is_same_v< Dummy, value_type >> = nullptr>
 Deferred (Rt t)
 
 Deferred (const Deferred &)=default
 
Deferredoperator= (const Deferred &)=default
 
 Deferred (Deferred &&)=default
 
Deferredoperator= (Deferred &&)=default
 
constexpr const value_typeget () const noexcept
 
constexpr value_typemutate () noexcept
 
void pack_unpack_lazy_function (PUP::er &p) noexcept
 
bool evaluated () const noexcept
 
void reset () noexcept
 
Deferred deep_copy () const noexcept
 
 Deferred (boost::shared_ptr< Deferred_detail::assoc_state< tmpl::conditional_t< MakeConstReference::value, const value_type &, value_type >>> &&state) noexcept
 

Friends

template<typename Rt1 , typename Fp , typename... Args>
void update_deferred_args (gsl::not_null< Deferred< Rt1 > *> deferred, Fp, Args &&... args) noexcept
 
template<typename Rt1 , typename Fp , typename... Args>
void update_deferred_args (gsl::not_null< Deferred< Rt1 > *> deferred, Args &&... args) noexcept
 

Detailed Description

template<typename Rt, typename MakeConstReference>
class Deferred< Rt, MakeConstReference >

Provides deferred or lazy evaluation of a function or function object, as well as efficient storage of an object that is mutable.

The class is similar to a std::shared_future that is able to hold and allow mutation of objects. std::shared_future allows lazy evaluation of functions but does not allow mutation of stored objects. Since mutation is only defined for storage of objects, not for lazily evaluated functions, attempts to mutate a lazily evaluated function's data is an error.

To construct a Deferred for lazy evaluation use the make_deferred() function.

Example

Construction of a Deferred with an object followed by mutation:

auto obj = Deferred<double>(3.8);
CHECK(obj.evaluated());
CHECK(3.8 == obj.get());
auto& obj_val = obj.mutate();
CHECK(3.8 == obj_val);
obj_val = 5.0;
CHECK(5.0 == obj.get());
Warning
If passed a lazy function that returns a const Rt& t (a const lvalue reference) the underlying data stored is actually a pointer to t, which will be dereferenced upon retrieval. This could lead to a dangling pointer/reference if care isn't taken. The reason for this design is that:
  1. We need to be able to retrieve data stored in the ConstGlobalCache from the DataBox. The way to do this is to have a pointer to the ConstGlobalCache inside the DataBox alongside compute items that return const Rt& to the ConstGlobalCache data.
  2. Functions used in the DataBox shouldn't return references, they are compute tags and should return by value. If we find an actual use-case for compute tags returning const Rt& where referencing behavior is undesired then this design needs to be reconsidered. It is currently the least breaking way to implement referencing DataBox members that can point to any memory.
Template Parameters
Rtthe type being stored

The documentation for this class was generated from the following file: