• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>& params) 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[] = {
64 	"0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",  "10",
65 	"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
66 	"22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
67 	"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
68 	"44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
69 	"55", "56", "57", "58", "59", "60", "61", "62", "63"};
70 
71 template <size_t ARG_NUM, typename unpacked_t>
unpackArgs(unpacked_t &)72 inline void unpackArgs(unpacked_t&) {}
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),
78 				  "ARG_NUM must be less than DE_LENGTH_OF_ARRAY(TOKENS)");
79 	unpacked[TOKENS[ARG_NUM]] = de::toString(cur);
80 	unpackArgs<ARG_NUM + 1>(unpacked, ::std::forward<args_t>(args)...);
81 }
82 } // detail
83 
84 /*--------------------------------------------------------------------*//*!
85  * \brief Implementation of specialize() using a variable argument list
86  *//*--------------------------------------------------------------------*/
87 template <typename... args_t>
format(args_t &&...args) const88 std::string StringTemplate::format(args_t&&... args) const
89 {
90 	std::map<std::string, std::string> unpacked = {};
91 	detail::unpackArgs<0>(unpacked, ::std::forward<args_t>(args)...);
92 	return specialize(unpacked);
93 }
94 
95 } // tcu
96 
97 #endif // _TCUSTRINGTEMPLATE_HPP
98