Skip to content

Commit 92ca863

Browse files
committed
write property values to nix
1 parent e22d44b commit 92ca863

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

+nix/Section.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
obj.propsCache.lastUpdate = 0;
9696
end;
9797

98+
function p = create_property_with_value(obj, name, val)
99+
p = nix.Property(nix_mx('Section::createPropertyWithValue', ...
100+
obj.nix_handle, name, val));
101+
obj.propsCache.lastUpdate = 0;
102+
end;
103+
98104
function delCheck = delete_property(obj, del)
99105
if(isstruct(del) && isfield(del, 'id'))
100106
delID = del.id;

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ void mexFunction(int nlhs,
235235
.reg("deleteProperty", REMOVER(nix::Property, nix::Section, deleteProperty));
236236
methods->add("Section::properties", nixsection::properties);
237237
methods->add("Section::createProperty", nixsection::create_property);
238+
methods->add("Section::createPropertyWithValue", nixsection::create_property_with_value);
238239

239240
classdef<nix::Feature>("Feature", methods)
240241
.desc(&nixfeature::describe)

src/nixsection.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,13 @@ void create_property(const extractor &input, infusor &output)
6969
output.set(0, handle(p));
7070
}
7171

72+
void create_property_with_value(const extractor &input, infusor &output)
73+
{
74+
nix::Section currObj = input.entity<nix::Section>(1);
75+
std::vector<nix::Value> currVec = input.extractFromCells(3);
76+
77+
nix::Property p = currObj.createProperty(input.str(2), currVec);
78+
output.set(0, handle(p));
79+
}
80+
7281
} // namespace nixsection

src/nixsection.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace nixsection {
1111

1212
void create_property(const extractor &input, infusor &output);
1313

14+
void create_property_with_value(const extractor &input, infusor &output);
15+
1416
} // namespace nixfile
1517

16-
#endif
18+
#endif

src/utils/arguments.h

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,74 @@ class extractor : public argument_helper<const mxArray> {
193193
return mxGetPr(array[pos]);
194194
}
195195

196+
std::vector<nix::Value> extractFromCells(size_t pos) const {
197+
198+
mwSize total_num_of_cells;
199+
mwIndex index;
200+
const mxArray *cell_element_ptr;
201+
202+
std::vector<nix::Value> vals;
203+
204+
total_num_of_cells = mxGetNumberOfElements(array[pos]);
205+
for (index = 0; index<total_num_of_cells; index++) {
206+
cell_element_ptr = mxGetCell(array[pos], index);
207+
208+
nix::Value currVal;
209+
210+
switch (mxGetClassID(cell_element_ptr)) {
211+
case mxLOGICAL_CLASS:
212+
{
213+
const mxLogical *curr = mxGetLogicals(cell_element_ptr);
214+
currVal.set(curr[0]); break; }
215+
case mxDOUBLE_CLASS:
216+
{
217+
double curr;
218+
const void *data = mxGetData(cell_element_ptr);
219+
memcpy(&curr, data, sizeof(double));
220+
currVal.set(curr); break; }
221+
case mxUINT32_CLASS:
222+
{
223+
uint32_t curr;
224+
const void *data = mxGetData(cell_element_ptr);
225+
memcpy(&curr, data, sizeof(uint32_t));
226+
currVal.set(curr); break; }
227+
case mxINT32_CLASS:
228+
{
229+
int32_t curr;
230+
const void *data = mxGetData(cell_element_ptr);
231+
memcpy(&curr, data, sizeof(int32_t));
232+
currVal.set(curr); break; }
233+
case mxUINT64_CLASS:
234+
{
235+
uint64_t curr;
236+
const void *data = mxGetData(cell_element_ptr);
237+
memcpy(&curr, data, sizeof(uint64_t));
238+
currVal.set(curr); break; }
239+
case mxINT64_CLASS:
240+
{
241+
int64_t curr;
242+
const void *data = mxGetData(cell_element_ptr);
243+
memcpy(&curr, data, sizeof(int64_t));
244+
currVal.set(curr); break; }
245+
246+
case mxCHAR_CLASS:
247+
{
248+
char *tmp = mxArrayToString(cell_element_ptr);
249+
std::string curr_string(tmp);
250+
currVal.set(curr_string);
251+
mxFree(tmp);
252+
break;
253+
}
254+
case mxUNKNOWN_CLASS:
255+
{ mexWarnMsgTxt("Unknown class."); break; }
256+
default:
257+
{ mexWarnMsgTxt("Unsupported class."); break; }
258+
}
259+
vals.push_back(currVal);
260+
}
261+
return vals;
262+
}
263+
196264
private:
197265
};
198266

@@ -214,4 +282,4 @@ class infusor : public argument_helper<mxArray> {
214282
private:
215283
};
216284

217-
#endif
285+
#endif

tests/TestSection.m

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
funcs{end+1} = @test_has_section;
1212
funcs{end+1} = @test_attrs;
1313
funcs{end+1} = @test_properties;
14-
funcs{end+1} = @test_create_property_data_type;
14+
funcs{end+1} = @test_create_property;
15+
funcs{end+1} = @test_create_property_with_value;
1516
funcs{end+1} = @test_delete_property;
1617
funcs{end+1} = @test_open_property;
1718
funcs{end+1} = @test_link;
@@ -139,7 +140,7 @@
139140
end
140141

141142
%% Test: Create property by data type
142-
function [] = test_create_property_data_type( varargin )
143+
function [] = test_create_property( varargin )
143144
f = nix.File(fullfile(pwd,'tests','testRW.h5'), nix.FileMode.Overwrite);
144145
s = f.createSection('mainSection', 'nixSection');
145146

@@ -153,6 +154,31 @@
153154
assert(strcmp(s.allProperties{1}.name, 'newProperty1'));
154155
end
155156

157+
%% Test: Create property with value
158+
function [] = test_create_property_with_value( varargin )
159+
f = nix.File(fullfile(pwd,'tests','testRW.h5'), nix.FileMode.Overwrite);
160+
s = f.createSection('mainSection', 'nixSection');
161+
162+
tmp = s.create_property_with_value('doubleProperty', {5, 6, 7, 8});
163+
assert(strcmp(s.allProperties{1}.name, 'doubleProperty'));
164+
assert(s.allProperties{1}.values{1} == 5);
165+
assert(size(s.allProperties{1}.values, 2) == 4);
166+
assert(strcmpi(tmp.datatype,'double'));
167+
168+
tmp = s.create_property_with_value('stringProperty', {'this', 'has', 'strings'});
169+
assert(strcmp(s.allProperties{2}.name, 'stringProperty'));
170+
assert(strcmp(s.allProperties{2}.values{1}, 'this'));
171+
assert(size(s.allProperties{2}.values, 2) == 3);
172+
assert(strcmpi(tmp.datatype, 'string'));
173+
174+
tmp = s.create_property_with_value('booleanProperty', {true, false, true});
175+
assert(strcmp(s.allProperties{3}.name, 'booleanProperty'));
176+
assert(s.allProperties{3}.values{1});
177+
assert(~s.allProperties{3}.values{2});
178+
assert(size(s.allProperties{3}.values, 2) == 3);
179+
assert(strcmpi(tmp.datatype, 'bool'));
180+
end
181+
156182
%% Test: Delete property by entity, propertyStruct, ID and name
157183
function [] = test_delete_property( varargin )
158184
f = nix.File(fullfile(pwd,'tests','testRW.h5'), nix.FileMode.Overwrite);

0 commit comments

Comments
 (0)