Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Attempts to perform an operation while holding
- * the lock, but only if the lock may be acquired
- * without blocking.
- *
- * \tparam T
- * The type of the functor that is
- * to be executed while the lock is
- * held.
- * \tparam Args
- * The types of a parameter pack to be
- * forwarded to operator () of the
- * instance of \em T supplied.
- *
- * \param [in] exec
- * The functor to execute while the
- * lock is held.
- * \param [in] args
- * A parameter pack whose types are
- * given by \em Args which shall be
- * forwarded to operator () of
- * \em exec.
- * \param [out] result
- * A pointer to an object of the type
- * returned by operator () of
- * \em exec. This address location
- * will be populated with the value
- * returned by \em exec with arguments
- * \em args. May be \em nullptr if
- * the return value of \em exec is
- * not desired.
- *
- * \return
- * \em true if the lock was acquired
- * and \em exec was called, \em false
- * otherwise.
- */
- template <typename T, typename... Args>
- auto TryExecute (
- const T & exec,
- Args &&... args,
- decltype(exec(std::forward<Args>(args)...)) * result
- ) -> typename std::enable_if<
- !std::is_same<
- decltype(exec(std::forward<Args>(args)...)),
- void
- >::value,
- bool
- >::type {
- if (!TryAcquire()) return false;
- try {
- if (result==nullptr) exec(std::forward<Args>(args)...);
- else *result=exec(std::forward<Args>(args)...);
- } catch (...) {
- Release();
- throw;
- }
- Release();
- return true;
- }
- /**
- * Attempts to perform an operation while holding
- * the lock, but only if the lock may be acquired
- * without blocking.
- *
- * \tparam T
- * The type of the functor to be executed
- * while the lock is held.
- * \tparam Args
- * The types of a parameter pack to be
- * forwarded to operator () of the
- * instance of \em T supplied.
- *
- * \param [in] exec
- * The functor to execute while the lock
- * is held.
- * \param [in] args
- * A parameter pack whose types are
- * given by \em Args which shall be
- * forwarded to operator () of
- * \em exec.
- *
- * \return
- * \em true if the lock was acquired
- * and \em exec was called, \em false
- * otherwise.
- */
- template <typename T, typename... Args>
- auto TryExecute (
- const T & exec,
- Args &&... args
- ) -> typename std::enable_if<
- std::is_same<
- decltype(exec(std::forward<Args>(args)...)),
- void
- >::value,
- bool
- >::type {
- if (!TryAcquire()) return false;
- try {
- exec(std::forward<Args>(args)...);
- } catch (...) {
- Release();
- throw;
- }
- Release();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement