Skip to content

Commit

Permalink
Merging 'master' into 'wrap'
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal committed Jul 22, 2021
2 parents 838e74d + 3a5e715 commit 117b401
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 88 deletions.
14 changes: 7 additions & 7 deletions wrap/gtwrap/interface_parser/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Author: Duy Nguyen Ta, Fan Jiang, Matthew Sklar, Varun Agrawal, and Frank Dellaert
"""

from typing import Iterable, List, Union
from typing import Any, Iterable, List, Union

from pyparsing import Literal, Optional, ZeroOrMore # type: ignore

Expand Down Expand Up @@ -48,12 +48,12 @@ class Hello {
args_list, t.is_const))

def __init__(self,
template: str,
template: Union[Template, Any],
name: str,
return_type: ReturnType,
args: ArgumentList,
is_const: str,
parent: Union[str, "Class"] = ''):
parent: Union["Class", Any] = ''):
self.template = template
self.name = name
self.return_type = return_type
Expand Down Expand Up @@ -98,7 +98,7 @@ def __init__(self,
name: str,
return_type: ReturnType,
args: ArgumentList,
parent: Union[str, "Class"] = ''):
parent: Union["Class", Any] = ''):
self.name = name
self.return_type = return_type
self.args = args
Expand Down Expand Up @@ -129,7 +129,7 @@ class Constructor:
def __init__(self,
name: str,
args: ArgumentList,
parent: Union["Class", str] = ''):
parent: Union["Class", Any] = ''):
self.name = name
self.args = args

Expand Down Expand Up @@ -167,7 +167,7 @@ def __init__(self,
return_type: ReturnType,
args: ArgumentList,
is_const: str,
parent: Union[str, "Class"] = ''):
parent: Union["Class", Any] = ''):
self.name = name
self.operator = operator
self.return_type = return_type
Expand Down Expand Up @@ -284,7 +284,7 @@ def __init__(
properties: List[Variable],
operators: List[Operator],
enums: List[Enum],
parent: str = '',
parent: Any = '',
):
self.template = template
self.is_virtual = is_virtual
Expand Down
2 changes: 1 addition & 1 deletion wrap/gtwrap/interface_parser/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def __init__(self,
return_type: ReturnType,
args_list: ArgumentList,
template: Template,
parent: str = ''):
parent: Any = ''):
self.name = name
self.return_type = return_type
self.args = args_list
Expand Down
6 changes: 3 additions & 3 deletions wrap/gtwrap/interface_parser/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# pylint: disable=unnecessary-lambda, expression-not-assigned

from typing import Iterable, List, Union
from typing import List, Sequence, Union

from pyparsing import (Forward, Optional, Or, ParseResults, # type: ignore
delimitedList)
Expand Down Expand Up @@ -49,12 +49,12 @@ class Typename:

def __init__(self,
t: ParseResults,
instantiations: Iterable[ParseResults] = ()):
instantiations: Sequence[ParseResults] = ()):
self.name = t[-1] # the name is the last element in this list
self.namespaces = t[:-1]

if instantiations:
if isinstance(instantiations, Iterable):
if isinstance(instantiations, Sequence):
self.instantiations = instantiations # type: ignore
else:
self.instantiations = instantiations.asList()
Expand Down
109 changes: 50 additions & 59 deletions wrap/gtwrap/matlab_wrapper/mixins.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""Mixins for reducing the amount of boilerplate in the main wrapper class."""

from typing import Any, Tuple, Union

import gtwrap.interface_parser as parser
import gtwrap.template_instantiator as instantiator


class CheckMixin:
"""Mixin to provide various checks."""
# Data types that are primitive types
not_ptr_type = ['int', 'double', 'bool', 'char', 'unsigned char', 'size_t']
not_ptr_type: Tuple = ('int', 'double', 'bool', 'char', 'unsigned char',
'size_t')
# Ignore the namespace for these datatypes
ignore_namespace = ['Matrix', 'Vector', 'Point2', 'Point3']
ignore_namespace: Tuple = ('Matrix', 'Vector', 'Point2', 'Point3')
# Methods that should be ignored
ignore_methods = ['pickle']
ignore_methods: Tuple = ('pickle', )
# Methods that should not be wrapped directly
whitelist = ['serializable', 'serialize']
whitelist: Tuple = ('serializable', 'serialize')
# Datatypes that do not need to be checked in methods
not_check_type: list = []

