Skip to content

Commit

Permalink
Closes #64: Create a class to represent proxy ids (#65)
Browse files Browse the repository at this point in the history
1. Create `ProxyID` class.
2. Rename `ProxyID` to `Identifier`.
  • Loading branch information
sgilmore10 authored Jul 10, 2023
1 parent a01993d commit 3465900
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
21 changes: 21 additions & 0 deletions libmexclass/matlab/+libmexclass/+proxy/Identifier.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
classdef Identifier < matlab.mixin.Scalar

properties(SetAccess=private, GetAccess=public)
% C++ Proxy ID.
ID = NaN
end

methods
function obj = Identifier(id)
if nargin == 1
validateattributes(id, {'numeric'}, ...
{'scalar', 'nonnegative', 'integer'});
obj.ID = uint64(id);
end
end

function tf = ismissing(obj)
tf = ismissing(obj.ID);
end
end
end
23 changes: 15 additions & 8 deletions libmexclass/matlab/+libmexclass/+proxy/Proxy.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
classdef Proxy < matlab.mixin.indexing.RedefinesDot & handle

properties
properties(SetAccess=private, GetAccess=public)
% C++ Proxy ID.
ID uint64 = uint64.empty(0, 1);
Identifier(1, 1) libmexclass.proxy.Identifier;
% C++ Proxy Name.
Name (1,1) string;
end

properties(Dependent)
ID
end

methods

function obj = Proxy(options)
arguments
options.ID (1,1) uint64
options.Name (1,1) string
options.ID (1,1) libmexclass.proxy.Identifier {mustBeNonmissing}
options.Name (1,1) string {mustBeNonmissing}
options.ConstructorArguments (1,:) cell
end

Expand All @@ -23,14 +27,14 @@
obj.Name = options.Name;

if isfield(options, "ID")
obj.ID = options.ID;
obj.Identifier = options.ID;
else
if ~isfield(options, "ConstructorArguments")
error("libmexclass:proxy:NoConstructorArguments", """ConstructorArguments"" must be specified when constructing a new Proxy instance.");
else
% Create the an instance of the specified C++ Proxy class and return its
% Proxy ID To be stored on the MATLAB Proxy object.
obj.ID = libmexclass.proxy.gateway("Create", options.Name, options.ConstructorArguments);
obj.Identifier = libmexclass.proxy.gateway("Create", options.Name, options.ConstructorArguments);
end
end
end
Expand All @@ -40,10 +44,14 @@ function delete(obj)
% Destroy the corresponding C++ Proxy instance when destroying
% the MATLAB object. ID may be empty if an error occured during
% construction. If so, do not call destroy.
if ~isempty(obj.ID)
if ~ismissing(obj.Identifier)
libmexclass.proxy.gateway("Destroy", obj.ID);
end
end

function id = get.ID(obj)
id = obj.Identifier.ID;
end
end

methods (Access=protected)
Expand All @@ -64,5 +72,4 @@ function delete(obj)
n = listLength(obj.AddedFields,indexOp,indexContext);
end
end

end

0 comments on commit 3465900

Please sign in to comment.