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__())
def construct(cls, original, typenames, class_instantiations, method_instantiations, instantiated_args, parent)
Definition: method.py:94
def construct(cls, original, typenames, class_instantiations, method_instantiations, instantiated_args, parent)
Definition: method.py:42
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:55