timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
transient_function.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
27// For std::decay
28#include <type_traits>
29
30namespace tim
31{
32namespace utility
33{
34template <typename>
35struct transient_function; // intentionally not defined
36
37/// \struct tim::utility::transient_function
38/// \brief A light-weight alternative to std::function. Pass any callback - including
39/// capturing lambdas - cheaply and quickly as a function argument
40///
41/// - No instantiation of called function at each call site
42/// - Simple to use - use transient_function<...> as the function argument
43/// - Low cost, cheap setup, one indirect function call to invoke
44/// - No risk of dynamic allocation (unlike std::function)
45/// - Not persistent: synchronous calls only
46///
47template <typename RetT, typename... Args>
48struct transient_function<RetT(Args...)>
49{
50 using dispatch_type = RetT (*)(void*, Args...);
51 using target_function_reference = RetT(Args...);
52
53 // dispatch_type() is instantiated by the transient_function constructor,
54 // which will store a pointer to the function in m_dispatch.
55 template <typename Up>
56 static RetT dispatcher(void* target, Args&&... args)
57 {
58 return (*static_cast<Up*>(target))(std::forward<Args>(args)...);
59 }
60
61 transient_function() = default;
64 transient_function& operator=(transient_function&&) noexcept = default;
65
66 template <typename Tp>
67 transient_function(Tp&& target)
68 : m_dispatch(&dispatcher<std::decay_t<Tp>>)
69 , m_target(&target)
70 {}
71
72 // Specialize for reference-to-function, to ensure that a valid pointer is stored.
74 : m_dispatch(dispatcher<target_function_reference>)
75 {
76 static_assert(
77 sizeof(void*) == sizeof(target),
78 "It will not be possible to pass functions by reference on this platform. "
79 "Please use explicit function pointers i.e. foo(target) -> foo(&target)");
80 m_target = static_cast<void*>(target);
81 }
82
83 // Specialize for reference-to-function, to ensure that a valid pointer is stored.
85 {
86 if(this == &target)
87 return *this;
88
89 static_assert(
90 sizeof(void*) == sizeof(target),
91 "It will not be possible to pass functions by reference on this platform. "
92 "Please use explicit function pointers i.e. foo(target) -> foo(&target)");
93 m_dispatch = dispatcher<target_function_reference>;
94 m_target = static_cast<void*>(target);
95 return *this;
96 }
97
98 RetT operator()(Args&&... args) const
99 {
100 return m_dispatch(m_target, std::forward<Args>(args)...);
101 }
102
103private:
104 // A pointer to the static function that will call the wrapped invokable object
105 dispatch_type m_dispatch = nullptr;
106 void* m_target = nullptr; // pointer to the invokable object
107};
108//
109} // namespace utility
110//
111namespace scope
112{
113//
114//--------------------------------------------------------------------------------------//
115//
116/// \struct tim::scope::destructor
117/// \brief provides an object which can be returned from functions that will execute
118/// the lambda provided during construction when it is destroyed
119///
121{
122 template <typename FuncT>
123 transient_destructor(FuncT&& _func)
124 : m_functor(std::forward<FuncT>(_func))
125 {}
126
127 // delete copy operations
130
131 // allow move operations
134
135 ~transient_destructor() { m_functor(); }
136
137private:
138 utility::transient_function<void()> m_functor = []() {};
139};
140} // namespace scope
141//
142} // namespace tim
STL namespace.
A light-weight alternative to std::function. Pass any callback - including capturing lambdas - cheapl...
Definition: kokkosp.cpp:39
typename std::decay< T >::type decay_t
Alias template for decay.
Definition: types.hpp:194
transient_destructor & operator=(const transient_destructor &)=delete
transient_destructor & operator=(transient_destructor &&rhs) noexcept=default
transient_destructor(transient_destructor &&rhs) noexcept=default
transient_destructor(const transient_destructor &)=delete
static RetT dispatcher(void *target, Args &&... args)
transient_function(transient_function &&) noexcept=default
transient_function(target_function_reference target)
transient_function & operator=(target_function_reference target)
typename typename typename
Definition: types.hpp:226