1 /******************************************************************************
2 * Copyright (C) 2015-2017 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * @file ${filename}.cpp
24 *
25 * @brief Dynamic Knobs for Core.
26 *
27 * ======================= AUTO GENERATED: DO NOT EDIT !!! ====================
28 *
29 * Generation Command Line:
30 * ${'\n* '.join(cmdline)}
31 *
32 ******************************************************************************/
33 <% calc_max_knob_len(knobs) %>
34 % for inc in includes:
35 #include <${inc}>
36 % endfor
37 #include <regex>
38 #include <core/utils.h>
39
40 //========================================================
41 // Implementation
42 //========================================================
43 void KnobBase::autoExpandEnvironmentVariables(std::string &text)
44 {
45 #if (__GNUC__) && (GCC_VERSION < 409000)
46 // <regex> isn't implemented prior to gcc-4.9.0
47 // unix style variable replacement
48 size_t start;
49 while ((start = text.find("${'${'}")) != std::string::npos) {
50 size_t end = text.find("}");
51 if (end == std::string::npos)
52 break;
53 const std::string var = GetEnv(text.substr(start + 2, end - start - 2));
54 text.replace(start, end - start + 1, var);
55 }
56 // win32 style variable replacement
57 while ((start = text.find("%")) != std::string::npos) {
58 size_t end = text.find("%", start + 1);
59 if (end == std::string::npos)
60 break;
61 const std::string var = GetEnv(text.substr(start + 1, end - start - 1));
62 text.replace(start, end - start + 1, var);
63 }
64 #else
65 {
66 // unix style variable replacement
67 static std::regex env("\\$\\{([^}]+)\\}");
68 std::smatch match;
69 while (std::regex_search(text, match, env))
70 {
71 const std::string var = GetEnv(match[1].str());
72 // certain combinations of gcc/libstd++ have problems with this
73 // text.replace(match[0].first, match[0].second, var);
74 text.replace(match.prefix().length(), match[0].length(), var);
75 }
76 }
77 {
78 // win32 style variable replacement
79 static std::regex env("\\%([^}]+)\\%");
80 std::smatch match;
81 while (std::regex_search(text, match, env))
82 {
83 const std::string var = GetEnv(match[1].str());
84 // certain combinations of gcc/libstd++ have problems with this
85 // text.replace(match[0].first, match[0].second, var);
86 text.replace(match.prefix().length(), match[0].length(), var);
87 }
88 }
89 #endif
90 }
91
92
93 //========================================================
94 // Static Data Members
95 //========================================================
96 GlobalKnobs g_GlobalKnobs;
97
98 //========================================================
99 // Knob Initialization
100 //========================================================
GlobalKnobs()101 GlobalKnobs::GlobalKnobs()
102 {
103 % for knob in knobs :
104 InitKnob(${ knob[0] });
105 % endfor
106 }
107
108 //========================================================
109 // Knob Display (Convert to String)
110 //========================================================
ToString(const char * optPerLinePrefix)111 std::string GlobalKnobs::ToString(const char* optPerLinePrefix)
112 {
113 std::basic_stringstream<char> str;
114 str << std::showbase << std::setprecision(1) << std::fixed;
115
116 if (optPerLinePrefix == nullptr) { optPerLinePrefix = ""; }
117
118 % for knob in knobs:
119 str << optPerLinePrefix << "KNOB_${knob[0]}:${space_knob(knob[0])}";
120 % if knob[1]['type'] == 'bool':
121 str << (KNOB_${knob[0]} ? "+\n" : "-\n");
122 % elif knob[1]['type'] != 'float' and knob[1]['type'] != 'std::string':
123 str << std::hex << std::setw(11) << std::left << KNOB_${knob[0]};
124 str << std::dec << KNOB_${knob[0]} << "\n";
125 % else:
126 str << KNOB_${knob[0]} << "\n";
127 % endif
128 % endfor
129 str << std::ends;
130
131 return str.str();
132 }
133 <%!
134 # Globally available python
135 max_len = 0
136 def calc_max_knob_len(knobs):
137 global max_len
138 max_len = 0
139 for knob in knobs:
140 if len(knob[0]) > max_len: max_len = len(knob[0])
141 max_len += len('KNOB_ ')
142 if max_len % 4: max_len += 4 - (max_len % 4)
143
144 def space_knob(knob):
145 knob_len = len('KNOB_' + knob)
146 return ' '*(max_len - knob_len)
147
148 def calc_max_name_len(choices_array):
149 _max_len = 0
150 for choice in choices_array:
151 if len(choice['name']) > _max_len: _max_len = len(choice['name'])
152
153 if _max_len % 4: _max_len += 4 - (_max_len % 4)
154 return _max_len
155
156 def space_name(name, max_len):
157 name_len = len(name)
158 return ' '*(max_len - name_len)
159 %>
160