1 <% 2 max_len = 0 3 for knob in knobs: 4 if len(knob[0]) > max_len: max_len = len(knob[0]) 5 max_len += len('KNOB_ ') 6 if max_len % 4: max_len += 4 - (max_len % 4) 7 8 def space_knob(knob): 9 knob_len = len('KNOB_' + knob) 10 return ' '*(max_len - knob_len) 11 12 def calc_max_name_len(choices_array): 13 _max_len = 0 14 for choice in choices_array: 15 if len(choice['name']) > _max_len: _max_len = len(choice['name']) 16 17 if _max_len % 4: _max_len += 4 - (_max_len % 4) 18 return _max_len 19 20 def space_name(name, max_len): 21 name_len = len(name) 22 return ' '*(max_len - name_len) 23 24 25 %>/****************************************************************************** 26 * 27 * Copyright 2015-2016 28 * Intel Corporation 29 * 30 * Licensed under the Apache License, Version 2.0 (the "License"); 31 * you may not use this file except in compliance with the License. 32 * You may obtain a copy of the License at 33 * 34 * http ://www.apache.org/licenses/LICENSE-2.0 35 * 36 * Unless required by applicable law or agreed to in writing, software 37 * distributed under the License is distributed on an "AS IS" BASIS, 38 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 39 * See the License for the specific language governing permissions and 40 * limitations under the License. 41 * 42 % if gen_header: 43 * @file ${filename}.h 44 % else: 45 * @file ${filename}.cpp 46 % endif 47 * 48 * @brief Dynamic Knobs for Core. 49 * 50 * ======================= AUTO GENERATED: DO NOT EDIT !!! ==================== 51 * 52 ******************************************************************************/ 53 %if gen_header: 54 #pragma once 55 #include <string> 56 57 template <typename T> 58 struct Knob 59 { 60 const T& Value() const { return m_Value; } 61 const T& Value(const T& newValue) { m_Value = newValue; return Value(); } 62 63 protected: 64 Knob(const T& defaultValue) : m_Value(defaultValue) {} 65 66 private: 67 T m_Value; 68 }; 69 70 #define DEFINE_KNOB(_name, _type, _default) \\ 71 72 struct Knob_##_name : Knob<_type> \\ 73 74 { \\ 75 76 Knob_##_name() : Knob<_type>(_default) { } \\ 77 78 static const char* Name() { return "KNOB_" #_name; } \\ 79 80 } _name; 81 82 #define GET_KNOB(_name) g_GlobalKnobs._name.Value() 83 #define SET_KNOB(_name, _newValue) g_GlobalKnobs._name.Value(_newValue) 84 85 struct GlobalKnobs 86 { 87 % for knob in knobs: 88 //----------------------------------------------------------- 89 // KNOB_${knob[0]} 90 // 91 % for line in knob[1]['desc']: 92 // ${line} 93 % endfor 94 % if knob[1].get('choices'): 95 <% 96 choices = knob[1].get('choices') 97 _max_len = calc_max_name_len(choices) %>// 98 % for i in range(len(choices)): 99 // ${choices[i]['name']}${space_name(choices[i]['name'], _max_len)} = ${format(choices[i]['value'], '#010x')} 100 % endfor 101 % endif 102 // 103 % if knob[1]['type'] == 'std::string': 104 DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, "${repr(knob[1]['default'])[1:-1]}"); 105 % else: 106 DEFINE_KNOB(${knob[0]}, ${knob[1]['type']}, ${knob[1]['default']}); 107 % endif 108 109 % endfor 110 GlobalKnobs(); 111 std::string ToString(const char* optPerLinePrefix=""); 112 }; 113 extern GlobalKnobs g_GlobalKnobs; 114 115 #undef DEFINE_KNOB 116 117 % for knob in knobs: 118 #define KNOB_${knob[0]}${space_knob(knob[0])} GET_KNOB(${knob[0]}) 119 % endfor 120 121 % else: 122 % for inc in includes: 123 #include <${inc}> 124 % endfor 125 126 //======================================================== 127 // Static Data Members 128 //======================================================== 129 GlobalKnobs g_GlobalKnobs; 130 131 //======================================================== 132 // Knob Initialization 133 //======================================================== 134 GlobalKnobs::GlobalKnobs() 135 { 136 % for knob in knobs: 137 InitKnob(${knob[0]}); 138 % endfor 139 } 140 141 //======================================================== 142 // Knob Display (Convert to String) 143 //======================================================== 144 std::string GlobalKnobs::ToString(const char* optPerLinePrefix) 145 { 146 std::basic_stringstream<char> str; 147 str << std::showbase << std::setprecision(1) << std::fixed; 148 149 if (optPerLinePrefix == nullptr) { optPerLinePrefix = ""; } 150 151 % for knob in knobs: 152 str << optPerLinePrefix << "KNOB_${knob[0]}:${space_knob(knob[0])}"; 153 % if knob[1]['type'] == 'bool': 154 str << (KNOB_${knob[0]} ? "+\n" : "-\n"); 155 % elif knob[1]['type'] != 'float' and knob[1]['type'] != 'std::string': 156 str << std::hex << std::setw(11) << std::left << KNOB_${knob[0]}; 157 str << std::dec << KNOB_${knob[0]} << "\n"; 158 % else: 159 str << KNOB_${knob[0]} << "\n"; 160 % endif 161 % endfor 162 str << std::ends; 163 164 return str.str(); 165 } 166 167 % endif 168