Peano
Loading...
Searching...
No Matches
Enumeration.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++ enumerations which is not a datatype supported
10 natively by MPI.
11
12 The attribute has two-fold meaning. It defines a enum class subtype
13 within the generated code and it creates a new attribute of this
14 type.
15
16 :Arguments:
17
18 name: String
19 Something that can become a C++ identifier
20
21 variants: [String]
22 Sequence of strings. The strings have to be something C++ accepts
23 as enumeration identifiers.
24
25 """
26
28 self,
29 name,
30 variants,
31 ifdefs=[],
32 qualifier=Attribute.Qualifier.NONE,
33 initval=None,
34 ):
35 if qualifier != Attribute.Qualifier.NONE:
36 raise NotImplementedError(
37 f"Attribute {name}: This data type can't work with qualifiers"
38 )
39 Attribute.__init__(
40 self, name, ifdefs=ifdefs, qualifier=qualifier, initval=initval
41 )
42 self._variants = variants
43 self.compress = True
44
45 def _enum_name(self):
46 return self._name_name[:1].title() + self._name_name[1:]
47
49 result = (
50 """
51 enum class """
52 + self._enum_name()
53 + """: int {
54 """
55 )
56
57 for i in self._variants:
58 if self._variants.index(i) != 0:
59 result += ", "
60 result += i
61 result += "="
62 result += str(self._variants.index(i))
63
64 result += """
65 };"""
66 return result
67
68 def get_methods(self, _full_qualified_name, for_declaration=True):
69 accessor_name = self.get_accessor_name()
70
71 toStringQualifier = ""
72 if for_declaration:
73 toStringQualifier = "static"
74
75 return [
76 (
77 "get" + accessor_name + "() const",
78 _full_qualified_name + "::" + accessor_name,
79 ),
80 ("set" + accessor_name + "(" + accessor_name + " value)", "void"),
81 # This method provides a way to get enums as strings.
82 (
83 "toString(" + accessor_name + " value)",
84 toStringQualifier + " std::string",
85 ),
86 ]
87
88 def get_plain_C_attributes(self, for_constructor=False):
89 if self.compress:
90 return [("_" + self._name_name, self._enum_name(), "[[clang::pack]]")]
91 else:
92 return [("_" + self._name_name, self._enum_name(), "")]
93
94 def get_method_body(self, signature):
95 if signature.startswith("get") and self.use_data_store:
96 return " return _dataStore._" + self._name_name + ";\n"
97 elif signature.startswith("set") and self.use_data_store:
98 return " _dataStore._" + self._name_name + " = value;\n"
99 elif signature.startswith("get") and not self.use_data_store:
100 return " return _" + self._name_name + ";\n"
101 elif signature.startswith("set") and not self.use_data_store:
102 return " _" + self._name_name + " = value;\n"
103 elif signature.startswith("toString") and not self.use_data_store:
104 return self._get_to_string_method_body()
105 else:
106 assert False
107 return ""
108
110 return [("MPI_INT", 1)]
111
113 """
114 This function generates the method body for the toString(value)
115 method. The idea is to have an easy way to access enum variants as
116 strings for meaningful printouts, such as in the yourDataModel.toString()
117 method.
118
119 For developing and debugging purposes, it can be useful to have a way
120 of accessing these names, so this method gets a standalone definition.
121 """
122 result = " std::ostringstream out;\n out "
123
124 for i in self._variants:
125 result += " << "
126 result += "(value =="
127 result += self._enum_name()
128 result += "::"
129 result += i
130 result += '? "'
131 result += i
132 result += '" : "") '
133 result += ";\n"
134 result += " return out.str();\n"
135
136 return result
137
138 def get_to_string(self):
139 """
140 This function generates the method body which is called in
141 yourDataModel.toString() method.
142 """
143 accessor_name = self.get_accessor_name()
144 varname = "_" + self.name
145 if self.use_data_store:
146 varname = "_dataStore._" + self._name_name
147
148 return "toString(" + varname + ")"
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
name(self)
I expect that there's at least one setter/getter pair.
Definition Attribute.py:279
Wrapper around C++ enumerations which is not a datatype supported natively by MPI.
Definition Enumeration.py:6
__init__(self, name, variants, ifdefs=[], qualifier=Attribute.Qualifier.NONE, initval=None)
name: String This is a plain string which has to follow the C++ naming conventions,...
_get_to_string_method_body(self)
This function generates the method body for the toString(value) method.
get_public_fields(self)
Return string that is to be embedded into the public part of the class definition.
get_native_MPI_type(self)
Return native (built-in) MPI datatype.
get_plain_C_attributes(self, for_constructor=False)
Return list of n-tuples.
get_methods(self, _full_qualified_name, for_declaration=True)
Return sequence of methods that are defined for this attribute.
get_to_string(self)
This function generates the method body which is called in yourDataModel.toString() method.
get_method_body(self, signature)
I hand in the method signature (see get_methods()) and wanna get the whole implementation.