diff --git a/classes/Settings.m b/classes/Settings.m index 47da9c6a4783f380eac689dfb3fdaa078d8e4ab8..b24a7e789d80223d54a4457362103f19eae14ef7 100644 --- a/classes/Settings.m +++ b/classes/Settings.m @@ -1,18 +1,18 @@ classdef Settings < Config & dynamicprops -% Settings: Provides standardized way of loading config files -% Looks for files in missions > settings for global settings -% Looks for files in caller's folder otherwise -% -% Constructor: -% - Settings: Creates an instance of the Settings class. -% Loaded config: - -% Loaded data: - -% Arguments: -% - configNames: config file names or path. -% Accepts shortened version, e.g: ode -> odeConfig.m -% -% If config name is provided, Settings checks inside the settings -% folder or inside the caller's folder + % Settings: Provides standardized way of loading config files + % Looks for files in missions > settings for global settings + % Looks for files in caller's folder otherwise + % + % Constructor: + % - Settings: Creates an instance of the Settings class. + % Loaded config: - + % Loaded data: - + % Arguments: + % - configNames: config file names or path. + % Accepts shortened version, e.g: ode -> odeConfig.m + % + % If config name is provided, Settings checks inside the settings + % folder or inside the caller's folder properties(Access = protected) configPath = '' @@ -25,31 +25,55 @@ classdef Settings < Config & dynamicprops arguments (Input, Repeating) configNames char end - + configNames = string(configNames); noConfig = ~endsWith(configNames, ["Config.m", "Config"]); noExtension = ~endsWith(configNames, ".m"); - + configNames(noConfig) = strcat(configNames(noConfig), 'Config'); configNames(noExtension) = strcat(configNames(noExtension), '.m'); varsIn = obj.getConfig(configNames); obj.loadConfig(varsIn); end + + function merge(target, source) + arguments + target + source + end + % MERGE - merges two settings classes + % Priority is given to the source settings + % empty fields are ignored + + fields = fieldnames(source)'; + for field = fields + fd = field{1}; + if isempty(source.(fd)), continue; end + if isempty(findprop(target, fd)) + target.addprop(fd); + target.(fd) = source.(fd); + elseif isa(source.(fd), 'Settings') + target.(fd).merge(source.(fd)) + else + target.(fd) = source.(fd); + end + end + end end - - methods(Access = protected) + + methods(Access = private) function loadConfig(obj, varsIn) props = fields(varsIn); - for j = 1:length(props) + for j = 1:length(props) obj.addprop(props{j}); obj.(props{j}) = varsIn.(props{j}); end end end - methods(Static) + methods(Static, Access = private) function outputVariables = getConfig(configNames) outputVariables = struct(); for i = 1:length(configNames) @@ -86,8 +110,8 @@ classdef Settings < Config & dynamicprops end end - - methods(Static, Access = protected) + + methods(Static, Access = private) function loadData(), end end end