rocprofiler-sdk/cxx/details/mpl.hpp Source File

rocprofiler-sdk/cxx/details/mpl.hpp Source File#

Rocprofiler SDK Developer API: rocprofiler-sdk/cxx/details/mpl.hpp Source File
Rocprofiler SDK Developer API 0.4.0
ROCm Profiling API and tools
mpl.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22//
23
24#pragma once
25
26#include <cstddef>
27#include <cstdint>
28#include <optional>
29#include <string>
30#include <string_view>
31#include <type_traits>
32#include <utility>
33
34#define ROCPROFILER_IMPL_HAS_CONCEPT(NAME, TRAIT) \
35 template <typename Tp, typename = typename Tp::TRAIT> \
36 inline constexpr bool NAME(int) \
37 { \
38 return true; \
39 } \
40 \
41 template <typename Tp> \
42 inline constexpr bool NAME(long) \
43 { \
44 return false; \
45 }
46
47#define ROCPROFILER_IMPL_SFINAE_CONCEPT(NAME, ...) \
48 template <typename Tp> \
49 struct NAME \
50 { \
51 private: \
52 static constexpr auto sfinae(int) -> decltype(__VA_ARGS__, bool()) { return true; } \
53 \
54 static constexpr auto sfinae(long) { return false; } \
55 \
56 public: \
57 static constexpr bool value = sfinae(0); \
58 constexpr auto operator()() const { return sfinae(0); } \
59 };
60
61namespace rocprofiler
62{
63namespace sdk
64{
65namespace mpl
66{
67template <typename Tp>
69{
70 using type = std::remove_cv_t<std::remove_reference_t<std::decay_t<Tp>>>;
71};
72
73template <typename Tp>
75
76template <typename Tp, typename Up>
78: std::is_same<unqualified_identity_t<Tp>, unqualified_identity_t<Up>>
79{};
80
81template <typename Tp>
83{
84 using type = Tp;
85 using return_type = void;
86
87 static constexpr auto value = false;
88 static constexpr void default_value() {}
89};
90
91template <>
92struct string_support<const char*>
93{
94 using type = const char*;
96
97 static constexpr auto value = true;
98 static constexpr type default_value() { return nullptr; }
99
100 type operator()(const char* val) const { return val; }
101};
102
103template <>
104struct string_support<std::string_view>
105{
106 using type = std::string_view;
107 using return_type = type;
108
109 static constexpr auto value = true;
110 static constexpr type default_value() { return type{}; }
111
112 type operator()(const char* val) const { return type{val}; }
113};
114
115template <>
116struct string_support<std::string>
117{
118 using type = std::string;
119 using return_type = type;
120
121 static constexpr auto value = true;
122 static type default_value() { return type{}; }
123
124 type operator()(const char* val) const { return type{val}; }
125};
126
127namespace impl
128{
129template <typename Tp>
130struct is_string_type : std::false_type
131{};
132
133template <>
134struct is_string_type<std::string> : std::true_type
135{};
136
137template <>
138struct is_string_type<char*> : std::true_type
139{};
140
141template <>
142struct is_string_type<const char*> : std::true_type
143{};
144
145template <>
146struct is_string_type<std::string_view> : std::true_type
147{};
148} // namespace impl
149
150template <typename Tp>
151struct is_string_type : impl::is_string_type<unqualified_identity_t<Tp>>
152{};
153
154// template <typename Tp>
155// struct can_stringify
156// {
157// private:
158// static constexpr auto sfinae(int)
159// -> decltype(std::declval<std::ostream&>() << std::declval<Tp>(), bool())
160// {
161// return true;
162// }
163
164// static constexpr auto sfinae(long) { return false; }
165
166// public:
167// static constexpr bool value = sfinae(0);
168// constexpr auto operator()() const { return sfinae(0); }
169// };
170
175
177ROCPROFILER_IMPL_SFINAE_CONCEPT(can_stringify, std::declval<std::ostream&>() << std::declval<Tp>())
179 std::begin(std::declval<Tp>()),
180 std::end(std::declval<Tp>()))
181
182// compatability
183template <typename Tp>
185
186template <typename ArgT>
187inline bool
188is_empty(ArgT&& _v)
189{
190 using arg_type = unqualified_identity_t<ArgT>;
191
193 {
194 return std::forward<ArgT>(_v).empty();
195 }
196 else if constexpr(is_string_type<arg_type>::value)
197 {
198 static_assert(std::is_constructible<std::string_view, ArgT>::value,
199 "not string_view constructible");
200 return std::string_view{std::forward<ArgT>(_v)}.empty();
201 }
202
203 return false;
204}
205
206namespace impl
207{
208template <typename ContainerT, typename... Args>
209inline auto
210emplace(ContainerT& _c, int, Args&&... _args)
211 -> decltype(_c.emplace_back(std::forward<Args>(_args)...))
212{
213 return _c.emplace_back(std::forward<Args>(_args)...);
214}
215
216template <typename ContainerT, typename... Args>
217inline auto
218emplace(ContainerT& _c, long, Args&&... _args) -> decltype(_c.emplace(std::forward<Args>(_args)...))
219{
220 return _c.emplace(std::forward<Args>(_args)...);
221}
222
223template <typename ContainerT, typename ArgT>
224inline auto
225reserve(ContainerT& _c, int, ArgT _arg) -> decltype(_c.reserve(_arg), bool())
226{
227 _c.reserve(_arg);
228 return true;
229}
230
231template <typename ContainerT, typename ArgT>
232inline auto
233reserve(ContainerT&, long, ArgT)
234{
235 return false;
236}
237} // namespace impl
238
239template <typename ContainerT, typename... Args>
240inline auto
241emplace(ContainerT& _c, Args&&... _args)
242{
243 return impl::emplace(_c, 0, std::forward<Args>(_args)...);
244}
245
246template <typename ContainerT, typename ArgT>
247inline auto
248reserve(ContainerT& _c, ArgT _arg)
249{
250 return impl::reserve(_c, 0, _arg);
251}
252} // namespace mpl
253} // namespace sdk
254} // namespace rocprofiler
255
256#undef ROCPROFILER_IMPL_HAS_CONCEPT
257#undef ROCPROFILER_IMPL_SFINAE_CONCEPT
#define ROCPROFILER_IMPL_HAS_CONCEPT(NAME, TRAIT)
Definition mpl.hpp:34
#define ROCPROFILER_IMPL_SFINAE_CONCEPT(NAME,...)
Definition mpl.hpp:47
auto reserve(ContainerT &_c, int, ArgT _arg) -> decltype(_c.reserve(_arg), bool())
Definition mpl.hpp:225
auto emplace(ContainerT &_c, int, Args &&... _args) -> decltype(_c.emplace_back(std::forward< Args >(_args)...))
Definition mpl.hpp:210
std::remove_cv_t< std::remove_reference_t< std::decay_t< Tp > > > type
Definition mpl.hpp:70
bool is_empty(ArgT &&_v)
Definition mpl.hpp:188
auto emplace(ContainerT &_c, Args &&... _args)
Definition mpl.hpp:241
constexpr bool has_value_type(int)
Definition mpl.hpp:172
auto reserve(ContainerT &_c, ArgT _arg)
Definition mpl.hpp:248
constexpr bool has_mapped_type(int)
Definition mpl.hpp:174
typename unqualified_identity< Tp >::type unqualified_identity_t
Definition mpl.hpp:74
constexpr bool has_key_type(int)
Definition mpl.hpp:173
constexpr bool has_traits(int)
Definition mpl.hpp:171
STL namespace.
static constexpr void default_value()
Definition mpl.hpp:88
static constexpr auto value
Definition mpl.hpp:87