1 /******************************************************************************
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http ://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * @file ${filename}.cpp
24 * @brief Dynamic Knobs for Core.
26 * ======================= AUTO GENERATED: DO NOT EDIT !!! ====================
28 * Generation Command Line:
29 * ${'\n* '.join(cmdline)}
31 ******************************************************************************/
32 <% calc_max_knob_len(knobs
) %>
40 // Update the input string.
41 static void autoExpandEnvironmentVariables(std::string
&text
);
44 // Leave input alone and return new string.
45 static std::string
expandEnvironmentVariables(std::string
const &input
)
47 std::string text
= input
;
48 autoExpandEnvironmentVariables(text
);
53 static T
expandEnvironmentVariables(T
const &input
)
60 struct Knob
: KnobBase
63 const T
& Value() const { return m_Value
; }
64 const T
& Value(T
const &newValue
)
66 m_Value
= expandEnvironmentVariables(newValue
);
71 Knob(T
const &defaultValue
) :
72 m_Value(expandEnvironmentVariables(defaultValue
))
80 #define DEFINE_KNOB(_name, _type, _default) \\
82 struct Knob_##_name : Knob<_type> \\
86 Knob_##_name() : Knob<_type>(_default) { } \\
88 static const char* Name() { return "KNOB_" #_name; } \\
92 #define GET_KNOB(_name) g_GlobalKnobs._name.Value()
93 #define SET_KNOB(_name, _newValue) g_GlobalKnobs._name.Value(_newValue)
98 //-----------------------------------------------------------
101 % for line in knob
[1]['desc']:
104 % if knob
[1].get('choices'):
106 choices
= knob
[1].get('choices')
107 _max_len
= calc_max_name_len(choices
) %>//
108 % for i in
range(len(choices
)):
109 // ${choices[i]['name']}${space_name(choices[i]['name'], _max_len)} = ${format(choices[i]['value'], '#010x')}
113 % if knob
[1]['type'] == 'std::string':
114 DEFINE_KNOB($
{knob
[0]}, $
{knob
[1]['type']}, "${repr(knob[1]['default'])[1:-1]}");
116 DEFINE_KNOB($
{knob
[0]}, $
{knob
[1]['type']}, $
{knob
[1]['default']});
121 std::string
ToString(const char* optPerLinePrefix
="");
123 extern GlobalKnobs g_GlobalKnobs
;
128 #define KNOB_${knob[0]}${space_knob(knob[0])} GET_KNOB(${knob[0]})
132 % for inc in includes
:
136 #include <core/utils.h>
138 //========================================================
140 //========================================================
141 void KnobBase::autoExpandEnvironmentVariables(std::string
&text
)
144 // unix style variable replacement
145 static std::regex
env("\\$\\{([^}]+)\\}");
147 while (std::regex_search(text
, match
, env
))
149 const std::string var
= GetEnv(match
[1].str());
150 // certain combinations of gcc/libstd++ have problems with this
151 // text.replace(match[0].first, match[0].second, var);
152 text
.replace(match
.prefix().length(), match
[0].length(), var
);
156 // win32 style variable replacement
157 static std::regex
env("\\%([^}]+)\\%");
159 while (std::regex_search(text
, match
, env
))
161 const std::string var
= GetEnv(match
[1].str());
162 // certain combinations of gcc/libstd++ have problems with this
163 // text.replace(match[0].first, match[0].second, var);
164 text
.replace(match
.prefix().length(), match
[0].length(), var
);
170 //========================================================
171 // Static Data Members
172 //========================================================
173 GlobalKnobs g_GlobalKnobs
;
175 //========================================================
176 // Knob Initialization
177 //========================================================
178 GlobalKnobs::GlobalKnobs()
181 InitKnob($
{knob
[0]});
185 //========================================================
186 // Knob Display (Convert to String)
187 //========================================================
188 std::string
GlobalKnobs::ToString(const char* optPerLinePrefix
)
190 std::basic_stringstream
<char> str
;
191 str
<< std::showbase
<< std::setprecision(1) << std::fixed
;
193 if (optPerLinePrefix
== nullptr) { optPerLinePrefix
= ""; }
196 str
<< optPerLinePrefix
<< "KNOB_${knob[0]}:${space_knob(knob[0])}";
197 % if knob
[1]['type'] == 'bool':
198 str
<< (KNOB_$
{knob
[0]} ? "+\n" : "-\n");
199 % elif knob
[1]['type'] != 'float' and knob
[1]['type'] != 'std::string':
200 str
<< std::hex
<< std::setw(11) << std::left
<< KNOB_$
{knob
[0]};
201 str
<< std::dec
<< KNOB_$
{knob
[0]} << "\n";
203 str
<< KNOB_$
{knob
[0]} << "\n";
214 # Globally available python
216 def
calc_max_knob_len(knobs
):
220 if len(knob
[0]) > max_len
: max_len
= len(knob
[0])
221 max_len
+= len('KNOB_ ')
222 if max_len
% 4: max_len
+= 4 - (max_len
% 4)
224 def
space_knob(knob
):
225 knob_len
= len('KNOB_' + knob
)
226 return ' '*(max_len
- knob_len
)
228 def
calc_max_name_len(choices_array
):
230 for choice in choices_array
:
231 if len(choice
['name']) > _max_len
: _max_len
= len(choice
['name'])
233 if _max_len
% 4: _max_len
+= 4 - (_max_len
% 4)
236 def
space_name(name
, max_len
):
238 return ' '*(max_len
- name_len
)