Peano
Loading...
Searching...
No Matches
Constants.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import os
4import re
5
6from .Helper import write_file
7
8from .Overwrite import Overwrite
9
10class Constants(object):
11 """!
12
13 Representative of generated Constants.h holding all user-defined constants
14
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
19 type.
20
21 Each project has one instance of Constants, so you can always
22 add/export new constants with
23
24 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 my_project.constants.export ...
26 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27
28 Constants also allows you to add defines (precompiler statements).
29
30 """
31 default_overwrite = True
32
33 def __init__(self,project):
34 self.defines = []
35 self.d = {}
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()
47
48 def add_include(self, include_statement ):
49 """
50 Add a whole include statement.
51 """
52 self.d[ "INCLUDES" ] += include_statement + "\n"
53
54 def export( self, name, value ):
55 """
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.
61 """
62 new_entry = "constexpr auto " + str(name) + " = " + str(value) + ";"
63 self.d[ "ADD_CONSTANTS" ] += " " + new_entry + "\n"
64 pass
65
66 def export_boolean_sequence( self, name, value ):
67 """
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.
73 """
74 new_entry = "const std::bitset<" + str(len(value)) + "> " + str(name) + " = 0";
75 base = 1
76 for i in range(0,len(value)):
77 if value[i]:
78 new_entry += "+" + str(base)
79 base *= 2
80 new_entry += ";"
81 self.d[ "ADD_CONSTANTS" ] += " " + new_entry + "\n"
82 pass
83
84 def export_string( self, name, value ):
85 """
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.
91 """
92 self.export_const_with_type( name, "\"" + str(value) + "\"", "std::string" )
93 pass
94
95 def export_const_with_type( self, name, value, type ):
96 """
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.
102 """
103 new_entry = "const " + type + " " + str(name) + " = " + value + ";"
104 self.d[ "ADD_CONSTANTS" ] += " " + new_entry + "\n"
105 pass
106
107 def export_constexpr_with_type( self, name, value, type ):
108 """
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.
114 """
115 new_entry = "constexpr " + type + " " + str(name) + " = " + value + ";"
116 self.d[ "ADD_CONSTANTS" ] += " " + new_entry + "\n"
117 pass
118
119 def clear(self):
120 self.d[ "ADD_CONSTANTS" ] = ""
121 pass
122
123 def export_boolean( self, name, value ):
124 new_entry = "constexpr bool " + name + " = "
125 if value:
126 new_entry += "true"
127 else:
128 new_entry += "false"
129 self.d[ "ADD_CONSTANTS" ] += " " + new_entry + ";\n"
130 pass
131
132 def define( self, name ):
133 """!
134
135 Add define pragma to Constants.h
136
137 This routine introduces a
138
139 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
140 #defined name
141 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
142
143 entry into your Constants.h.
144
145 """
146 new_entry = "#define " + name
147 self.d[ "ADD_CONSTANTS" ] += " " + new_entry + "\n"
148 pass
149
150 def define_value(self, name, value):
151 """!
152
153 Add define pragma to Constants.h
154
155 This routine introduces a
156
157 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
158 #defined name value
159 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
160
161 entry into your Constants.h.
162
163 """
164 new_entry = "#define " + name + " " + value
165 self.d[ "ADD_CONSTANTS" ] += " " + new_entry + "\n"
166 pass
167
168 def readme_entry(self):
169 """!
170
171 """
172 result = """
173
174## Exported C++ constants in project
175
176""" + self.d[ "ADD_CONSTANTS" ] + """
177
178These constants are available to the application within its namespace.
179"""
180 return result
181
182 def generate(self, overwrite, directory):
183 filename = directory + "/Constants.h"
184
185 def generate(self, overwrite, directory, subdirectory=""):
186 filename = directory + "/" + subdirectory + "Constants.h"
187 if write_file(overwrite,self.default_overwritedefault_overwrite,filename):
188 print( "write " + filename )
189
190 #for i in self.cppfiles:
191 # self.d[ 'SOURCES' ] += " "
192 # self.d[ 'SOURCES' ] += i
193
194 # We first eliminate the precompiled variant, and then we get rid of the
195 # postfix in the case where it is a source file
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.
Definition Constants.py:10
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.
Definition Constants.py:107
export(self, name, value)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
Definition Constants.py:54
define_value(self, name, value)
Add define pragma to Constants.h.
Definition Constants.py:150
export_boolean_sequence(self, name, value)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
Definition Constants.py:66
export_string(self, name, value)
Tell the C++ code underlying the project that a certain variable with a name has a certain value.
Definition Constants.py:84
export_boolean(self, name, value)
Definition Constants.py:123
define(self, name)
Add define pragma to Constants.h.
Definition Constants.py:132
generate(self, overwrite, directory)
Definition Constants.py:182
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.
Definition Constants.py:95
add_include(self, include_statement)
Add a whole include statement.
Definition Constants.py:48