Skip to content

Commit

Permalink
Merging 'master' into 'wrap'
Browse files Browse the repository at this point in the history
  • Loading branch information
borglab-launchpad committed Mar 25, 2021
2 parents b7e19d6 + 9519247 commit 399c30c
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 192 deletions.
22 changes: 18 additions & 4 deletions wrap/cmake/GtwrapUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ macro(get_python_version)

set(Python_VERSION_MAJOR
${PYTHON_VERSION_MAJOR}
PARENT_SCOPE)
CACHE INTERNAL "")
set(Python_VERSION_MINOR
${PYTHON_VERSION_MINOR}
PARENT_SCOPE)
CACHE INTERNAL "")
set(Python_VERSION_PATCH
${PYTHON_VERSION_PATCH}
PARENT_SCOPE)
CACHE INTERNAL "")
set(Python_EXECUTABLE
${PYTHON_EXECUTABLE}
PARENT_SCOPE)
CACHE INTERNAL "")

else()
# Get info about the Python interpreter
Expand All @@ -37,6 +37,20 @@ macro(get_python_version)
"Cannot find Python interpreter. Please install Python>=3.5.")
endif()

# Set both sets of variables
set(PYTHON_VERSION_MAJOR
${Python_VERSION_MAJOR}
CACHE INTERNAL "")
set(PYTHON_VERSION_MINOR
${Python_VERSION_MINOR}
CACHE INTERNAL "")
set(PYTHON_VERSION_PATCH
${Python_VERSION_PATCH}
CACHE INTERNAL "")
set(PYTHON_EXECUTABLE
${Python_EXECUTABLE}
CACHE INTERNAL "")

endif()
endmacro()

Expand Down
10 changes: 8 additions & 2 deletions wrap/cmake/PybindWrap.cmake
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
set(PYBIND11_PYTHON_VERSION ${WRAP_PYTHON_VERSION})

if(GTWRAP_PYTHON_PACKAGE_DIR)
# packaged
set(GTWRAP_PACKAGE_DIR "${GTWRAP_PYTHON_PACKAGE_DIR}")
else()
set(GTWRAP_PACKAGE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
endif()

# Get the Python version
include(GtwrapUtils)
message(status "Checking Python Version")
gtwrap_get_python_version(${WRAP_PYTHON_VERSION})

message(status "Setting Python version for wrapper")
set(PYBIND11_PYTHON_VERSION ${WRAP_PYTHON_VERSION})

# User-friendly Pybind11 wrapping and installing function.
# Builds a Pybind11 module from the provided interface_header.
# For example, for the interface header gtsam.h, this will
Expand Down
6 changes: 3 additions & 3 deletions wrap/gtwrap/matlab_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def _format_instance_method(self, instance_method, separator=''):

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

return method[2 * len(separator):]

Expand Down Expand Up @@ -1337,9 +1337,9 @@ def wrap_collector_function_return(self, method):
method_name = method.to_cpp()
obj_start = 'obj->'

if method.instantiation:
if method.instantiations:
# method_name += '<{}>'.format(
# self._format_type_name(method.instantiation))
# self._format_type_name(method.instantiations))
# method_name = self._format_instance_method(method, '::')
method = method.to_cpp()

Expand Down
79 changes: 41 additions & 38 deletions wrap/gtwrap/template_instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,40 +206,35 @@ class InstantiatedMethod(parser.Method):
"""
We can only instantiate template methods with a single template parameter.
"""
def __init__(self, original, instantiation=''):
def __init__(self, original, instantiations=''):
self.original = original
self.instantiation = instantiation
self.instantiations = instantiations
self.template = ''
self.is_const = original.is_const
self.parent = original.parent

if not original.template:
self.name = original.name
self.return_type = original.return_type
self.args = original.args
else:
#TODO(Varun) enable multiple templates for methods
if len(self.original.template.typenames) > 1:
raise ValueError("Can only instantiate template method with "
"single template parameter.")
self.name = instantiate_name(original.name, [self.instantiation])
self.return_type = instantiate_return_type(
original.return_type,
[self.original.template.typenames[0]],
[self.instantiation],
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)
instantiated_args = instantiate_args_list(
original.args.args_list,
[self.original.template.typenames[0]],
[self.instantiation],
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)
self.args = parser.ArgumentList(instantiated_args)
# Check for typenames if templated.
# This way, we can gracefully handle bot templated and non-templated methois.
typenames = 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,
typenames,
self.instantiations,
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)

instantiated_args = instantiate_args_list(
original.args.args_list,
typenames,
self.instantiations,
# Keyword type name `This` should already be replaced in the
# previous class template instantiation round.
cpp_typename='',
)
self.args = parser.ArgumentList(instantiated_args)

