Skip to content

Commit 7168a37

Browse files
committed
Merge pull request #85 from mpsonntag/dev
Refactor write / update Property values
2 parents 981521a + cd9b310 commit 7168a37

File tree

9 files changed

+123
-17
lines changed

9 files changed

+123
-17
lines changed

+nix/Property.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,32 @@
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

3653
dispStr = 'Note: nix only supports updating the actual value at the moment.';
3754
dispStr = [dispStr, char(10), 'Attributes like uncertainty or checksum cannot be set at the moment.'];
3855
disp(dispStr);
56+
57+
%-- TODO: clearing existing values using obj.values = '' works,
58+
%-- but is a hack. Refactor in good time.
3959
end
4060
end
4161

+nix/Section.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
end;
9797

9898
function p = create_property_with_value(obj, name, val)
99+
if(~iscell(val))
100+
val = num2cell(val);
101+
end;
99102
p = nix.Property(nix_mx('Section::createPropertyWithValue', ...
100103
obj.nix_handle, name, val));
101104
obj.propsCache.lastUpdate = 0;

src/nixgen.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,23 @@ mxArray *dataset_read_all(const nix::DataSet &da) {
3939
return data;
4040
}
4141

42+
std::vector<nix::Value> extract_property_values(const extractor &input, int idx) {
43+
std::vector<nix::Value> currVec;
44+
45+
mxClassID currID = input.class_id(idx);
46+
if (currID == mxCELL_CLASS) {
47+
const mxArray *cell_element_ptr = input.cellElemPtr(idx, 0);
48+
49+
if (mxGetClassID(cell_element_ptr) == mxSTRUCT_CLASS) {
50+
currVec = input.extractFromStruct(idx);
51+
} else {
52+
currVec = input.vec(idx);
53+
}
54+
} else {
55+
mexPrintf("Unsupported data type\n");
56+
}
57+
58+
return currVec;
59+
}
60+
4261
} // namespace nixgen

src/nixgen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace nixgen {
88

99
mxArray *dataset_read_all(const nix::DataSet &da);
1010

11+
std::vector<nix::Value> extract_property_values(const extractor &input, int idx);
12+
1113
} // namespace nixgen
1214

1315
#endif

src/nixproperty.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "nixproperty.h"
2+
#include "nixgen.h"
23

34
#include "mex.h"
45

@@ -55,8 +56,8 @@ namespace nixproperty {
5556
{
5657
nix::Property prop = input.entity<nix::Property>(1);
5758
prop.deleteValues();
58-
std::vector<nix::Value> getVals = input.extractFromStruct(2);
59-
prop.values(getVals);
59+
60+
prop.values(nixgen::extract_property_values(input, 2));
6061
}
6162

6263
} // namespace nixproperty

src/nixsection.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "nixsection.h"
2+
#include "nixgen.h"
23

34
#include "mex.h"
45
#include <nix.hpp>
@@ -68,9 +69,8 @@ void create_property(const extractor &input, infusor &output)
6869
void create_property_with_value(const extractor &input, infusor &output)
6970
{
7071
nix::Section currObj = input.entity<nix::Section>(1);
71-
std::vector<nix::Value> currVec = input.vec(3);
7272

73-
nix::Property p = currObj.createProperty(input.str(2), currVec);
73+
nix::Property p = currObj.createProperty(input.str(2), nixgen::extract_property_values(input, 3));
7474
output.set(0, handle(p));
7575
}
7676

src/utils/arguments.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class extractor : public argument_helper<const mxArray> {
6565
return mx_array_to_str(array[pos]);
6666
}
6767

68+
const mxArray * cellElemPtr(size_t pos, int index) const {
69+
const mxArray *cell_element_ptr = mxGetCell(array[pos], index);
70+
return cell_element_ptr;
71+
}
72+
6873
template<typename T>
6974
T num(size_t pos) const {
7075
check_arg_type(pos, nix::to_data_type<T>::value);

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

tests/TestSection.m

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,58 @@
156156
f = nix.File(fullfile(pwd,'tests','testRW.h5'), nix.FileMode.Overwrite);
157157
s = f.createSection('mainSection', 'nixSection');
158158

159-
tmp = s.create_property_with_value('doubleProperty', {5, 6, 7, 8});
160-
assert(strcmp(s.allProperties{1}.name, 'doubleProperty'));
161-
assert(s.open_property(s.allProperties{1}.id).values{1}.value == 5);
162-
assert(size(s.open_property(s.allProperties{1}.id).values, 1) == 4);
159+
tmp = s.create_property_with_value('doubleProperty1', [5, 6, 7, 8]);
160+
assert(strcmp(s.allProperties{end}.name, 'doubleProperty1'));
161+
assert(s.open_property(s.allProperties{end}.id).values{1}.value == 5);
162+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 4);
163+
assert(strcmpi(tmp.datatype,'double'));
164+
165+
tmp = s.create_property_with_value('doubleProperty2', {5, 6, 7, 8});
166+
assert(strcmp(s.allProperties{end}.name, 'doubleProperty2'));
167+
assert(s.open_property(s.allProperties{end}.id).values{1}.value == 5);
168+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 4);
163169
assert(strcmpi(tmp.datatype,'double'));
164170

165-
tmp = s.create_property_with_value('stringProperty', {'this', 'has', 'strings'});
166-
assert(strcmp(s.allProperties{2}.name, 'stringProperty'));
167-
assert(strcmp(s.open_property(s.allProperties{2}.id).values{1}.value, 'this'));
168-
assert(size(s.open_property(s.allProperties{2}.id).values, 1) == 3);
171+
tmp = s.create_property_with_value('stringProperty1', ['a', 'string']);
172+
assert(strcmp(s.allProperties{end}.name, 'stringProperty1'));
173+
assert(strcmp(s.open_property(s.allProperties{end}.id).values{1}.value, 'a'));
174+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 7);
169175
assert(strcmpi(tmp.datatype, 'string'));
176+
177+
tmp = s.create_property_with_value('stringProperty2', {'this', 'has', 'strings'});
178+
assert(strcmp(s.allProperties{end}.name, 'stringProperty2'));
179+
assert(strcmp(s.open_property(s.allProperties{end}.id).values{1}.value, 'this'));
180+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 3);
181+
assert(strcmpi(tmp.datatype, 'string'));
182+
183+
tmp = s.create_property_with_value('booleanProperty1', [true, false, true]);
184+
assert(strcmp(s.allProperties{end}.name, 'booleanProperty1'));
185+
assert(s.open_property(s.allProperties{end}.id).values{1}.value);
186+
assert(~s.open_property(s.allProperties{end}.id).values{2}.value);
187+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 3);
188+
assert(strcmpi(tmp.datatype, 'bool'));
170189

171-
tmp = s.create_property_with_value('booleanProperty', {true, false, true});
172-
assert(strcmp(s.allProperties{3}.name, 'booleanProperty'));
173-
assert(s.open_property(s.allProperties{3}.id).values{1}.value);
174-
assert(~s.open_property(s.allProperties{3}.id).values{2}.value);
175-
assert(size(s.open_property(s.allProperties{3}.id).values, 1) == 3);
190+
tmp = s.create_property_with_value('booleanProperty2', {true, false, true});
191+
assert(strcmp(s.allProperties{end}.name, 'booleanProperty2'));
192+
assert(s.open_property(s.allProperties{end}.id).values{1}.value);
193+
assert(~s.open_property(s.allProperties{end}.id).values{2}.value);
194+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 3);
176195
assert(strcmpi(tmp.datatype, 'bool'));
196+
197+
val1 = s.open_property(s.allProperties{1}.id).values{1};
198+
val2 = s.open_property(s.allProperties{1}.id).values{2};
199+
tmp = s.create_property_with_value('doubleByStrunct1', [val1, val2]);
200+
assert(strcmp(s.allProperties{end}.name, 'doubleByStrunct1'));
201+
assert(s.open_property(s.allProperties{end}.id).values{1}.value == 5);
202+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 2);
203+
assert(strcmpi(tmp.datatype,'double'));
204+
205+
val3 = s.open_property(s.allProperties{1}.id).values{3};
206+
tmp = s.create_property_with_value('doubleByStrunct2', {val1, val2, val3});
207+
assert(strcmp(s.allProperties{end}.name, 'doubleByStrunct2'));
208+
assert(s.open_property(s.allProperties{end}.id).values{3}.value == 7);
209+
assert(size(s.open_property(s.allProperties{end}.id).values, 1) == 3);
210+
assert(strcmpi(tmp.datatype,'double'));
177211
end
178212

179213
%% Test: Delete property by entity, propertyStruct, ID and name

0 commit comments

Comments
 (0)