template_instantiator/classes.py
Go to the documentation of this file.
1 """Instantiate a class and its members."""
2 
3 import gtwrap.interface_parser as parser
4 from gtwrap.template_instantiator.constructor import InstantiatedConstructor
5 from gtwrap.template_instantiator.helpers import (InstantiationHelper,
6  instantiate_args_list,
7  instantiate_name,
8  instantiate_return_type,
9  instantiate_type)
10 from gtwrap.template_instantiator.method import (InstantiatedMethod,
11  InstantiatedStaticMethod)
12 
13 
14 class InstantiatedClass(parser.Class):
15  """
16  Instantiate the class defined in the interface file.
17  """
18 
19  def __init__(self, original: parser.Class, instantiations=(), new_name=''):
20  """
21  Template <T, U>
22  Instantiations: [T1, U1]
23  """
24  self.original = original
25  self.instantiations = instantiations
26 
27  self.template = None
28  self.is_virtual = original.is_virtual
29  self.parent = original.parent
30 
31  # If the class is templated, check if the number of provided instantiations
32  # match the number of templates, else it's only a partial instantiation which is bad.
33  if original.template:
34  assert len(original.template.typenames) == len(
35  instantiations), "Typenames and instantiations mismatch!"
36 
37  # Get the instantiated name of the class. E.g. FuncDouble
39  original.name, instantiations) if not new_name else new_name
40 
41  # Check for typenames if templated.
42  # By passing in typenames, we can gracefully handle both templated and non-templated classes
43  # This will allow the `This` keyword to be used in both templated and non-templated classes.
44  typenames = self.original.template.typenames if self.original.template else []
45 
46  # Instantiate the parent class, constructors, static methods, properties, respectively.
47  self.parent_class = self.instantiate_parent_class(typenames)
48  self.ctors = self.instantiate_ctors(typenames)
50  self.properties = self.instantiate_properties(typenames)
51 
52  # Instantiate all operator overloads
53  self.operators = self.instantiate_operators(typenames)
54 
55  # Set enums
56  self.enums = original.enums
57 
58  # Instantiate all instance methods
59  self.methods = self.instantiate_methods(typenames)
60 
61  self.dunder_methods = original.dunder_methods
62 
63  super().__init__(
64  self.template,
65  self.is_virtual,
66  self.name,
67  [self.parent_class],
68  self.ctors,
69  self.methods,
70  self.static_methods,
71  self.dunder_methods,
72  self.properties,
73  self.operators,
74  self.enums,
75  parent=self.parent,
76  )
77 
78  def __repr__(self):
79  return "{virtual}Class {cpp_class} : {parent_class}\n"\
80  "{ctors}\n{static_methods}\n{methods}\n{operators}".format(
81  virtual="virtual " if self.is_virtual else '',
82  cpp_class=self.to_cpp(),
83  parent_class=self.parent,
84  ctors="\n".join([repr(ctor) for ctor in self.ctors]),
85  static_methods="\n".join([repr(m)
86  for m in self.static_methods]),
87  methods="\n".join([repr(m) for m in self.methods]),
88  operators="\n".join([repr(op) for op in self.operators])
89  )
90 
91  def instantiate_parent_class(self, typenames):
92  """
93  Instantiate the inherited parent names.
94 
95  Args:
96  typenames: List of template types to instantiate.
97 
98  Return: List of constructors instantiated with provided template args.
99  """
100 
101  if isinstance(self.original.parent_class, parser.type.TemplatedType):
102  return instantiate_type(
103  self.original.parent_class, typenames, self.instantiations,
104  parser.Typename(self.namespaces())).typename
105  else:
106  return self.original.parent_class
107 
108  def instantiate_ctors(self, typenames):
109  """
110  Instantiate the class constructors.
111 
112  Args:
113  typenames: List of template types to instantiate.
114 
115  Return: List of constructors instantiated with provided template args.
116  """
117 
118  helper = InstantiationHelper(
119  instantiation_type=InstantiatedConstructor)
120 
121  instantiated_ctors = helper.multilevel_instantiation(
122  self.original.ctors, typenames, self)
123 
124  return instantiated_ctors
125 
126  def instantiate_static_methods(self, typenames):
127  """
128  Instantiate static methods in the class.
129 
130  Args:
131  typenames: List of template types to instantiate.
132 
133  Return: List of static methods instantiated with provided template args.
134  """
135  helper = InstantiationHelper(
136  instantiation_type=InstantiatedStaticMethod)
137 
138  instantiated_static_methods = helper.multilevel_instantiation(
139  self.original.static_methods, typenames, self)
140 
141  return instantiated_static_methods
142 
143  def instantiate_methods(self, typenames):
144  """
145  Instantiate regular methods in the class.
146 
147  Args:
148  typenames: List of template types to instantiate.
149 
150  Return: List of methods instantiated with provided template args.
151  """
152  instantiated_methods = []
153 
154  helper = InstantiationHelper(instantiation_type=InstantiatedMethod)
155 
156  instantiated_methods = helper.multilevel_instantiation(
157  self.original.methods, typenames, self)
158 
159  return instantiated_methods
160 
161  def instantiate_operators(self, typenames):
162  """
163  Instantiate the class-level template in the operator overload.
164 
165  Args:
166  typenames: List of template types to instantiate.
167 
168  Return: List of methods instantiated with provided template args on the class.
169  """
170  instantiated_operators = []
171  for operator in self.original.operators:
172  instantiated_args = instantiate_args_list(
173  operator.args.list(),
174  typenames,
175  self.instantiations,
176  self.cpp_typename(),
177  )
178  instantiated_operators.append(
179  parser.Operator(
180  name=operator.name,
181  operator=operator.operator,
182  return_type=instantiate_return_type(
183  operator.return_type,
184  typenames,
185  self.instantiations,
186  self.cpp_typename(),
187  ),
188  args=parser.ArgumentList(instantiated_args),
189  is_const=operator.is_const,
190  parent=self,
191  ))
192  return instantiated_operators
193 
194  def instantiate_properties(self, typenames):
195  """
196  Instantiate the class properties.
197 
198  Args:
199  typenames: List of template types to instantiate.
200 
201  Return: List of properties instantiated with provided template args.
202  """
203  instantiated_ = instantiate_args_list(
204  self.original.properties,
205  typenames,
206  self.instantiations,
207  self.cpp_typename(),
208  )
209  # Convert to type Variable
210  instantiated_properties = [
211  parser.Variable(ctype=[arg.ctype],
212  name=arg.name,
213  default=arg.default) for arg in instantiated_
214  ]
215  return instantiated_properties
216 
217  def cpp_typename(self):
218  """
219  Return a parser.Typename including namespaces and cpp name of this
220  class.
221  """
222  if self.original.template:
223  name = "{}<{}>".format(
224  self.original.name,
225  ", ".join([inst.to_cpp() for inst in self.instantiations]))
226  else:
227  name = self.original.name
228  namespaces_name = self.namespaces()
229  namespaces_name.append(name)
230  return parser.Typename(namespaces_name)
231 
232  def to_cpp(self):
233  """Generate the C++ code for wrapping."""
234  return self.cpp_typename().to_cpp()
gtwrap.template_instantiator.classes.InstantiatedClass.instantiations
instantiations
Definition: template_instantiator/classes.py:25
gtwrap.template_instantiator.classes.InstantiatedClass.template
template
Definition: template_instantiator/classes.py:27
gtwrap.template_instantiator.constructor
Definition: constructor.py:1
gtwrap.template_instantiator.classes.InstantiatedClass.dunder_methods
dunder_methods
Definition: template_instantiator/classes.py:61
format
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:226
gtwrap.template_instantiator.classes.InstantiatedClass.parent_class
parent_class
Definition: template_instantiator/classes.py:47
gtwrap.template_instantiator.classes.InstantiatedClass.original
original
Definition: template_instantiator/classes.py:24
gtwrap.template_instantiator.classes.InstantiatedClass.name
name
Definition: template_instantiator/classes.py:38
gtwrap.template_instantiator.helpers
Definition: helpers.py:1
gtwrap.template_instantiator.classes.InstantiatedClass.operators
operators
Definition: template_instantiator/classes.py:53
gtwrap.template_instantiator.classes.InstantiatedClass.methods
methods
Definition: template_instantiator/classes.py:59
gtwrap.template_instantiator.classes.InstantiatedClass.properties
properties
Definition: template_instantiator/classes.py:50
gtwrap.template_instantiator.classes.InstantiatedClass.static_methods
static_methods
Definition: template_instantiator/classes.py:49
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:842
gtwrap.template_instantiator.classes.InstantiatedClass.instantiate_operators
def instantiate_operators(self, typenames)
Definition: template_instantiator/classes.py:161
gtwrap.template_instantiator.classes.InstantiatedClass.instantiate_ctors
def instantiate_ctors(self, typenames)
Definition: template_instantiator/classes.py:108
gtwrap.template_instantiator.classes.InstantiatedClass.__init__
def __init__(self, parser.Class original, instantiations=(), new_name='')
Definition: template_instantiator/classes.py:19
gtwrap.template_instantiator.classes.InstantiatedClass
Definition: template_instantiator/classes.py:14
gtwrap.template_instantiator.classes.InstantiatedClass.to_cpp
def to_cpp(self)
Definition: template_instantiator/classes.py:232
gtwrap.template_instantiator.helpers.instantiate_return_type
def instantiate_return_type(parser.ReturnType return_type, Sequence[parser.template.Typename] template_typenames, Sequence[parser.Typename] instantiations, parser.Typename cpp_typename, 'InstantiatedClass' instantiated_class=None)
Definition: helpers.py:173
gtwrap.template_instantiator.helpers.instantiate_type
parser.Type instantiate_type(parser.Type ctype, Sequence[str] template_typenames, Sequence[parser.Typename] instantiations, parser.Typename cpp_typename, 'InstantiatedClass' instantiated_class=None)
Definition: helpers.py:31
gtwrap.template_instantiator.helpers.InstantiationHelper
Definition: helpers.py:215
gtwrap.template_instantiator.classes.InstantiatedClass.__repr__
def __repr__(self)
Definition: template_instantiator/classes.py:78
gtwrap.template_instantiator.helpers.instantiate_name
def instantiate_name(str original_name, Sequence[parser.Typename] instantiations)
Definition: helpers.py:196
gtwrap.template_instantiator.method
Definition: method.py:1
gtwrap.template_instantiator.classes.InstantiatedClass.parent
parent
Definition: template_instantiator/classes.py:29
gtwrap.template_instantiator.classes.InstantiatedClass.instantiate_methods
def instantiate_methods(self, typenames)
Definition: template_instantiator/classes.py:143
gtwrap.template_instantiator.helpers.instantiate_args_list
def instantiate_args_list(Sequence[parser.Argument] args_list, Sequence[parser.template.Typename] template_typenames, Sequence instantiations, parser.Typename cpp_typename)
Definition: helpers.py:144
gtwrap.template_instantiator.classes.InstantiatedClass.instantiate_static_methods
def instantiate_static_methods(self, typenames)
Definition: template_instantiator/classes.py:126
gtwrap.template_instantiator.classes.InstantiatedClass.is_virtual
is_virtual
Definition: template_instantiator/classes.py:28
gtwrap.template_instantiator.classes.InstantiatedClass.cpp_typename
def cpp_typename(self)
Definition: template_instantiator/classes.py:217
gtwrap.interface_parser
Definition: wrap/gtwrap/interface_parser/__init__.py:1
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
gtwrap.template_instantiator.classes.InstantiatedClass.ctors
ctors
Definition: template_instantiator/classes.py:48
gtwrap.template_instantiator.classes.InstantiatedClass.instantiate_parent_class
def instantiate_parent_class(self, typenames)
Definition: template_instantiator/classes.py:91
gtwrap.template_instantiator.classes.InstantiatedClass.enums
enums
Definition: template_instantiator/classes.py:56
gtwrap.template_instantiator.classes.InstantiatedClass.instantiate_properties
def instantiate_properties(self, typenames)
Definition: template_instantiator/classes.py:194
repr
str repr(handle h)
Definition: pytypes.h:2467


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:58