Peano
Loading...
Searching...
No Matches
UserDefinedType.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 user-defined attribute
10
11 Wrapper around user-defined object, i.e. an instance of a proper class that
12 is not built into C++. At the moment, we support this only for static
13 attributes. On the one hand, having real object attributes which stem from
14 user code is tricky (are there appropriate to string methods, appropriate
15 constructors) for a "normal", serial code. It becomes close to impossible
16 to map complex nested classes onto MPI. Therefore, this constraint to
17 class attributes.
18
19 """
20
22 self,
23 name,
24 type,
25 include,
26 ifdefs=[],
27 qualifier=Attribute.Qualifier.NONE,
28 initval=None,
29 getter_returns_reference=True,
30 user_type_has_toString_method=True,
31 ):
32 """!
33
34 Create attribute
35
36 @param name Name of the attribute
37
38 @param type Fully qualified type of the attribute as string separated
39 with two colons (::)
40
41 @param include String which holds the include of the type
42
43 For all other attributes see superclass' constructor.
44
45 """
46 Attribute.__init__(
47 self,
48 name,
49 ifdefs,
50 qualifier=qualifier,
51 initval=initval,
52 expose_in_header_file=qualifier == Attribute.Qualifier.CONSTEXPR,
53 )
54
55 self._type = type
56 self._include = include
57 self._getter_returns_reference = getter_returns_reference
58 self._user_type_has_toString_method = user_type_has_toString_method
59
60 assert (
61 qualifier == Attribute.Qualifier.STATIC
62 or qualifier == Attribute.Qualifier.CONST_STATIC
63 )
64
65 pass
66
67 def get_methods(self, _full_qualified_class_name, for_declaration=True):
68 accessor_name = self.get_accessor_name()
69 methods = []
70
71 if self._is_static:
72 if for_declaration and self._getter_returns_reference:
73 getter = ("get" + accessor_name + "()", "static " + self._type + "& ")
74 elif for_declaration and not self._getter_returns_reference:
75 getter = ("get" + accessor_name + "()", "static " + self._type)
77 getter = ("get" + accessor_name + "()", self._type + "& ")
78 else:
79 getter = ("get" + accessor_name + "()", self._type)
80
81 elif self._is_const_static:
82 if for_declaration and self._getter_returns_reference:
83 getter = ("get" + accessor_name + "()", "static " + self._type + "& ")
84 elif for_declaration and not self._getter_returns_reference:
85 getter = ("get" + accessor_name + "()", "static " + self._type)
87 getter = ("get" + accessor_name + "()", self._type + "& ")
88 else:
89 getter = ("get" + accessor_name + "()", self._type)
90
91 elif self._is_const:
92 getter = ("get" + accessor_name + "() const", "const " + self._type)
93
94 elif self._is_constexpr:
95 getter = ("get" + accessor_name + "() const", "constexpr " + self._type)
96
97 else:
98 # default case.
99 getter = ("get" + accessor_name + "() const", self._type)
100
101 methods.append(getter)
102
103 # Add a setter method, if it makes sense.
104 if not (
106 or self._is_const
107 or self._is_constexpr
108 or self._is_static
109 ):
110 methods.append(
111 ("set" + accessor_name + "(const " + self._type + "& value)", "void")
112 )
113
114 return methods
115
116 def get_plain_C_attributes(self, for_constructor=False):
117 typestring = self._type
118 namestring = "_" + self._name
119
120 # add const/static qualifiers and initialization values, if needed
121 if self._is_static:
122 typestring = "inline static " + self._type
123 if self._initval is not None:
124 namestring += " = " + str(self._initval)
125
126 elif self._is_const_static:
127 typestring = "inline const static " + self._type
128 namestring += " = " + str(self._initval)
129
130 elif self._is_const:
131 typestring = "const " + self._type
132 namestring += " = " + str(self._initval)
133
134 elif self._is_constexpr:
135 typestring = "constexpr static " + self._type
136 namestring += " = " + str(self._initval)
137
138 else:
139 # default case
140 # If an initial value is provided, add it
141 # However, only add it if we need it for a definition,
142 # not as optional argument for constructor method
143 if self._initval is not None and not for_constructor:
144 namestring += " = " + str(self._initval)
145
146 return [(namestring, typestring, "")]
147
148 def get_method_body(self, signature):
149 if signature.startswith("get") and self.use_data_store:
150 return " return _dataStore._" + self._name + ";\n"
151 elif signature.startswith("get") and not self.use_data_store:
152 return " return _" + self._name + ";\n"
153 elif signature.startswith("set") and self.use_data_store:
154 return " _dataStore._" + self._name + " = value;\n"
155 elif signature.startswith("set") and not self.use_data_store:
156 return " _" + self._name + " = value;\n"
157 else:
158 assert False
159 return ""
160
162 """
163
164 I map the type onto a byte field,but it is not clear if this
165 actually works. At the moment, we do not use the MPI data
166 implicitly, as we support user-defined attributes if and only
167 if they are static. That is, we never exchange them as part of
168 an object. That is, at the moment this routine is kind of
169 deprecated.
170
171 """
172 return [("MPI_BYTE", "sizeof(" + self._type + ")")]
173
174 def get_to_string(self):
175 """
176
177 Return string representation of attribute.
178
179 """
181 return "_dataStore._" + self._name + ".toString()"
183 return "_" + self._name + ".toString()"
184 else:
185 return """ "<no toString() method>" """
186
187 def get_includes(self):
188 return self._include
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 user-defined attribute.
get_plain_C_attributes(self, for_constructor=False)
Return list of n-tuples.
get_to_string(self)
Return string representation of attribute.
__init__(self, name, type, include, ifdefs=[], qualifier=Attribute.Qualifier.NONE, initval=None, getter_returns_reference=True, user_type_has_toString_method=True)
Create attribute.
get_methods(self, _full_qualified_class_name, for_declaration=True)
Return sequence of methods that are defined for this attribute.
get_method_body(self, signature)
I hand in the method signature (see get_methods()) and wanna get the whole implementation.
get_native_MPI_type(self)
I map the type onto a byte field,but it is not clear if this actually works.