Skip to content

Commit bcbc25c

Browse files
committed
refactor update values
1 parent 01de620 commit bcbc25c

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

+nix/Property.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@
3030
end
3131

3232
function [] = set.values(obj, val)
33+
34+
%-- when an update occurs, check, if the datatype of the
35+
%-- data within a normal array is consistent with the
36+
%-- datatype of the property
37+
checkDType = obj.datatype;
38+
if(~iscell(val) && ~isstruct(val) && ~isempty(val))
39+
checkDType = strrep(strrep(class(val),'char','string'),'logical','bool');
40+
%-- convert any values to cell array
41+
val = num2cell(val);
42+
elseif (iscell(val) && ~isstruct(val{1}))
43+
checkDType = strrep(strrep(class(val{1}),'char','string'),'logical','bool');
44+
end;
45+
46+
if(isempty(find(strcmpi(checkDType, obj.datatype),1)))
47+
error('Values do not match property data type!');
48+
end;
49+
3350
nix_mx('Property::updateValues', obj.nix_handle, val);
3451
obj.valuesCache.lastUpdate = 0;
3552

src/nixproperty.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,27 @@ namespace nixproperty {
5555
{
5656
nix::Property prop = input.entity<nix::Property>(1);
5757
prop.deleteValues();
58-
std::vector<nix::Value> getVals = input.extractFromStruct(2);
58+
59+
std::vector<nix::Value> getVals;
60+
61+
mxClassID currID = input.class_id(2);
62+
if (currID == mxCELL_CLASS)
63+
{
64+
const mxArray *cell_element_ptr = input.cellElemPtr(2, 0);
65+
if (mxGetClassID(cell_element_ptr) == mxSTRUCT_CLASS)
66+
{
67+
getVals = input.extractFromStruct(2);
68+
}
69+
else
70+
{
71+
getVals = input.vec(2);
72+
}
73+
}
74+
else
75+
{
76+
mexPrintf("Unsupported data type\n");
77+
}
78+
5979
prop.values(getVals);
6080
}
6181

tests/TestProperty.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,26 @@
7878
assert(updateDouble.values{1}.value == 2);
7979
updateDouble.values{1}.value = 2.2;
8080
assert(updateDouble.values{1}.value == 2.2);
81+
82+
%-- test remove values from property
83+
delValues = s.open_property(s.allProperties{3}.id);
84+
assert(size(delValues.values, 1) == 4);
85+
delValues.values = '';
86+
assert(size(delValues.values, 1) == 0);
87+
clear delValues;
88+
89+
%-- test add new values to empty value property
90+
newValues = s.open_property(s.allProperties{3}.id);
91+
newValues.values = [1,2,3,4,5];
92+
assert(newValues.values{5}.value == 5);
93+
newValues.values = '';
94+
newValues.values = {6,7,8};
95+
assert(newValues.values{3}.value == 8);
96+
97+
%-- test add new values by value structure
98+
val1 = newValues.values{1};
99+
val2 = newValues.values{2};
100+
updateNewDouble = s.create_property('doubleProperty2', nix.DataType.Double);
101+
updateNewDouble.values = {val1, val2};
102+
assert(s.open_property(s.allProperties{end}.id).values{2}.value == val2.value);
81103
end

0 commit comments

Comments
 (0)