11"""Mixins for reducing the amount of boilerplate in the main wrapper class."""
22
3+ from typing import Any , Tuple , Union
4+
35import gtwrap .interface_parser as parser
46import gtwrap .template_instantiator as instantiator
57
68
79class CheckMixin :
810 """Mixin to provide various checks."""
911 # Data types that are primitive types
10- not_ptr_type = ['int' , 'double' , 'bool' , 'char' , 'unsigned char' , 'size_t' ]
12+ not_ptr_type : Tuple = ('int' , 'double' , 'bool' , 'char' , 'unsigned char' ,
13+ 'size_t' )
1114 # Ignore the namespace for these datatypes
12- ignore_namespace = [ 'Matrix' , 'Vector' , 'Point2' , 'Point3' ]
15+ ignore_namespace : Tuple = ( 'Matrix' , 'Vector' , 'Point2' , 'Point3' )
1316 # Methods that should be ignored
14- ignore_methods = [ 'pickle' ]
17+ ignore_methods : Tuple = ( 'pickle' , )
1518 # Methods that should not be wrapped directly
16- whitelist = [ 'serializable' , 'serialize' ]
19+ whitelist : Tuple = ( 'serializable' , 'serialize' )
1720 # Datatypes that do not need to be checked in methods
1821 not_check_type : list = []
1922
@@ -23,7 +26,7 @@ def _has_serialization(self, cls):
2326 return True
2427 return False
2528
26- def is_shared_ptr (self , arg_type ):
29+ def is_shared_ptr (self , arg_type : parser . Type ):
2730 """
2831 Determine if the `interface_parser.Type` should be treated as a
2932 shared pointer in the wrapper.
@@ -33,7 +36,7 @@ def is_shared_ptr(self, arg_type):
3336 and arg_type .typename .name not in self .ignore_namespace
3437 and arg_type .typename .name != 'string' )
3538
36- def is_ptr (self , arg_type ):
39+ def is_ptr (self , arg_type : parser . Type ):
3740 """
3841 Determine if the `interface_parser.Type` should be treated as a
3942 raw pointer in the wrapper.
@@ -43,7 +46,7 @@ def is_ptr(self, arg_type):
4346 and arg_type .typename .name not in self .ignore_namespace
4447 and arg_type .typename .name != 'string' )
4548
46- def is_ref (self , arg_type ):
49+ def is_ref (self , arg_type : parser . Type ):
4750 """
4851 Determine if the `interface_parser.Type` should be treated as a
4952 reference in the wrapper.
@@ -55,7 +58,14 @@ def is_ref(self, arg_type):
5558
5659class FormatMixin :
5760 """Mixin to provide formatting utilities."""
58- def _clean_class_name (self , instantiated_class ):
61+
62+ ignore_namespace : tuple
63+ data_type : Any
64+ data_type_param : Any
65+ _return_count : Any
66+
67+ def _clean_class_name (self ,
68+ instantiated_class : instantiator .InstantiatedClass ):
5969 """Reformatted the C++ class name to fit Matlab defined naming
6070 standards
6171 """
@@ -65,23 +75,23 @@ def _clean_class_name(self, instantiated_class):
6575 return instantiated_class .name
6676
6777 def _format_type_name (self ,
68- type_name ,
69- separator = '::' ,
70- include_namespace = True ,
71- constructor = False ,
72- method = False ):
78+ type_name : parser . Typename ,
79+ separator : str = '::' ,
80+ include_namespace : bool = True ,
81+ is_constructor : bool = False ,
82+ is_method : bool = False ):
7383 """
7484 Args:
7585 type_name: an interface_parser.Typename to reformat
7686 separator: the statement to add between namespaces and typename
7787 include_namespace: whether to include namespaces when reformatting
78- constructor : if the typename will be in a constructor
79- method : if the typename will be in a method
88+ is_constructor : if the typename will be in a constructor
89+ is_method : if the typename will be in a method
8090
8191 Raises:
8292 constructor and method cannot both be true
8393 """
84- if constructor and method :
94+ if is_constructor and is_method :
8595 raise ValueError (
8696 'Constructor and method parameters cannot both be True' )
8797
@@ -93,9 +103,9 @@ def _format_type_name(self,
93103 if name not in self .ignore_namespace and namespace != '' :
94104 formatted_type_name += namespace + separator
95105
96- if constructor :
106+ if is_constructor :
97107 formatted_type_name += self .data_type .get (name ) or name
98- elif method :
108+ elif is_method :
99109 formatted_type_name += self .data_type_param .get (name ) or name
100110 else :
101111 formatted_type_name += name
@@ -106,8 +116,8 @@ def _format_type_name(self,
106116 template = '{}' .format (
107117 self ._format_type_name (type_name .instantiations [idx ],
108118 include_namespace = include_namespace ,
109- constructor = constructor ,
110- method = method ))
119+ is_constructor = is_constructor ,
120+ is_method = is_method ))
111121 templates .append (template )
112122
113123 if len (templates ) > 0 : # If there are no templates
@@ -119,15 +129,15 @@ def _format_type_name(self,
119129 self ._format_type_name (type_name .instantiations [idx ],
120130 separator = separator ,
121131 include_namespace = False ,
122- constructor = constructor ,
123- method = method ))
132+ is_constructor = is_constructor ,
133+ is_method = is_method ))
124134
125135 return formatted_type_name
126136
127137 def _format_return_type (self ,
128- return_type ,
129- include_namespace = False ,
130- separator = "::" ):
138+ return_type : parser . function . ReturnType ,
139+ include_namespace : bool = False ,
140+ separator : str = "::" ):
131141 """Format return_type.
132142
133143 Args:
@@ -154,18 +164,15 @@ def _format_return_type(self,
154164
155165 return return_wrap
156166
157- def _format_class_name (self , instantiated_class , separator = '' ):
167+ def _format_class_name (self ,
168+ instantiated_class : instantiator .InstantiatedClass ,
169+ separator : str = '' ):
158170 """Format a template_instantiator.InstantiatedClass name."""
159171 if instantiated_class .parent == '' :
160172 parent_full_ns = ['' ]
161173 else :
162174 parent_full_ns = instantiated_class .parent .full_namespaces ()
163- # class_name = instantiated_class.parent.name
164- #
165- # if class_name != '':
166- # class_name += separator
167- #
168- # class_name += instantiated_class.name
175+
169176 parentname = "" .join ([separator + x
170177 for x in parent_full_ns ]) + separator
171178
@@ -175,10 +182,12 @@ def _format_class_name(self, instantiated_class, separator=''):
175182
176183 return class_name
177184
178- def _format_static_method (self , static_method , separator = '' ):
179- """Example:
180-
181- gtsamPoint3.staticFunction
185+ def _format_static_method (self ,
186+ static_method : parser .StaticMethod ,
187+ separator : str = '' ):
188+ """
189+ Example:
190+ gtsam.Point3.staticFunction()
182191 """
183192 method = ''
184193
@@ -188,35 +197,17 @@ def _format_static_method(self, static_method, separator=''):
188197
189198 return method [2 * len (separator ):]
190199
191- def _format_instance_method (self , instance_method , separator = '' ):
192- """Example:
193-
194- gtsamPoint3.staticFunction
195- """
196- method = ''
197-
198- if isinstance (instance_method , instantiator .InstantiatedMethod ):
199- method_list = [
200- separator + x
201- for x in instance_method .parent .parent .full_namespaces ()
202- ]
203- method += "" .join (method_list ) + separator
204-
205- method += instance_method .parent .name + separator
206- method += instance_method .original .name
207- method += "<" + instance_method .instantiations .to_cpp () + ">"
208-
209- return method [2 * len (separator ):]
210-
211- def _format_global_method (self , static_method , separator = '' ):
200+ def _format_global_function (self ,
201+ function : Union [parser .GlobalFunction , Any ],
202+ separator : str = '' ):
212203 """Example:
213204
214205 gtsamPoint3.staticFunction
215206 """
216207 method = ''
217208
218- if isinstance (static_method , parser .GlobalFunction ):
219- method += "" .join ([separator + x for x in static_method .parent .full_namespaces ()]) + \
209+ if isinstance (function , parser .GlobalFunction ):
210+ method += "" .join ([separator + x for x in function .parent .full_namespaces ()]) + \
220211 separator
221212
222213 return method [2 * len (separator ):]
0 commit comments