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

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

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/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 =
37  ((std::is_convertible_v<Ts, std::string_view> || IsCharArray<Ts>::value ||
38  std::is_same_v<Ts, char>) &&
39  ...);
40 
41 template <typename... Ts>
42 [[nodiscard]] auto
43 concat(const Ts&... xs) -> std::enable_if_t<!AllConvertibleToStringView<Ts...>, std::string>
44 {
45  using ::operator<<;
46  thread_local std::ostringstream oss;
47  oss.str("");
48 
49  (oss << ... << xs);
50  return oss.str();
51 }
52 
53 template <std::size_t N>
54 [[nodiscard]] constexpr inline std::size_t getSize(char (&)[N]) noexcept
55 {
56  return N;
57 }
58 
59 template <std::size_t N>
60 [[nodiscard]] constexpr inline std::size_t getSize(const char (&)[N]) noexcept
61 {
62  return N;
63 }
64 
65 [[nodiscard]] constexpr inline std::size_t getSize(const char* s) noexcept
66 {
67  const char* end = s;
68  while(*end++ != 0) {}
69  return end - s - 1;
70 }
71 
72 [[nodiscard]] constexpr inline std::size_t getSize(const char&) noexcept { return 1; }
73 
74 [[nodiscard]] inline std::size_t getSize(const std::string& s) noexcept { return s.size(); }
75 
76 [[nodiscard]] constexpr inline std::size_t getSize(const std::string_view& s) noexcept
77 {
78  return s.size();
79 }
80 
81 template <typename... Ts>
82 auto concatInto(std::string& result,
83  const Ts&... xs) -> std::enable_if_t<AllConvertibleToStringView<Ts...>, void>
84 {
85  const std::size_t space = (1 + ... + getSize(xs));
86  result.reserve(result.size() + space);
87  ((result += xs), ...);
88 }
89 
90 template <typename... Ts>
91 [[nodiscard]] auto
92 concat(const Ts&... xs) -> std::enable_if_t<AllConvertibleToStringView<Ts...>, std::string>
93 {
94  std::string result;
95  concatInto(result, xs...);
96  return result;
97 }
98 
99 // Function for types convertible to std::string_view
100 template <typename Sep, typename First, typename... Rest>
101 [[nodiscard]] auto concat(Sep sep, const First& first, const Rest&... rest)
102  -> std::enable_if_t<AllConvertibleToStringView<First, Rest...>, std::string>
103 {
104  std::string result;
105  result += first;
106  ((result += sep, result += rest), ...);
107  return result;
108 }
109 
110 // Function for other types
111 template <typename Sep, typename First, typename... Rest>
112 [[nodiscard]] auto concat(Sep sep, const First& first, const Rest&... rest)
113  -> std::enable_if_t<!AllConvertibleToStringView<First, Rest...>, std::string>
114 {
115  using ::operator<<;
116  thread_local std::ostringstream oss;
117  oss.str("");
118  oss << first;
119  ((oss << sep << rest), ...);
120  return oss.str();
121 }
122 
123 } // 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:82
constexpr bool AllConvertibleToStringView
Definition: concat.hpp:36
auto concat(const Ts &... xs) -> std::enable_if_t<!AllConvertibleToStringView< Ts... >, std::string >
Definition: concat.hpp:43
constexpr std::size_t getSize(char(&)[N]) noexcept
Definition: concat.hpp:54
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