Peano
Loading...
Searching...
No Matches
String.py
Go to the documentation of this file.
1# This file is part of the DaStGen2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3from .Attribute import Attribute
4
5
7 """!
8
9 Wrapper around C++ string which is not a dataype supported by
10 MPI natively.
11
12 max_length Maximum length of the strings that can be handled.
13 The shorter you keep this value, the smaller the
14 MPI message size, as we don't use dynamic data
15 structures. We always map the C++ string onto an
16 array with max_length entries.
17
18 """
19
21 self,
22 name,
23 max_length=80,
24 ifdefs=[],
25 qualifier=Attribute.Qualifier.NONE,
26 initval=None,
27 ):
28 if initval is not None:
29 # sanitize initval
30 # Make sure it starts with a `"`
31 if not initval.startswith('"'):
32 initval = '"' + initval
33 # and that it ends with a `\0"`
34 if not initval.endswith('\\0"'):
35 if initval.endswith('"'):
36 # only cut off trailing '"'
37 # will be added back one line below, after the null character
38 initval = initval[:-1]
39 initval += '\\0"'
40
41 # Do we need to expose this attribute in the header file?
42 # Only necessary for functions that will be inlined, i.e. constexpr
43 expose = qualifier == Attribute.Qualifier.CONSTEXPR
44
45 Attribute.__init__(
46 self,
47 name,
48 ifdefs,
49 qualifier=qualifier,
50 initval=initval,
51 expose_in_header_file=expose,
52 )
53 self._max_length = max_length
54
55 return
56
57 def get_methods(self, _full_qualified_class_name, for_declaration=True):
58 accessor_name = self.get_accessor_name()
59 methods = []
60
61 if self._is_static:
62 if for_declaration:
63 # No 'const' here: static member function cannot have 'const' qualifier
64 getter = ("get" + accessor_name + "()", "static std::string")
65 else:
66 # No 'static' here: 'static' can only be specified inside the class definition
67 getter = ("get" + accessor_name + "()", "std::string")
68
69 elif self._is_const_static:
70 if for_declaration:
71 # No 'const' here: static member function cannot have 'const' qualifier
72 getter = ("get" + accessor_name + "()", "static std::string")
73 else:
74 # No 'static' here: 'static' can only be specified inside the class definition
75 # No 'const' here either: function definition will then differ from that in declaration
76 getter = ("get" + accessor_name + "()", "std::string")
77
78 elif self._is_const:
79 getter = ("get" + accessor_name + "() const", "const std::string")
80
82 raise NotImplementedError("Strings + constexpr not implemented yet.")
83 # getter = ("get" + accessor_name + "() const [80]", "constexpr char")
84
85 else:
86 # default case.
87 getter = ("get" + accessor_name + "() const", "std::string")
88
89 methods.append(getter)
90
91 # Add a setter method, if it makes sense.
93 methods.append(
94 ("set" + accessor_name + "(const std::string& value)", "void")
95 )
96
97 return methods
98
99 def get_plain_C_attributes(self, for_constructor=False):
100 typestring = "char"
101 namestring = "_" + self._name + "[" + str(self._max_length) + "]"
102 lengthtype = (
103 "int" # type + qualifiers string for integer carrying string length
104 )
105 lengthval = None
106
107 # add const/static qualifiers and initialization values, if needed
108 if self._is_static:
109 typestring = "inline static char"
110 lengthtype = "inline static int"
111
112 if self._initval is not None:
113 namestring += " = " + self._initval
114 # initval is sanitized at this point. Take away 4:
115 # 2 aposprophes, and 2 extra '\' to escape the NULL char
116 lengthval = len(self._initval) - 4
117 lengthtype = "inline static int"
118
119 elif self._is_const_static:
120 typestring = "inline const static char"
121 namestring += " = " + self._initval
122 # initval is sanitized at this point. Take away 4:
123 # 2 aposprophes, and 2 extra '\' to escape the NULL char
124 lengthval = len(self._initval) - 4
125 lengthtype = "inline const static int"
126
127 elif self._is_const:
128 typestring = "const char"
129 namestring += " = " + self._initval
130 # initval is sanitized at this point. Take away 4:
131 # 2 aposprophes, and 2 extra '\' to escape the NULL char
132 lengthval = len(self._initval) - 4
133 lengthtype = "const int"
134
136 raise NotImplementedError("Strings + constexpr not implemented yet.")
137 # typestring = "constexpr static char"
138 # namestring += " = " + self._initval
139 # lengthval = len(self._initval) - 4
140 # lengthtype = "constexpr int"
141
142 else:
143 # default case
144 # If an initial value is provided, add it
145 # However, only add it if we need it for a definition,
146 # not as optional argument for constructor method
147 if self._initval is not None and not for_constructor:
148 namestring += " = " + self._initval
149 # initval is sanitized at this point. Take away 4:
150 # 2 aposprophes, and 2 extra '\' to escape the NULL char
151 lengthval = len(self._initval) - 4
152
153 # construct name of integer which contains string's length
154 lengthstring = "_" + self._name + "Length"
155 if lengthval is not None:
156 # if lengthval has a value, it means that we are using
157 # some intial value. So add the correct value of the length.
158 lengthstring += " = " + str(lengthval)
159
160 return [
161 (namestring, typestring, ""),
162 (lengthstring, lengthtype),
163 ]
164
166 return self._name
167
168 def get_method_body(self, signature):
169 if signature.startswith("get"):
170 return (
171 """
172 std::ostringstream result;
173 for (int i=0; i<_"""
174 + self._name
175 + """Length; i++) {
176 result << static_cast<char>(_"""
177 + self._name
178 + """[i]);
179 }
180 return result.str();
181"""
182 )
183 elif signature.startswith("set"):
184 return (
185 """
186 _"""
187 + self._name
188 + """Length = value.length();
189 for (int i=0; i<value.length(); i++) {
190 _"""
191 + self._name
192 + """[i] = value.data()[i];
193 }
194
195"""
196 )
197 else:
198 assert False
199 return ""
200
202 return [("MPI_CHAR", self._max_length), ("MPI_INT", 1)]
203
204 def get_to_string(self):
205 """
206
207 Return string representation of attribute.
208
209 """
210 return "get" + self.get_accessor_name() + "()"
Represents one attribute.
Definition Attribute.py:8
get_accessor_name(self)
Generate the accessor name used throughout dastgen2 to create variables, function names,...
Definition Attribute.py:259
Wrapper around C++ string which is not a dataype supported by MPI natively.
Definition String.py:6
get_methods(self, _full_qualified_class_name, for_declaration=True)
Return sequence of methods that are defined for this attribute.
Definition String.py:57
get_to_string(self)
Return string representation of attribute.
Definition String.py:204
get_method_body(self, signature)
I hand in the method signature (see get_methods()) and wanna get the whole implementation.
Definition String.py:168
get_native_MPI_type(self)
Return native (built-in) MPI datatype.
Definition String.py:201
get_plain_C_attributes(self, for_constructor=False)
Return list of n-tuples.
Definition String.py:99
__init__(self, name, max_length=80, ifdefs=[], qualifier=Attribute.Qualifier.NONE, initval=None)
name: String This is a plain string which has to follow the C++ naming conventions,...
Definition String.py:27