|
| 1 | +classdef DynamicPropertyManager < handle |
| 2 | +% DynamicPropertyManager - Manages dynamic properties for a target object |
| 3 | + |
| 4 | +% - This class provides a consistent interface for creating and removing |
| 5 | +% dynamic properties on a target object. |
| 6 | +% - It additionally provides an internal name registry, to keep track of |
| 7 | +% original names of properties that might not be valid MATLAB |
| 8 | +% identifiers. When adding a property with a name which is not a valid |
| 9 | +% MATLAB identifier, a valid alias is registered and used as a name for |
| 10 | +% the dynamic property. |
| 11 | +% |
| 12 | +% Used by types.untyped.Set and matnwb.mixin.HasUnnamedGroups to |
| 13 | +% allow users to access neurodata types stored in object properties of |
| 14 | +% other neurodata types through dot-syntax. |
| 15 | + |
| 16 | + properties (Access = private) |
| 17 | + TargetObject % The object to manage dynamic properties for |
| 18 | + DynamicPropertyMap % A containers.Map (name) -> (meta.DynamicProperty) |
| 19 | + NameRegistry % NameRegistry instance for name mapping |
| 20 | + end |
| 21 | + |
| 22 | + properties (SetAccess = immutable) |
| 23 | + PropertyAddedFunction % Function handle called when a property is added |
| 24 | + PropertyRemovedFunction % Function handle called when a property is removed |
| 25 | + end |
| 26 | + |
| 27 | + methods |
| 28 | + function obj = DynamicPropertyManager(targetObject, nameRegistry, propArgs) |
| 29 | + % Create a new DynamicPropertyManager for the target object |
| 30 | + arguments |
| 31 | + targetObject (1,1) dynamicprops |
| 32 | + nameRegistry (1,1) matnwb.utility.NameRegistry = matnwb.utility.NameRegistry |
| 33 | + propArgs.propertyAddedFunction function_handle = function_handle.empty |
| 34 | + propArgs.propertyRemovedFunction function_handle = function_handle.empty |
| 35 | + end |
| 36 | + |
| 37 | + obj.TargetObject = targetObject; |
| 38 | + obj.DynamicPropertyMap = containers.Map('KeyType', 'char', 'ValueType', 'any'); |
| 39 | + obj.NameRegistry = nameRegistry; |
| 40 | + obj.PropertyAddedFunction = propArgs.propertyAddedFunction; |
| 41 | + obj.PropertyRemovedFunction = propArgs.propertyRemovedFunction; |
| 42 | + end |
| 43 | + |
| 44 | + function metaProperty = addProperty(obj, originalName, options) |
| 45 | + % Add a dynamic property to the target object |
| 46 | + arguments |
| 47 | + obj matnwb.utility.DynamicPropertyManager |
| 48 | + originalName (1,1) string |
| 49 | + options.GetMethod function_handle = function_handle.empty |
| 50 | + options.SetMethod function_handle = function_handle.empty |
| 51 | + options.Dependent (1,1) logical = false |
| 52 | + end |
| 53 | + |
| 54 | + % Get or create valid name |
| 55 | + if obj.NameRegistry.existOriginalName(originalName) |
| 56 | + validName = obj.NameRegistry.getValidName(originalName); |
| 57 | + else |
| 58 | + validName = obj.NameRegistry.addMapping(originalName); |
| 59 | + end |
| 60 | + |
| 61 | + % Check if property already exists |
| 62 | + assert(~isprop(obj.TargetObject, validName), ... |
| 63 | + 'NWB:DynamicPropertyManager:PropertyExists', ... |
| 64 | + 'Property "%s" already exists on target object', validName); |
| 65 | + |
| 66 | + % Add the property |
| 67 | + metaProperty = obj.TargetObject.addprop(validName); |
| 68 | + |
| 69 | + % Set get/set methods if provided |
| 70 | + if ~isempty(options.GetMethod) |
| 71 | + metaProperty.GetMethod = options.GetMethod; |
| 72 | + end |
| 73 | + |
| 74 | + if ~isempty(options.SetMethod) |
| 75 | + metaProperty.SetMethod = options.SetMethod; |
| 76 | + end |
| 77 | + |
| 78 | + metaProperty.Dependent = options.Dependent; |
| 79 | + |
| 80 | + % Store the property metadata |
| 81 | + obj.DynamicPropertyMap(validName) = metaProperty; |
| 82 | + |
| 83 | + % Call the callback if set |
| 84 | + if ~isempty(obj.PropertyAddedFunction) |
| 85 | + obj.PropertyAddedFunction(originalName); |
| 86 | + end |
| 87 | + |
| 88 | + if ~nargout |
| 89 | + clear metaProperty |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + function removeProperty(obj, name) |
| 94 | + % Remove a dynamic property from the target object |
| 95 | + arguments |
| 96 | + obj matnwb.utility.DynamicPropertyManager |
| 97 | + name (1,1) string |
| 98 | + end |
| 99 | + |
| 100 | + % Try to remove entry assuming original name is given, fall back |
| 101 | + % to removing entry using property name. |
| 102 | + if obj.existOriginalName(name) |
| 103 | + originalName = name; |
| 104 | + propertyName = obj.getPropertyNameForOriginalName(originalName); |
| 105 | + elseif obj.existPropertyName(name) |
| 106 | + propertyName = name; |
| 107 | + originalName = obj.getOriginalNameForPropertyName(propertyName); |
| 108 | + else |
| 109 | + error('NWB:DynamicPropertyManager:UnknownProperty', ... |
| 110 | + 'No property with name "%s" exists', name); |
| 111 | + end |
| 112 | + |
| 113 | + % Check if the property exists in our map |
| 114 | + assert(obj.DynamicPropertyMap.isKey(propertyName), ... |
| 115 | + 'NWB:DynamicPropertyManager:PropertyNotManaged', ... |
| 116 | + 'Property "%s" is not managed by this DynamicPropertyManager', propertyName); |
| 117 | + |
| 118 | + % Get the property metadata |
| 119 | + propMeta = obj.DynamicPropertyMap(propertyName); |
| 120 | + |
| 121 | + % Delete the property |
| 122 | + delete(propMeta); |
| 123 | + |
| 124 | + % Remove from internal maps |
| 125 | + obj.DynamicPropertyMap.remove(propertyName); |
| 126 | + obj.NameRegistry.removeMapping(propertyName); |
| 127 | + |
| 128 | + % Call the callback if set |
| 129 | + if ~isempty(obj.PropertyRemovedFunction) |
| 130 | + obj.PropertyRemovedFunction(originalName); |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + function originalName = getOriginalNameForPropertyName(obj, propertyName) |
| 135 | + originalName = obj.NameRegistry.getOriginalName(propertyName); |
| 136 | + end |
| 137 | + |
| 138 | + function propertyName = getPropertyNameForOriginalName(obj, originalName) |
| 139 | + propertyName = obj.NameRegistry.getValidName(originalName); |
| 140 | + end |
| 141 | + |
| 142 | + function count = getPropertyCount(obj) |
| 143 | + count = obj.DynamicPropertyMap.Count; |
| 144 | + end |
| 145 | + |
| 146 | + function names = getAllPropertyNames(obj) |
| 147 | + % Get all property names |
| 148 | + names = obj.DynamicPropertyMap.keys(); |
| 149 | + end |
| 150 | + |
| 151 | + function names = getAllOriginalNames(obj) |
| 152 | + % Get all property names |
| 153 | + names = obj.NameRegistry.getAllOriginalNames(); |
| 154 | + end |
| 155 | + |
| 156 | + function tf = existPropertyName(obj, propertyName) |
| 157 | + arguments |
| 158 | + obj matnwb.utility.DynamicPropertyManager |
| 159 | + propertyName (1,1) string |
| 160 | + end |
| 161 | + tf = obj.NameRegistry.existValidName(propertyName); |
| 162 | + end |
| 163 | + |
| 164 | + function tf = existOriginalName(obj, originalName) |
| 165 | + arguments |
| 166 | + obj matnwb.utility.DynamicPropertyManager |
| 167 | + originalName (1,1) string |
| 168 | + end |
| 169 | + tf = obj.NameRegistry.existOriginalName(originalName); |
| 170 | + end |
| 171 | + |
| 172 | + function T = getPropertyMappingTable(obj) |
| 173 | + % Get a table showing property mappings |
| 174 | + T = obj.NameRegistry.getNameMappingTable(); |
| 175 | + end |
| 176 | + end |
| 177 | +end |
0 commit comments