1 /******************************************************************************
2  * Copyright (C) 2015-2018 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 // clang-format off
34 <% calc_max_knob_len(knobs) %>
35 % for inc in includes:
36 #include <${inc}>
37 % endfor
38 #include <regex>
39 #include <core/utils.h>
40 
41 //========================================================
42 // Implementation
43 //========================================================
44 void KnobBase::autoExpandEnvironmentVariables(std::string& text)
45 {
46     size_t start;
47     while ((start = text.find("${'${'}")) != std::string::npos)
48     {
49         size_t end = text.find("}");
50         if (end == std::string::npos)
51             break;
52         const std::string var = GetEnv(text.substr(start + 2, end - start - 2));
53         text.replace(start, end - start + 1, var);
54     }
55     // win32 style variable replacement
56     while ((start = text.find("%")) != std::string::npos)
57     {
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 }
65 
66 //========================================================
67 // Static Data Members
68 //========================================================
69 % for knob in knobs:
70 % if knob[1]['type'] == 'std::string':
71 ${knob[1]['type']} GlobalKnobs::Knob_${knob[0]}::m_default = "${repr(knob[1]['default'])[1:-1]}";
72 % else:
73 ${knob[1]['type']} GlobalKnobs::Knob_${knob[0]}::m_default = ${knob[1]['default']};
74 % endif
75 % endfor
76 GlobalKnobs g_GlobalKnobs;
77 
78 //========================================================
79 // Knob Initialization
80 //========================================================
81 GlobalKnobs::GlobalKnobs()
82 {
83     % for knob in knobs :
84     InitKnob(${ knob[0] });
85     % endfor
86 }
87 
88 //========================================================
89 // Knob Display (Convert to String)
90 //========================================================
91 std::string GlobalKnobs::ToString(const char* optPerLinePrefix)
92 {
93     std::basic_stringstream<char> str;
94     str << std::showbase << std::setprecision(1) << std::fixed;
95 
96     if (optPerLinePrefix == nullptr)
97     {
98         optPerLinePrefix = "";
99     }
100 
101     % for knob in knobs:
102     str << optPerLinePrefix << "KNOB_${knob[0]}:${space_knob(knob[0])}";
103     % if knob[1]['type'] == 'bool':
104     str << (KNOB_${knob[0]} ? "+\n" : "-\n");
105     % elif knob[1]['type'] != 'float' and knob[1]['type'] != 'std::string':
106     str << std::hex << std::setw(11) << std::left << KNOB_${knob[0]};
107     str << std::dec << KNOB_${knob[0]} << "\n";
108     % else:
109     str << KNOB_${knob[0]} << "\n";
110     % endif
111     % endfor
112     str << std::ends;
113 
114     return str.str();
115 }
116 <%!
117     # Globally available python
118     max_len = 0
119     def calc_max_knob_len(knobs):
120         global max_len
121         max_len = 0
122         for knob in knobs:
123             if len(knob[0]) > max_len: max_len = len(knob[0])
124         max_len += len('KNOB_ ')
125         if max_len % 4: max_len += 4 - (max_len % 4)
126 
127     def space_knob(knob):
128         knob_len = len('KNOB_' + knob)
129         return ' '*(max_len - knob_len)
130 
131     def calc_max_name_len(choices_array):
132         _max_len = 0
133         for choice in choices_array:
134             if len(choice['name']) > _max_len: _max_len = len(choice['name'])
135 
136         if _max_len % 4: _max_len += 4 - (_max_len % 4)
137         return _max_len
138 
139     def space_name(name, max_len):
140         name_len = len(name)
141         return ' '*(max_len - name_len)
142 %>
143 // clang-format on
144