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.
construct.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/**
26 * \file timemory/operations/types/construct.hpp
27 * \brief Definition for various functions for construct in operations
28 */
29
30#pragma once
31
35
36namespace tim
37{
38namespace operation
39{
40//
41//--------------------------------------------------------------------------------------//
42//
43///
44/// \struct tim::operation::construct
45/// \brief The purpose of this operation class is construct an object with specific args
46///
47//
48//--------------------------------------------------------------------------------------//
49//
50template <typename Tp>
52{
53 using type = Tp;
54
56
57 template <typename Arg, typename... Args>
58 construct(type& obj, Arg&& arg, Args&&... args);
59
60 template <typename... Args, enable_if_t<sizeof...(Args) == 0, int> = 0>
61 construct(type&, Args&&...);
62
63 template <typename... Args,
64 enable_if_t<std::is_constructible<Tp, Args...>::value, int> = 0>
65 static auto get(Args&&... args)
66 {
67 return Tp(std::forward<Args>(args)...);
68 }
69
70 template <typename... Args, enable_if_t<!std::is_constructible<Tp, Args...>::value &&
71 std::is_default_constructible<Tp>::value,
72 int> = 0>
73 static auto get(Args&&...)
74 {
75 return Tp{};
76 }
77
78private:
79 // resolution #1 (best)
80 // construction is possible with given arguments
81 template <typename Up, typename... Args>
82 auto sfinae(Up& obj, int, Args&&... args)
83 -> decltype(Up(std::forward<Args>(args)...), void())
84 {
85 obj = Up(std::forward<Args>(args)...);
86 }
87
88 // resolution #2
89 // construction is not possible with given arguments
90 template <typename Up, typename... Args>
91 auto sfinae(Up&, long, Args&&...) -> decltype(void(), void())
92 {}
93};
94//
95template <typename Tp>
96struct construct<Tp*>
97{
99
100 template <typename... Args>
101 static Tp* get(Args&&...)
102 {
103 return nullptr;
104 }
105};
106//
107template <typename Tp>
108struct construct<std::shared_ptr<Tp>>
109{
110 using base_type = construct<Tp>;
111
112 template <typename... Args>
113 static auto get(Args&&...)
114 {
115 return std::shared_ptr<Tp>(nullptr);
116 }
117};
118//
119template <typename Tp, typename... Deleter>
120struct construct<std::unique_ptr<Tp, Deleter...>>
121{
122 using base_type = construct<Tp>;
123
124 template <typename... Args>
125 static auto get(Args&&...)
126 {
127 return std::unique_ptr<Tp, Deleter...>(nullptr);
128 }
129};
130//
131//--------------------------------------------------------------------------------------//
132//
133template <typename Tp>
134template <typename Arg, typename... Args>
135construct<Tp>::construct(type& obj, Arg&& arg, Args&&... args)
136{
137 sfinae(obj, 0, std::forward<Arg>(arg), std::forward<Args>(args)...);
138}
139//
140//--------------------------------------------------------------------------------------//
141//
142template <typename Tp>
143template <typename... Args, enable_if_t<sizeof...(Args) == 0, int>>
145{}
146//
147//--------------------------------------------------------------------------------------//
148//
149} // namespace operation
150} // namespace tim
STL namespace.
Definition: kokkosp.cpp:39
typename std::enable_if< B, T >::type enable_if_t
Alias template for enable_if.
Definition: types.hpp:190
The declaration for the types for operations without definitions.
Include the macros for operations.
Declare the operations types.
static Tp * get(Args &&...)
Definition: construct.hpp:101
The purpose of this operation class is construct an object with specific args.
Definition: construct.hpp:52
TIMEMORY_DELETED_OBJECT(construct) template< typename Arg
static auto get(Args &&... args)
Definition: construct.hpp:65
typename Args construct(type &obj, Arg &&arg, Args &&... args)
Definition: construct.hpp:135
static auto get(Args &&...)
Definition: construct.hpp:73