template_instantiator/namespace.py
Go to the documentation of this file.
1 """Instantiate a namespace."""
2 
3 import itertools
4 
5 import gtwrap.interface_parser as parser
6 from gtwrap.template_instantiator.classes import InstantiatedClass
7 from gtwrap.template_instantiator.declaration import InstantiatedDeclaration
8 from gtwrap.template_instantiator.function import InstantiatedGlobalFunction
9 
10 
11 def instantiate_namespace(namespace):
12  """
13  Instantiate the classes and other elements in the `namespace` content and
14  assign it back to the namespace content attribute.
15 
16  @param[in/out] namespace The namespace whose content will be replaced with
17  the instantiated content.
18  """
19  instantiated_content = []
20  typedef_content = []
21 
22  for element in namespace.content:
23  if isinstance(element, parser.Class):
24  original_class = element
25  if not original_class.template:
26  instantiated_content.append(
27  InstantiatedClass(original_class, []))
28  else:
29  # This case is for when the templates have enumerated instantiations.
30 
31  # Use itertools to get all possible combinations of instantiations
32  # Works even if one template does not have an instantiation list
33  for instantiations in itertools.product(
34  *original_class.template.instantiations):
35  instantiated_content.append(
36  InstantiatedClass(original_class,
37  list(instantiations)))
38 
39  elif isinstance(element, parser.GlobalFunction):
40  original_func = element
41  if not original_func.template:
42  instantiated_content.append(
43  InstantiatedGlobalFunction(original_func, []))
44  else:
45  # Use itertools to get all possible combinations of instantiations
46  # Works even if one template does not have an instantiation list
47  for instantiations in itertools.product(
48  *original_func.template.instantiations):
49  instantiated_content.append(
50  InstantiatedGlobalFunction(original_func,
51  list(instantiations)))
52 
53  elif isinstance(element, parser.TypedefTemplateInstantiation):
54  # This is for the case where `typedef` statements are used
55  # to specify the template parameters.
56  typedef_inst = element
57  top_level = namespace.top_level()
58  original_element = top_level.find_class_or_function(
59  typedef_inst.typename)
60 
61  # Check if element is a typedef'd class, function or
62  # forward declaration from another project.
63  if isinstance(original_element, parser.Class):
64  typedef_content.append(
65  InstantiatedClass(original_element,
66  typedef_inst.typename.instantiations,
67  typedef_inst.new_name))
68  elif isinstance(original_element, parser.GlobalFunction):
69  typedef_content.append(
71  original_element, typedef_inst.typename.instantiations,
72  typedef_inst.new_name))
73  elif isinstance(original_element, parser.ForwardDeclaration):
74  typedef_content.append(
76  original_element, typedef_inst.typename.instantiations,
77  typedef_inst.new_name))
78 
79  elif isinstance(element, parser.Namespace):
80  element = instantiate_namespace(element)
81  instantiated_content.append(element)
82  else:
83  instantiated_content.append(element)
84 
85  instantiated_content.extend(typedef_content)
86  namespace.content = instantiated_content
87 
88  return namespace
gtwrap.template_instantiator.declaration.InstantiatedDeclaration
Definition: template_instantiator/declaration.py:7
list
Definition: pytypes.h:2166
gtwrap.template_instantiator.namespace.instantiate_namespace
def instantiate_namespace(namespace)
Definition: template_instantiator/namespace.py:11
gtwrap.template_instantiator.classes
Definition: template_instantiator/classes.py:1
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:842
gtwrap.template_instantiator.classes.InstantiatedClass
Definition: template_instantiator/classes.py:14
gtwrap.template_instantiator.function.InstantiatedGlobalFunction
Definition: template_instantiator/function.py:9
gtwrap.template_instantiator.function
Definition: template_instantiator/function.py:1
gtwrap.interface_parser
Definition: wrap/gtwrap/interface_parser/__init__.py:1
gtwrap.template_instantiator.declaration
Definition: template_instantiator/declaration.py:1


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