6from .Helper
import write_file
8from .Overwrite
import Overwrite
13 Representative of generated Constants.h holding all user-defined constants
15 Represents all constants that a Project exports from the Python
16 script into C++. I do provide routines to export defines or
17 constants (via constexpr). For the latter, I rely on the auto
18 type word unless you use a specialised routine for a particular
21 Each project has one instance of Constants, so you can always
22 add/export new constants with
24 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 my_project.constants.export ...
26 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 Constants also allows you to add defines (precompiler statements).
31 default_overwrite =
True
36 self.
d[
"ADD_CONSTANTS" ] =
""
37 self.
d[
"INCLUDES" ] =
""
38 self.
d[
"OPEN_NAMESPACE" ] =
""
39 self.
d[
"CLOSE_NAMESPACE" ] =
""
40 self.
d[
"INCLUDE_GUARD" ] =
"_"
41 for i
in project.namespace:
42 self.
d[
"OPEN_NAMESPACE" ] +=
"namespace " + i +
"{\n"
43 self.
d[
"CLOSE_NAMESPACE" ] +=
"}\n"
44 self.
d[
"INCLUDE_GUARD" ] += i +
"_"
45 self.
d[
"INCLUDE_GUARD" ] +=
"CONSTANTS_"
46 self.
d[
"INCLUDE_GUARD" ] = self.
d[
"INCLUDE_GUARD" ].upper()
50 Add a whole include statement.
52 self.
d[
"INCLUDES" ] += include_statement +
"\n"
56 Tell the C++ code underlying the project that a certain variable with a
57 name has a certain value. The passed arguments are mapped onto an
58 constexpr. Therefore, name has to be a string, while value can be an
59 integer, a float or a string as well. If you want to export booleans
60 or just define variants, you have to use the other routines.
62 new_entry =
"constexpr auto " + str(name) +
" = " + str(value) +
";"
63 self.
d[
"ADD_CONSTANTS" ] +=
" " + new_entry +
"\n"
68 Tell the C++ code underlying the project that a certain variable with a
69 name has a certain value. The passed arguments are mapped onto an
70 constexpr. Therefore, name has to be a string, while value can be an
71 integer, a float or a string as well. If you want to export booleans
72 or just define variants, you have to use the other routines.
74 new_entry =
"const std::bitset<" + str(len(value)) +
"> " + str(name) +
" = 0";
76 for i
in range(0,len(value)):
78 new_entry +=
"+" + str(base)
81 self.
d[
"ADD_CONSTANTS" ] +=
" " + new_entry +
"\n"
86 Tell the C++ code underlying the project that a certain variable with a
87 name has a certain value. The passed arguments are mapped onto an
88 constexpr. Therefore, name has to be a string, while value can be an
89 integer, a float or a string as well. If you want to export booleans
90 or just define variants, you have to use the other routines.
97 Tell the C++ code underlying the project that a certain variable with a
98 name has a certain value. The passed arguments are mapped onto an
99 const. Therefore, name has to be a string, while value can be an
100 integer, a float or a string as well. If you want to export booleans
101 or just define variants, you have to use the other routines.
103 new_entry =
"const " + type +
" " + str(name) +
" = " + value +
";"
104 self.
d[
"ADD_CONSTANTS" ] +=
" " + new_entry +
"\n"
109 Tell the C++ code underlying the project that a certain variable with a
110 name has a certain value. The passed arguments are mapped onto an
111 constexpr. Therefore, name has to be a string, while value can be an
112 integer, a float or a string as well. If you want to export booleans
113 or just define variants, you have to use the other routines.
115 new_entry =
"constexpr " + type +
" " + str(name) +
" = " + value +
";"
116 self.
d[
"ADD_CONSTANTS" ] +=
" " + new_entry +
"\n"
120 self.
d[
"ADD_CONSTANTS" ] =
""
124 new_entry =
"constexpr bool " + name +
" = "
129 self.
d[
"ADD_CONSTANTS" ] +=
" " + new_entry +
";\n"
135 Add define pragma to Constants.h
137 This routine introduces a
139 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
141 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
143 entry into your Constants.h.
146 new_entry =
"#define " + name
147 self.
d[
"ADD_CONSTANTS" ] +=
" " + new_entry +
"\n"
153 Add define pragma to Constants.h
155 This routine introduces a
157 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
159 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
161 entry into your Constants.h.
164 new_entry =
"#define " + name +
" " + value
165 self.
d[
"ADD_CONSTANTS" ] +=
" " + new_entry +
"\n"
174## Exported C++ constants in project
176""" + self.
d[
"ADD_CONSTANTS" ] +
"""
178These constants are available to the application within its namespace.
183 filename = directory +
"/Constants.h"
185 def generate(self, overwrite, directory, subdirectory=""):
186 filename = directory +
"/" + subdirectory +
"Constants.h"
188 print(
"write " + filename )
196 with open( os.path.realpath(__file__).replace(
".pyc",
".h.template" ).replace(
".py",
".h.template" ),
"r" )
as input:
197 template = input.read()
198 with open( filename,
"w" )
as output:
199 output.write( template.format(**self.
d) )
Representative of generated Constants.h holding all user-defined constants.
export_constexpr_with_type(self, name, value, type)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
export(self, name, value)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
define_value(self, name, value)
Add define pragma to Constants.h.
export_boolean_sequence(self, name, value)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
export_string(self, name, value)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
export_boolean(self, name, value)
define(self, name)
Add define pragma to Constants.h.
generate(self, overwrite, directory)
export_const_with_type(self, name, value, type)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
add_include(self, include_statement)
Add a whole include statement.