Skip to content

Instantly share code, notes, and snippets.

@dakka
Last active September 11, 2023 21:06
Show Gist options
  • Save dakka/cab147714b0110bb8a444a2868ebcc60 to your computer and use it in GitHub Desktop.
Save dakka/cab147714b0110bb8a444a2868ebcc60 to your computer and use it in GitHub Desktop.
call_once_with_result - a std::call_once storing the callable object's result and making it available, C++17
/-----------------------------------------------------------------------------------------
// Copyright (C) 2022-23 Fix8 Market Technologies Pty Ltd
// by David L. Dight
//
// call_once_with_result - a std::call_once storing the callable object's result and making it available
// class once_with_result and function call_once_with_result
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//----------------------------------------------------------------------------------------
#include <mutex>
#include <type_traits>
#include <functional>
#include <utility>
template<typename Fn, typename... Args>
class once_with_result
{
using ret_type = std::invoke_result_t<Fn, Args...>;
ret_type _result;
template<typename T>
struct callable_wrapper
{
std::decay_t<T> _func;
ret_type& _res;
constexpr callable_wrapper(ret_type& result, T&& func) : _res(result), _func(std::forward<T>(func)) {}
void operator()() { _res = _func(); }
};
public:
constexpr once_with_result(std::once_flag& flag, Fn&& func, Args&&... args)
{
std::call_once(flag, callable_wrapper(_result, std::bind(std::forward<Fn>(func), std::forward<Args>(args)...)));
}
once_with_result() = delete;
~once_with_result() = default;
constexpr auto get_result() const { return _result; }
};
template<typename Fn, typename... Args>
auto call_once_with_result(std::once_flag& flag, Fn&& func, Args&&... args)
{
return once_with_result (flag, std::forward<Fn>(func), std::forward<Args>(args)...).get_result();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment