|
| 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 |
0 commit comments