Skip to content

Commit f1a26bd

Browse files
committed
Backend class
1 parent 00c1b4b commit f1a26bd

35 files changed

+265
-194
lines changed

+stdlib/+dotnet/get_hostname.m renamed to +stdlib/+dotnet/hostname.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%% DOTNET.GET_HOSTNAME get the computer network hostname
22
% not necessary FQDN
33

4-
function n = get_hostname()
4+
function n = hostname()
55

66
n = char(System.Environment.MachineName);
77
% https://learn.microsoft.com/en-us/dotnet/api/system.environment.machinename

+stdlib/+java/get_hostname.m renamed to +stdlib/+java/hostname.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function n = get_hostname()
1+
function n = hostname()
22

33
n = char(java.net.InetAddress.getLocalHost().getHostName());
44

+stdlib/+python/get_hostname.m renamed to +stdlib/+python/hostname.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function n = get_hostname()
1+
function n = hostname()
22

33
try
44
n = char(py.socket.gethostname());

+stdlib/+sys/device.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
c0 = 'pwsh -c "(Get-CimInstance -ClassName Win32_Volume -Filter \"DriveLetter = ''';
1616
c1 = stdlib.root_name(stdlib.absolute(p));
1717
c2 = '''\").SerialNumber"';
18-
cmd = [c0, c1, c2];
18+
cmd = strcat(c0, c1, c2);
1919
elseif ismac()
2020
cmd = sprintf('stat -f %%d "%s"', p);
2121
else

+stdlib/+sys/get_hostname.m renamed to +stdlib/+sys/hostname.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [n, cmd] = get_hostname()
1+
function [n, cmd] = hostname()
22

33
cmd = 'hostname';
44
[s, n] = system(cmd);

+stdlib/Backend.m

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
classdef Backend < matlab.mixin.SetGet
2+
3+
properties (Constant)
4+
optionalBackends = ["python", "dotnet", "java"]
5+
namespace = "stdlib"
6+
end
7+
8+
properties
9+
backends string = ["native", "legacy", "sys"]
10+
func
11+
backend
12+
end
13+
14+
methods
15+
16+
function self = Backend(functionName, backendReq)
17+
arguments
18+
functionName (1,1) string = ""
19+
backendReq (1,:) string = string.empty
20+
end
21+
22+
for i = 1:numel(self.optionalBackends)
23+
f = str2func(sprintf('%s.has_%s', self.namespace, self.optionalBackends(i)));
24+
if f()
25+
self.backends = [self.optionalBackends(i), self.backends];
26+
end
27+
end
28+
29+
if any(strlength(functionName))
30+
self.func = self.getFunc(functionName, backendReq);
31+
end
32+
end
33+
34+
35+
function m = select(self, functionName, backendReq)
36+
arguments
37+
self
38+
functionName (1,1) string
39+
backendReq (1,:) string = string.empty
40+
end
41+
42+
if ~any(strlength(backendReq))
43+
backendReq = self.backends;
44+
end
45+
46+
for m = backendReq
47+
switch m
48+
case 'dotnet'
49+
if ~any(ismember(self.backends, 'dotnet'))
50+
continue
51+
end
52+
53+
switch functionName
54+
case {'create_symlink', 'ram_total', 'read_symlink'}
55+
if stdlib.dotnet_api() < 6, continue, end
56+
case {'get_owner', 'is_admin'}
57+
if isunix(), continue, end
58+
end
59+
case 'java'
60+
if ~any(ismember(self.backends, 'java'))
61+
continue
62+
end
63+
64+
switch functionName
65+
case {'device', 'hard_link_count' , 'inode', 'is_admin'}
66+
if ispc(), continue, end
67+
end
68+
case 'python'
69+
if ~any(ismember(self.backends, 'python'))
70+
continue
71+
end
72+
73+
switch functionName
74+
case {'filesystem_type', 'ram_total', 'ram_free'}
75+
if ~stdlib.python.has_psutil(); continue, end
76+
case {'cpu_load', 'get_owner'}
77+
if ispc(), continue, end
78+
case 'is_admin'
79+
if ispc() || stdlib.matlabOlderThan('R2024a'), continue, end
80+
end
81+
case 'native'
82+
83+
switch functionName
84+
case 'create_symlink'
85+
if stdlib.matlabOlderThan('R2024b') || (ispc() && stdlib.matlabOlderThan('R2025a'))
86+
continue
87+
end
88+
case {'is_symlink', 'read_symlink'}
89+
if stdlib.matlabOlderThan('R2024b'), continue, end
90+
case {'get_permissions', 'set_permissions'}
91+
if stdlib.matlabOlderThan('R2025a'), continue, end
92+
end
93+
end
94+
95+
if ~isempty(which(sprintf('%s.%s.%s', self.namespace, m, functionName)))
96+
return
97+
end
98+
99+
end
100+
101+
m = string.empty;
102+
103+
end
104+
105+
106+
function func = getFunc(self, functionName, backendReq)
107+
arguments
108+
self
109+
functionName (1,1) string
110+
backendReq (1,:) string = string.empty
111+
end
112+
113+
if isscalar(backendReq)
114+
self.backend = backendReq;
115+
else
116+
self.backend = self.select(functionName, backendReq);
117+
end
118+
119+
if isempty(self.backend)
120+
error('No backend found for %s.%s', self.namespace, functionName);
121+
else
122+
func = str2func(sprintf('%s.%s.%s', self.namespace, self.backend, functionName));
123+
end
124+
125+
end
126+
127+
end
128+
end

+stdlib/cpu_arch.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
%% CPU_ARCH get the CPU architecture
2+
%
3+
%%% Outputs
4+
% * a: Returns the CPU architecture as a string.
5+
% * b: backend used
26

3-
function a = cpu_arch(backend)
7+
function [a, b] = cpu_arch(backend)
48
arguments
59
backend (1,:) string = ["java", "dotnet", "native"]
610
end
711

8-
fun = hbackend(backend, "cpu_arch");
12+
o = stdlib.Backend(mfilename(), backend);
13+
a = o.func();
914

10-
a = fun();
15+
b = o.backend;
1116

1217
end

+stdlib/cpu_load.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
%% CPU_LOAD get total physical CPU load
22
%
3-
% Returns the "recent cpu usage" for the whole system.
3+
%%% Outputs
4+
% * a: Returns the "recent cpu usage" for the whole system.
5+
% * b: backend used
46
%
57
% This value is a double greater than 0.
68
% If the system recent cpu usage is not available, the backend returns a negative or NaN value.
79

8-
function L = cpu_load(backend)
10+
function [a, b] = cpu_load(backend)
911
arguments
1012
backend (1,:) string = ["java", "python", "sys"]
1113
end
1214

13-
fun = hbackend(backend, "cpu_load");
14-
15-
L = fun();
15+
o = stdlib.Backend(mfilename(), backend);
16+
a = o.func();
1617

18+
b = o.backend;
1719

1820
end

+stdlib/create_symlink.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
backend (1,:) string = ["native", "dotnet", "python", "sys"]
1515
end
1616

17-
[fun, b] = hbackend(backend, "create_symlink");
18-
19-
ok = fun(target, link);
17+
o = stdlib.Backend(mfilename(), backend);
18+
ok = o.func(target, link);
2019

20+
b = o.backend;
2121
end

+stdlib/device.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
backend (1,:) string = ["java", "python", "sys"]
1313
end
1414

15-
[fun, b] = hbackend(backend, "device");
16-
17-
i = fun(file);
15+
o = stdlib.Backend(mfilename(), backend);
16+
i = o.func(file);
1817

18+
b = o.backend;
1919
end

0 commit comments

Comments
 (0)