method.py
Go to the documentation of this file.
1 """Class method and static method instantiators."""
2 
3 from typing import Iterable
4 
5 import gtwrap.interface_parser as parser
6 from gtwrap.template_instantiator.helpers import (instantiate_name,
7  instantiate_return_type)
8 
9 
10 class InstantiatedMethod(parser.Method):
11  """
12  Instantiate method with template parameters.
13 
14  E.g.
15  class A {
16  template<X, Y>
17  void func(X x, Y y);
18  }
19  """
20  def __init__(self,
21  original: parser.Method,
22  instantiations: Iterable[parser.Typename] = ()):
23  self.original = original
24  self.instantiations = instantiations
25  self.template = original.template
26  self.is_const = original.is_const
27  self.parent = original.parent
28 
29  self.name = instantiate_name(original.name, self.instantiations)
30  self.return_type = original.return_type
31  self.args = original.args
32 
33  super().__init__(self.template,
34  self.name,
35  self.return_type,
36  self.args,
37  self.is_const,
38  parent=self.parent)
39 
40  @classmethod
41  def construct(cls, original, typenames, class_instantiations,
42  method_instantiations, instantiated_args, parent):
43  """Class method to construct object as required by InstantiationHelper."""
44  method = parser.Method(
45  template=original.template,
46  name=original.name,
47  return_type=instantiate_return_type(
48  original.return_type, typenames,
49  class_instantiations + method_instantiations,
50  parent.cpp_typename()),
51  args=parser.ArgumentList(instantiated_args),
52  is_const=original.is_const,
53  parent=parent,
54  )
55  return InstantiatedMethod(method, instantiations=method_instantiations)
56 
57  def to_cpp(self):
58  """Generate the C++ code for wrapping."""
59  if self.original.template:
60  # to_cpp will handle all the namespacing and templating
61  instantiation_list = [x.to_cpp() for x in self.instantiations]
62  # now can simply combine the instantiations, separated by commas
63  ret = "{}<{}>".format(self.original.name,
64  ",".join(instantiation_list))
65  else:
66  ret = self.original.name
67  return ret
68 
69  def __repr__(self):
70  return "Instantiated {}".format(super().__repr__())
71 
72 
73 class InstantiatedStaticMethod(parser.StaticMethod):
74  """
75  Instantiate static method with template parameters.
76  """
77  def __init__(self,
78  original: parser.StaticMethod,
79  instantiations: Iterable[parser.Typename] = ()):
80  self.original = original
81  self.instantiations = instantiations
82 
83  self.name = instantiate_name(original.name, self.instantiations)
84  self.return_type = original.return_type
85  self.args = original.args
86  self.template = original.template
87  self.parent = original.parent
88 
89  super().__init__(self.name, self.return_type, self.args, self.template,
90  self.parent)
91 
92  @classmethod
93  def construct(cls, original, typenames, class_instantiations,
94  method_instantiations, instantiated_args, parent):
95  """Class method to construct object as required by InstantiationHelper."""
96  method = parser.StaticMethod(
97  name=original.name,
98  return_type=instantiate_return_type(original.return_type,
99  typenames,
100  class_instantiations +
101  method_instantiations,
102  parent.cpp_typename(),
103  instantiated_class=parent),
104  args=parser.ArgumentList(instantiated_args),
105  template=original.template,
106  parent=parent,
107  )
108  return InstantiatedStaticMethod(method,
109  instantiations=method_instantiations)
110 
111  def to_cpp(self):
112  """Generate the C++ code for wrapping."""
113  if self.original.template:
114  # to_cpp will handle all the namespacing and templating
115  instantiation_list = [x.to_cpp() for x in self.instantiations]
116  # now can simply combine the instantiations, separated by commas
117  ret = "{}<{}>".format(self.original.name,
118  ",".join(instantiation_list))
119  else:
120  ret = self.original.name
121  return ret
122 
123  def __repr__(self):
124  return "Instantiated {}".format(super().__repr__())
gtwrap.template_instantiator.method.InstantiatedMethod.template
template
Definition: method.py:23
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.method.InstantiatedMethod.is_const
is_const
Definition: method.py:24
gtwrap.template_instantiator.method.InstantiatedStaticMethod.to_cpp
def to_cpp(self)
Definition: method.py:111
gtwrap.template_instantiator.method.InstantiatedStaticMethod.original
original
Definition: method.py:78
gtwrap.template_instantiator.method.InstantiatedStaticMethod.__repr__
def __repr__(self)
Definition: method.py:123
gtwrap.template_instantiator.helpers
Definition: helpers.py:1
gtwrap.template_instantiator.method.InstantiatedMethod.return_type
return_type
Definition: method.py:28
gtwrap.template_instantiator.method.InstantiatedMethod.__init__
def __init__(self, parser.Method original, Iterable[parser.Typename] instantiations=())
Definition: method.py:20
gtwrap.template_instantiator.method.InstantiatedStaticMethod.parent
parent
Definition: method.py:85
gtwrap.template_instantiator.method.InstantiatedStaticMethod
Definition: method.py:73
gtwrap.template_instantiator.method.InstantiatedStaticMethod.return_type
return_type
Definition: method.py:82
gtwrap.template_instantiator.method.InstantiatedStaticMethod.__init__
def __init__(self, parser.StaticMethod original, Iterable[parser.Typename] instantiations=())
Definition: method.py:77
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.method.InstantiatedMethod
Definition: method.py:10
gtwrap.template_instantiator.method.InstantiatedMethod.instantiations
instantiations
Definition: method.py:22
gtwrap.template_instantiator.method.InstantiatedMethod.__repr__
def __repr__(self)
Definition: method.py:69
gtwrap.template_instantiator.method.InstantiatedStaticMethod.construct
def construct(cls, original, typenames, class_instantiations, method_instantiations, instantiated_args, parent)
Definition: method.py:93
gtwrap.template_instantiator.method.InstantiatedStaticMethod.instantiations
instantiations
Definition: method.py:79
gtwrap.template_instantiator.helpers.instantiate_name
def instantiate_name(str original_name, Sequence[parser.Typename] instantiations)
Definition: helpers.py:196
gtwrap.template_instantiator.method.InstantiatedMethod.parent
parent
Definition: method.py:25
gtwrap.template_instantiator.method.InstantiatedMethod.args
args
Definition: method.py:29
gtwrap.template_instantiator.method.InstantiatedMethod.name
name
Definition: method.py:27
gtwrap.interface_parser
Definition: wrap/gtwrap/interface_parser/__init__.py:1
gtwrap.template_instantiator.method.InstantiatedMethod.to_cpp
def to_cpp(self)
Definition: method.py:57
gtwrap.template_instantiator.method.InstantiatedStaticMethod.args
args
Definition: method.py:83
gtwrap.template_instantiator.method.InstantiatedMethod.original
original
Definition: method.py:21
gtwrap.template_instantiator.method.InstantiatedMethod.construct
def construct(cls, original, typenames, class_instantiations, method_instantiations, instantiated_args, parent)
Definition: method.py:41
gtwrap.template_instantiator.method.InstantiatedStaticMethod.template
template
Definition: method.py:84
gtwrap.template_instantiator.method.InstantiatedStaticMethod.name
name
Definition: method.py:81


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:10