super().__init__(self.template,
self.name,
Expand All @@ -251,7 +246,13 @@ def __init__(self, original, instantiation=''):
def to_cpp(self):
"""Generate the C++ code for wrapping."""
if self.original.template:
ret = "{}<{}>".format(self.original.name, self.instantiation)
instantiation_list = [
# Get the fully qualified name
"::".join(x.namespaces + [x.name])
for x in self.instantiations
]
ret = "{}<{}>".format(self.original.name,
",".join(instantiation_list))
else:
ret = self.original.name
return ret
Expand Down Expand Up @@ -293,12 +294,13 @@ def __init__(self, original, instantiations=(), new_name=''):
# This will allow the `This` keyword to be used in both templated and non-templated classes.
typenames = self.original.template.typenames if self.original.template else []

# Instantiate the constructors, static methods, properties and instance methods, respectively.
# Instantiate the constructors, static methods, properties
# and instance methods, respectively.
self.ctors = self.instantiate_ctors(typenames)
self.static_methods = self.instantiate_static_methods(typenames)
self.properties = self.instantiate_properties(typenames)
instantiated_methods = self.instantiate_class_templates_in_methods(
typenames)
instantiated_methods = \
self.instantiate_class_templates_in_methods(typenames)

# Second instantiation round to instantiate templated methods.
# This is done in case both the class and the method are templated.
Expand All @@ -307,11 +309,12 @@ def __init__(self, original, instantiations=(), new_name=''):
if not method.template:
self.methods.append(InstantiatedMethod(method, ''))
else:
assert len(
method.template.typenames) == 1, ""\
"Can only instantiate single template methods"
for inst in method.template.instantiations[0]:
self.methods.append(InstantiatedMethod(method, inst))
instantiations = []
# Get all combinations of template parameters
for instantiations in itertools.product(
*method.template.instantiations):
self.methods.append(
InstantiatedMethod(method, instantiations))

super().__init__(
self.template,
Expand Down
31 changes: 21 additions & 10 deletions wrap/tests/expected/matlab/FunDouble.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
%at https://gtsam.org/doxygen/
%
%-------Methods-------
%dhamaalString(double d, string t) : returns Fun<double>
%multiTemplatedMethodStringSize_t(double d, string t, size_t u) : returns Fun<double>
%templatedMethodString(double d, string t) : returns Fun<double>
%
%-------Static Methods-------
%divertido() : returns Fun<double>
%staticMethodWithThis() : returns Fun<double>
%
%-------Serialization Interface-------
%string_serialize() : returns string
Expand Down Expand Up @@ -34,28 +35,38 @@ function delete(obj)
%DISPLAY Calls print on the object
function disp(obj), obj.display; end
%DISP Calls print on the object
function varargout = dhamaalString(this, varargin)
% DHAMAALSTRING usage: dhamaalString(double d, string t) : returns Fun<double>
function varargout = multiTemplatedMethodStringSize_t(this, varargin)
% MULTITEMPLATEDMETHODSTRINGSIZE_T usage: multiTemplatedMethodStringSize_t(double d, string t, size_t u) : returns Fun<double>
% Doxygen can be found at https://gtsam.org/doxygen/
if length(varargin) == 2 && isa(varargin{1},'double') && isa(varargin{2},'char')
if length(varargin) == 3 && isa(varargin{1},'double') && isa(varargin{2},'char') && isa(varargin{3},'numeric')
varargout{1} = class_wrapper(7, this, varargin{:});
return
end
error('Arguments do not match any overload of function FunDouble.dhamaalString');
error('Arguments do not match any overload of function FunDouble.multiTemplatedMethodStringSize_t');
end

function varargout = templatedMethodString(this, varargin)
% TEMPLATEDMETHODSTRING usage: templatedMethodString(double d, string t) : returns Fun<double>
% Doxygen can be found at https://gtsam.org/doxygen/
if length(varargin) == 2 && isa(varargin{1},'double') && isa(varargin{2},'char')
varargout{1} = class_wrapper(8, this, varargin{:});
return
end
error('Arguments do not match any overload of function FunDouble.templatedMethodString');
end

end

methods(Static = true)
function varargout = Divertido(varargin)
% DIVERTIDO usage: divertido() : returns Fundouble
function varargout = StaticMethodWithThis(varargin)
% STATICMETHODWITHTHIS usage: staticMethodWithThis() : returns Fundouble
% Doxygen can be found at https://gtsam.org/doxygen/
if length(varargin) == 0
varargout{1} = class_wrapper(8, varargin{:});
varargout{1} = class_wrapper(9, varargin{:});
return
end

error('Arguments do not match any overload of function FunDouble.divertido');
error('Arguments do not match any overload of function FunDouble.staticMethodWithThis');
end

end
Expand Down
4 changes: 2 additions & 2 deletions wrap/tests/expected/matlab/MultipleTemplatesIntDouble.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
function obj = MultipleTemplatesIntDouble(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(43, my_ptr);
class_wrapper(44, my_ptr);
else
error('Arguments do not match any overload of MultipleTemplatesIntDouble constructor');
end
obj.ptr_MultipleTemplatesIntDouble = my_ptr;
end

function delete(obj)
class_wrapper(44, obj.ptr_MultipleTemplatesIntDouble);
class_wrapper(45, obj.ptr_MultipleTemplatesIntDouble);
end

function display(obj), obj.print(''); end
Expand Down
4 changes: 2 additions & 2 deletions wrap/tests/expected/matlab/MultipleTemplatesIntFloat.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
function obj = MultipleTemplatesIntFloat(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(45, my_ptr);
class_wrapper(46, my_ptr);
else
error('Arguments do not match any overload of MultipleTemplatesIntFloat constructor');
end
obj.ptr_MultipleTemplatesIntFloat = my_ptr;
end

function delete(obj)
class_wrapper(46, obj.ptr_MultipleTemplatesIntFloat);
class_wrapper(47, obj.ptr_MultipleTemplatesIntFloat);
end

function display(obj), obj.print(''); end
Expand Down
6 changes: 3 additions & 3 deletions wrap/tests/expected/matlab/MyFactorPosePoint2.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
function obj = MyFactorPosePoint2(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(47, my_ptr);
class_wrapper(48, my_ptr);
elseif nargin == 4 && isa(varargin{1},'numeric') && isa(varargin{2},'numeric') && isa(varargin{3},'double') && isa(varargin{4},'gtsam.noiseModel.Base')
my_ptr = class_wrapper(48, varargin{1}, varargin{2}, varargin{3}, varargin{4});
my_ptr = class_wrapper(49, varargin{1}, varargin{2}, varargin{3}, varargin{4});
else
error('Arguments do not match any overload of MyFactorPosePoint2 constructor');
end
obj.ptr_MyFactorPosePoint2 = my_ptr;
end

function delete(obj)
class_wrapper(49, obj.ptr_MyFactorPosePoint2);
class_wrapper(50, obj.ptr_MyFactorPosePoint2);
end

function display(obj), obj.print(''); end
Expand Down
6 changes: 3 additions & 3 deletions wrap/tests/expected/matlab/MyVector12.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
function obj = MyVector12(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(40, my_ptr);
class_wrapper(41, my_ptr);
elseif nargin == 0
my_ptr = class_wrapper(41);
my_ptr = class_wrapper(42);
else
error('Arguments do not match any overload of MyVector12 constructor');
end
obj.ptr_MyVector12 = my_ptr;
end

function delete(obj)
class_wrapper(42, obj.ptr_MyVector12);
class_wrapper(43, obj.ptr_MyVector12);
end

function display(obj), obj.print(''); end
Expand Down
6 changes: 3 additions & 3 deletions wrap/tests/expected/matlab/MyVector3.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
function obj = MyVector3(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(37, my_ptr);
class_wrapper(38, my_ptr);
elseif nargin == 0
my_ptr = class_wrapper(38);
my_ptr = class_wrapper(39);
else
error('Arguments do not match any overload of MyVector3 constructor');
end
obj.ptr_MyVector3 = my_ptr;
end

function delete(obj)
class_wrapper(39, obj.ptr_MyVector3);
class_wrapper(40, obj.ptr_MyVector3);
end

function display(obj), obj.print(''); end
Expand Down
8 changes: 4 additions & 4 deletions wrap/tests/expected/matlab/PrimitiveRefDouble.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
function obj = PrimitiveRefDouble(varargin)
if nargin == 2 && isa(varargin{1}, 'uint64') && varargin{1} == uint64(5139824614673773682)
my_ptr = varargin{2};
class_wrapper(33, my_ptr);
class_wrapper(34, my_ptr);
elseif nargin == 0
my_ptr = class_wrapper(34);
my_ptr = class_wrapper(35);
else
error('Arguments do not match any overload of PrimitiveRefDouble constructor');
end
obj.ptr_PrimitiveRefDouble = my_ptr;
end

function delete(obj)
class_wrapper(35, obj.ptr_PrimitiveRefDouble);
class_wrapper(36, obj.ptr_PrimitiveRefDouble);
end

function display(obj), obj.print(''); end
Expand All @@ -43,7 +43,7 @@ function delete(obj)
% BRUTAL usage: Brutal(double t) : returns PrimitiveRefdouble
% Doxygen can be found at https://gtsam.org/doxygen/
if length(varargin) == 1 && isa(varargin{1},'double')
varargout{1} = class_wrapper(36, varargin{:});
varargout{1} = class_wrapper(37, varargin{:});
return
end

Expand Down
Loading

0 comments on commit 399c30c

Please sign in to comment.