• 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		(void);
42 
43 	void				setString			(const std::string& str);
44 
45 	std::string			specialize			(const std::map<std::string, std::string>& params) const;
46 
47 	template <typename... args_t>
48 	std::string			format				(args_t&&... args) const;
49 
50 private:
51 						StringTemplate		(const StringTemplate&);		// not allowed!
52 	StringTemplate&		operator=			(const StringTemplate&);		// not allowed!
53 
54 	std::string			m_template;
55 } DE_WARN_UNUSED_TYPE;
56 
57 /*--------------------------------------------------------------------*//*!
58  * Utility to unpack consecutive arguments into a parameter map
59  *//*--------------------------------------------------------------------*/
60 namespace detail
61 {
62 static constexpr const char* TOKENS[] = {
63 	"0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",  "10",
64 	"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
65 	"22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
66 	"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
67 	"44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
68 	"55", "56", "57", "58", "59", "60", "61", "62", "63"};
69 
70 template <size_t ARG_NUM, typename unpacked_t>
unpackArgs(unpacked_t &)71 inline void unpackArgs(unpacked_t&) {}
72 
73 template <size_t ARG_NUM, typename unpacked_t, typename arg_t, typename... args_t>
unpackArgs(unpacked_t & unpacked,arg_t && cur,args_t &&...args)74 inline void unpackArgs(unpacked_t& unpacked, arg_t&& cur, args_t&&... args)
75 {
76 	static_assert(ARG_NUM < DE_LENGTH_OF_ARRAY(TOKENS),
77 				  "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 } // 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 } // tcu
95 
96 #endif // _TCUSTRINGTEMPLATE_HPP
97