1 #ifndef _TCUSTRINGTEMPLATE_HPP
2 #define _TCUSTRINGTEMPLATE_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 * Copyright (c) 2020 The Khronos Group Inc.
9 * Copyright (c) 2020 Advanced Micro Devices, Inc.
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 *//*!
24 * \file
25 * \brief String template class.
26 *//*--------------------------------------------------------------------*/
27
28 #include <deStringUtil.hpp>
29
30 #include <map>
31 #include <string>
32
33 namespace tcu
34 {
35
36 class StringTemplate
37 {
38 public:
39 StringTemplate(void);
40 StringTemplate(const std::string &str);
41 StringTemplate(StringTemplate &&other);
42 ~StringTemplate(void);
43
44 void setString(const std::string &str);
45
46 std::string specialize(const std::map<std::string, std::string> ¶ms) const;
47
48 template <typename... args_t>
49 std::string format(args_t &&...args) const;
50
51 private:
52 StringTemplate(const StringTemplate &); // not allowed!
53 StringTemplate &operator=(const StringTemplate &); // not allowed!
54
55 std::string m_template;
56 } DE_WARN_UNUSED_TYPE;
57
58 /*--------------------------------------------------------------------*//*!
59 * Utility to unpack consecutive arguments into a parameter map
60 *//*--------------------------------------------------------------------*/
61 namespace detail
62 {
63 static constexpr const char *TOKENS[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
64 "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
65 "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38",
66 "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51",
67 "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63"};
68
69 template <size_t ARG_NUM, typename unpacked_t>
unpackArgs(unpacked_t &)70 inline void unpackArgs(unpacked_t &)
71 {
72 }
73
74 template <size_t ARG_NUM, typename unpacked_t, typename arg_t, typename... args_t>
unpackArgs(unpacked_t & unpacked,arg_t && cur,args_t &&...args)75 inline void unpackArgs(unpacked_t &unpacked, arg_t &&cur, args_t &&...args)
76 {
77 static_assert(ARG_NUM < DE_LENGTH_OF_ARRAY(TOKENS), "ARG_NUM must be less than DE_LENGTH_OF_ARRAY(TOKENS)");
78 unpacked[TOKENS[ARG_NUM]] = de::toString(cur);
79 unpackArgs<ARG_NUM + 1>(unpacked, ::std::forward<args_t>(args)...);
80 }
81 } // namespace detail
82
83 /*--------------------------------------------------------------------*//*!
84 * \brief Implementation of specialize() using a variable argument list
85 *//*--------------------------------------------------------------------*/
86 template <typename... args_t>
format(args_t &&...args) const87 std::string StringTemplate::format(args_t &&...args) const
88 {
89 std::map<std::string, std::string> unpacked = {};
90 detail::unpackArgs<0>(unpacked, ::std::forward<args_t>(args)...);
91 return specialize(unpacked);
92 }
93
94 } // namespace tcu
95
96 #endif // _TCUSTRINGTEMPLATE_HPP
97