/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/docs-7.0.0/include/ck_tile/host/concat.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/docs-7.0.0/include/ck_tile/host/concat.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/docs-7.0.0/include/ck_tile/host/concat.hpp Source File
concat.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
7 
8 namespace ck_tile {
9 
10 template <typename T>
12 {
13 };
14 
15 template <std::size_t N>
16 struct IsCharArray<char[N]> : std::true_type
17 {
18 };
19 
20 template <std::size_t N>
21 struct IsCharArray<const char[N]> : std::true_type
22 {
23 };
24 
25 template <std::size_t N>
26 struct IsCharArray<char (&)[N]> : std::true_type
27 {
28 };
29 
30 template <std::size_t N>
31 struct IsCharArray<const char (&)[N]> : std::true_type
32 {
33 };
34 
35 template <typename... Ts>
36 inline constexpr bool AllConvertibleToStringView = ((std::is_convertible_v<Ts, std::string_view> ||
38  std::is_same_v<Ts, char>)&&...);
39 
40 template <typename... Ts>
41 [[nodiscard]] auto concat(const Ts&... xs)
42  -> std::enable_if_t<!AllConvertibleToStringView<Ts...>, std::string>
43 {
44  using ::operator<<;
45  thread_local std::ostringstream oss;
46  oss.str("");
47 
48  (oss << ... << xs);
49  return oss.str();
50 }
51 
52 template <std::size_t N>
53 [[nodiscard]] constexpr inline std::size_t getSize(char (&)[N]) noexcept
54 {
55  return N;
56 }
57 
58 template <std::size_t N>
59 [[nodiscard]] constexpr inline std::size_t getSize(const char (&)[N]) noexcept
60 {
61  return N;
62 }
63 
64 [[nodiscard]] constexpr inline std::size_t getSize(const char* s) noexcept
65 {
66  const char* end = s;
67  while(*end++ != 0) {}
68  return end - s - 1;
69 }
70 
71 [[nodiscard]] constexpr inline std::size_t getSize(const char&) noexcept { return 1; }
72 
73 [[nodiscard]] inline std::size_t getSize(const std::string& s) noexcept { return s.size(); }
74 
75 [[nodiscard]] constexpr inline std::size_t getSize(const std::string_view& s) noexcept
76 {
77  return s.size();
78 }
79 
80 template <typename... Ts>
81 auto concatInto(std::string& result, const Ts&... xs)
83 {
84  const std::size_t space = (1 + ... + getSize(xs));
85  result.reserve(result.size() + space);
86  ((result += xs), ...);
87 }
88 
89 template <typename... Ts>
90 [[nodiscard]] auto concat(const Ts&... xs)
91  -> std::enable_if_t<AllConvertibleToStringView<Ts...>, std::string>
92 {
93  std::string result;
94  concatInto(result, xs...);
95  return result;
96 }
97 
98 // Function for types convertible to std::string_view
99 template <typename Sep, typename First, typename... Rest>
100 [[nodiscard]] auto concat(Sep sep, const First& first, const Rest&... rest)
101  -> std::enable_if_t<AllConvertibleToStringView<First, Rest...>, std::string>
102 {
103  std::string result;
104  result += first;
105  ((result += sep, result += rest), ...);
106  return result;
107 }
108 
109 // Function for other types
110 template <typename Sep, typename First, typename... Rest>
111 [[nodiscard]] auto concat(Sep sep, const First& first, const Rest&... rest)
112  -> std::enable_if_t<!AllConvertibleToStringView<First, Rest...>, std::string>
113 {
114  using ::operator<<;
115  thread_local std::ostringstream oss;
116  oss.str("");
117  oss << first;
118  ((oss << sep << rest), ...);
119  return oss.str();
120 }
121 
122 } // namespace ck_tile
Definition: cluster_descriptor.hpp:13
auto concatInto(std::string &result, const Ts &... xs) -> std::enable_if_t< AllConvertibleToStringView< Ts... >, void >
Definition: concat.hpp:81
constexpr bool AllConvertibleToStringView
Definition: concat.hpp:36
auto concat(const Ts &... xs) -> std::enable_if_t<!AllConvertibleToStringView< Ts... >, std::string >
Definition: concat.hpp:41
constexpr std::size_t getSize(char(&)[N]) noexcept
Definition: concat.hpp:53
bool_constant< false > false_type
Definition: integral_constant.hpp:63
typename std::enable_if< B, T >::type enable_if_t
Definition: enable_if.hpp:27
bool_constant< true > true_type
Definition: integral_constant.hpp:62
Definition: concat.hpp:12