2020# Modified by Massimiliano Leoni, 2016
2121
2222from ufl .log import error
23+ # from ufl.core.ufl_type import UFLType
24+ from ufl .utils .formatting import camel2underscore
25+
26+
27+ class UFLType (type ):
28+ """Base class for all UFL types.
29+
30+ Equip UFL types with properties such as:
31+
32+ - `_ufl_typecode_`: The integer typecode is a contiguous index different for each
33+ type. This is used for fast lookup into e.g. multifunction handler tables.
34+
35+ - `_ufl_num_typecodes_`: A global counter of the number of typecodes assigned.
36+
37+ """
38+
39+ def __init__ (cls , name , bases , attrs ):
40+ # Determine integer typecode by incrementally counting all types
41+ cls ._ufl_typecode_ = UFLType ._ufl_num_typecodes_
42+ UFLType ._ufl_num_typecodes_ += 1
43+
44+ UFLType ._ufl_all_classes_ .append (cls )
45+
46+ # Determine handler name by a mapping from "TypeName" to "type_name"
47+ cls ._ufl_handler_name_ = camel2underscore (cls .__name__ )
48+ UFLType ._ufl_all_handler_names_ .add (cls ._ufl_handler_name_ )
49+
50+ # Append space for counting object creation and destriction of
51+ # this this type.
52+ UFLType ._ufl_obj_init_counts_ .append (0 )
53+ UFLType ._ufl_obj_del_counts_ .append (0 )
54+
55+ # A global counter of the number of typecodes assigned.
56+ _ufl_num_typecodes_ = 0
57+
58+ # A global array of all Expr subclasses, indexed by typecode
59+ _ufl_all_classes_ = []
60+
61+ # A global set of all handler names added
62+ _ufl_all_handler_names_ = set ()
63+
64+ # A global array of the number of initialized objects for each
65+ # typecode
66+ _ufl_obj_init_counts_ = []
67+
68+ # A global array of the number of deleted objects for each
69+ # typecode
70+ _ufl_obj_del_counts_ = []
71+
72+ # Type trait: If the type is abstract. An abstract class cannot
73+ # be instantiated and does not need all properties specified.
74+ _ufl_is_abstract_ = True
75+
76+ # Type trait: If the type is terminal.
77+ _ufl_is_terminal_ = None
2378
2479
2580# --- The base object for all UFL expression tree nodes ---
2681
27- class Expr (object ):
82+ class Expr (object , metaclass = UFLType ):
2883 """Base class for all UFL expression types.
2984
3085 *Instance properties*
@@ -128,22 +183,10 @@ def __init__(self):
128183 # implement for this type in a multifunction.
129184 _ufl_handler_name_ = "expr"
130185
131- # The integer typecode, a contiguous index different for each
132- # type. This is used for fast lookup into e.g. multifunction
133- # handler tables.
134- _ufl_typecode_ = 0
135-
136186 # Number of operands, "varying" for some types, or None if not
137187 # applicable for abstract types.
138188 _ufl_num_ops_ = None
139189
140- # Type trait: If the type is abstract. An abstract class cannot
141- # be instantiated and does not need all properties specified.
142- _ufl_is_abstract_ = True
143-
144- # Type trait: If the type is terminal.
145- _ufl_is_terminal_ = None
146-
147190 # Type trait: If the type is a literal.
148191 _ufl_is_literal_ = None
149192
@@ -227,15 +270,6 @@ def __init__(self):
227270
228271 # --- Global variables for collecting all types ---
229272
230- # A global counter of the number of typecodes assigned
231- _ufl_num_typecodes_ = 1
232-
233- # A global set of all handler names added
234- _ufl_all_handler_names_ = set ()
235-
236- # A global array of all Expr subclasses, indexed by typecode
237- _ufl_all_classes_ = []
238-
239273 # A global dict mapping language_operator_name to the type it
240274 # produces
241275 _ufl_language_operators_ = {}
@@ -245,14 +279,6 @@ def __init__(self):
245279
246280 # --- Mechanism for profiling object creation and deletion ---
247281
248- # A global array of the number of initialized objects for each
249- # typecode
250- _ufl_obj_init_counts_ = [0 ]
251-
252- # A global array of the number of deleted objects for each
253- # typecode
254- _ufl_obj_del_counts_ = [0 ]
255-
256282 # Backup of default init and del
257283 _ufl_regular__init__ = __init__
258284
@@ -425,8 +451,6 @@ def geometric_dimension(self):
425451# Initializing traits here because Expr is not defined in the class
426452# declaration
427453Expr ._ufl_class_ = Expr
428- Expr ._ufl_all_handler_names_ .add (Expr )
429- Expr ._ufl_all_classes_ .append (Expr )
430454
431455
432456def ufl_err_str (expr ):
0 commit comments