Expand All @@ -23,7 +26,7 @@ def _has_serialization(self, cls):
return True
return False

def is_shared_ptr(self, arg_type):
def is_shared_ptr(self, arg_type: parser.Type):
"""
Determine if the `interface_parser.Type` should be treated as a
shared pointer in the wrapper.
Expand All @@ -33,7 +36,7 @@ def is_shared_ptr(self, arg_type):
and arg_type.typename.name not in self.ignore_namespace
and arg_type.typename.name != 'string')

def is_ptr(self, arg_type):
def is_ptr(self, arg_type: parser.Type):
"""
Determine if the `interface_parser.Type` should be treated as a
raw pointer in the wrapper.
Expand All @@ -43,7 +46,7 @@ def is_ptr(self, arg_type):
and arg_type.typename.name not in self.ignore_namespace
and arg_type.typename.name != 'string')

def is_ref(self, arg_type):
def is_ref(self, arg_type: parser.Type):
"""
Determine if the `interface_parser.Type` should be treated as a
reference in the wrapper.
Expand All @@ -55,7 +58,14 @@ def is_ref(self, arg_type):

class FormatMixin:
"""Mixin to provide formatting utilities."""
def _clean_class_name(self, instantiated_class):

ignore_namespace: tuple
data_type: Any
data_type_param: Any
_return_count: Any

def _clean_class_name(self,
instantiated_class: instantiator.InstantiatedClass):
"""Reformatted the C++ class name to fit Matlab defined naming
standards
"""
Expand All @@ -65,23 +75,23 @@ def _clean_class_name(self, instantiated_class):
return instantiated_class.name

def _format_type_name(self,
type_name,
separator='::',
include_namespace=True,
constructor=False,
method=False):
type_name: parser.Typename,
separator: str = '::',
include_namespace: bool = True,
is_constructor: bool = False,
is_method: bool = False):
"""
Args:
type_name: an interface_parser.Typename to reformat
separator: the statement to add between namespaces and typename
include_namespace: whether to include namespaces when reformatting
constructor: if the typename will be in a constructor
method: if the typename will be in a method
is_constructor: if the typename will be in a constructor
is_method: if the typename will be in a method
Raises:
constructor and method cannot both be true
"""
if constructor and method:
if is_constructor and is_method:
raise ValueError(
'Constructor and method parameters cannot both be True')

Expand All @@ -93,9 +103,9 @@ def _format_type_name(self,
if name not in self.ignore_namespace and namespace != '':
formatted_type_name += namespace + separator

if constructor:
if is_constructor:
formatted_type_name += self.data_type.get(name) or name
elif method:
elif is_method:
formatted_type_name += self.data_type_param.get(name) or name
else:
formatted_type_name += name
Expand All @@ -106,8 +116,8 @@ def _format_type_name(self,
template = '{}'.format(
self._format_type_name(type_name.instantiations[idx],
include_namespace=include_namespace,
constructor=constructor,
method=method))
is_constructor=is_constructor,
is_method=is_method))
templates.append(template)

if len(templates) > 0: # If there are no templates
Expand All @@ -119,15 +129,15 @@ def _format_type_name(self,
self._format_type_name(type_name.instantiations[idx],
separator=separator,
include_namespace=False,
constructor=constructor,
method=method))
is_constructor=is_constructor,
is_method=is_method))

return formatted_type_name

def _format_return_type(self,
return_type,
include_namespace=False,
separator="::"):
return_type: parser.function.ReturnType,
include_namespace: bool = False,
separator: str = "::"):
"""Format return_type.
Args:
Expand All @@ -154,18 +164,15 @@ def _format_return_type(self,

return return_wrap

def _format_class_name(self, instantiated_class, separator=''):
def _format_class_name(self,
instantiated_class: instantiator.InstantiatedClass,
separator: str = ''):
"""Format a template_instantiator.InstantiatedClass name."""
if instantiated_class.parent == '':
parent_full_ns = ['']
else:
parent_full_ns = instantiated_class.parent.full_namespaces()
# class_name = instantiated_class.parent.name
#
# if class_name != '':
# class_name += separator
#
# class_name += instantiated_class.name

parentname = "".join([separator + x
for x in parent_full_ns]) + separator

Expand All @@ -175,10 +182,12 @@ def _format_class_name(self, instantiated_class, separator=''):

return class_name

def _format_static_method(self, static_method, separator=''):
"""Example:
gtsamPoint3.staticFunction
def _format_static_method(self,
static_method: parser.StaticMethod,
separator: str = ''):
"""
Example:
gtsam.Point3.staticFunction()
"""
method = ''

Expand All @@ -188,35 +197,17 @@ def _format_static_method(self, static_method, separator=''):

return method[2 * len(separator):]

def _format_instance_method(self, instance_method, separator=''):
"""Example:
gtsamPoint3.staticFunction
"""
method = ''

if isinstance(instance_method, instantiator.InstantiatedMethod):
method_list = [
separator + x
for x in instance_method.parent.parent.full_namespaces()
]
method += "".join(method_list) + separator

method += instance_method.parent.name + separator
method += instance_method.original.name
method += "<" + instance_method.instantiations.to_cpp() + ">"

return method[2 * len(separator):]

def _format_global_method(self, static_method, separator=''):
def _format_global_function(self,
function: Union[parser.GlobalFunction, Any],
separator: str = ''):
"""Example:
gtsamPoint3.staticFunction
"""
method = ''

if isinstance(static_method, parser.GlobalFunction):
method += "".join([separator + x for x in static_method.parent.full_namespaces()]) + \
if isinstance(function, parser.GlobalFunction):
method += "".join([separator + x for x in function.parent.full_namespaces()]) + \
separator

return method[2 * len(separator):]
7 changes: 2 additions & 5 deletions wrap/gtwrap/matlab_wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from functools import partial, reduce
from typing import Dict, Iterable, List, Union

from loguru import logger

import gtwrap.interface_parser as parser
import gtwrap.template_instantiator as instantiator
from gtwrap.matlab_wrapper.mixins import CheckMixin, FormatMixin
Expand Down Expand Up @@ -200,7 +198,7 @@ def _wrap_variable_arguments(self, args, wrap_datatypes=True):
check_type = self._format_type_name(
arg.ctype.typename,
separator='.',
constructor=not wrap_datatypes)
is_constructor=not wrap_datatypes)

var_arg_wrap += " && isa(varargin{{{num}}},'{data_type}')".format(
num=i, data_type=check_type)
Expand Down Expand Up @@ -1090,11 +1088,10 @@ def wrap_collector_function_return(self, method):
if method.instantiations:
# method_name += '<{}>'.format(
# self._format_type_name(method.instantiations))
# method_name = self._format_instance_method(method, '::')
method = method.to_cpp()

elif isinstance(method, parser.GlobalFunction):
method_name = self._format_global_method(method, '::')
method_name = self._format_global_function(method, '::')
method_name += method.name

else:
Expand Down
11 changes: 5 additions & 6 deletions wrap/gtwrap/template_instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import itertools
from copy import deepcopy
from typing import Iterable, List
from typing import Any, Iterable, List, Sequence

import gtwrap.interface_parser as parser

Expand Down Expand Up @@ -214,17 +214,17 @@ class A {
}
"""
def __init__(self,
original,
original: parser.Method,
instantiations: Iterable[parser.Typename] = ()):
self.original = original
self.instantiations = instantiations
self.template = ''
self.template: Any = ''
self.is_const = original.is_const
self.parent = original.parent

# Check for typenames if templated.
# This way, we can gracefully handle both templated and non-templated methods.
typenames = self.original.template.typenames if self.original.template else []
typenames: Sequence = self.original.template.typenames if self.original.template else []
self.name = instantiate_name(original.name, self.instantiations)
self.return_type = instantiate_return_type(
original.return_type,
Expand Down Expand Up @@ -348,13 +348,12 @@ def __repr__(self):
return "{virtual}Class {cpp_class} : {parent_class}\n"\
"{ctors}\n{static_methods}\n{methods}\n{operators}".format(
virtual="virtual " if self.is_virtual else '',
name=self.name,
cpp_class=self.to_cpp(),
parent_class=self.parent,
ctors="\n".join([repr(ctor) for ctor in self.ctors]),
methods="\n".join([repr(m) for m in self.methods]),
static_methods="\n".join([repr(m)
for m in self.static_methods]),
methods="\n".join([repr(m) for m in self.methods]),
operators="\n".join([repr(op) for op in self.operators])
)

Expand Down
Loading

0 comments on commit 117b401

Please sign in to comment.