template_instantiator/function.py
Go to the documentation of this file.
1 """Instantiate global function."""
2 
3 import gtwrap.interface_parser as parser
4 from gtwrap.template_instantiator.helpers import (instantiate_args_list,
5  instantiate_name,
6  instantiate_return_type)
7 
8 
9 class InstantiatedGlobalFunction(parser.GlobalFunction):
10  """
11  Instantiate global functions.
12 
13  E.g.
14  template<T = {double}>
15  T add(const T& x, const T& y);
16  """
17  def __init__(self, original, instantiations=(), new_name=''):
18  self.original = original
19  self.instantiations = instantiations
20  self.template = ''
21  self.parent = original.parent
22 
23  if not original.template:
24  self.name = original.name
25  self.return_type = original.return_type
26  self.args = original.args
27  else:
28  self.name = instantiate_name(
29  original.name, instantiations) if not new_name else new_name
31  original.return_type,
32  self.original.template.typenames,
33  self.instantiations,
34  # Keyword type name `This` should already be replaced in the
35  # previous class template instantiation round.
36  cpp_typename='',
37  )
38  instantiated_args = instantiate_args_list(
39  original.args.list(),
40  self.original.template.typenames,
41  self.instantiations,
42  # Keyword type name `This` should already be replaced in the
43  # previous class template instantiation round.
44  cpp_typename='',
45  )
46  self.args = parser.ArgumentList(instantiated_args)
47 
48  super().__init__(self.name,
49  self.return_type,
50  self.args,
51  self.template,
52  parent=self.parent)
53 
54  def to_cpp(self):
55  """Generate the C++ code for wrapping."""
56  if self.original.template:
57  instantiated_names = [
58  "::".join(inst.namespaces + [inst.instantiated_name()])
59  for inst in self.instantiations
60  ]
61  ret = "{}<{}>".format(self.original.name,
62  ",".join(instantiated_names))
63  else:
64  ret = self.original.name
65  return ret
66 
67  def __repr__(self):
68  return "Instantiated {}".format(
69  super(InstantiatedGlobalFunction, self).__repr__())
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:14