Skip to content

Commit cd9b310

Browse files
committed
add generic extract value function
1 parent bcbc25c commit cd9b310

File tree

5 files changed

+28
-43
lines changed

5 files changed

+28
-43
lines changed

+nix/Property.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
dispStr = 'Note: nix only supports updating the actual value at the moment.';
5454
dispStr = [dispStr, char(10), 'Attributes like uncertainty or checksum cannot be set at the moment.'];
5555
disp(dispStr);
56+
57+
%-- TODO: clearing existing values using obj.values = '' works,
58+
%-- but is a hack. Refactor in good time.
5659
end
5760
end
5861

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: 2 additions & 21 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

@@ -56,27 +57,7 @@ namespace nixproperty {
5657
nix::Property prop = input.entity<nix::Property>(1);
5758
prop.deleteValues();
5859

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-
79-
prop.values(getVals);
60+
prop.values(nixgen::extract_property_values(input, 2));
8061
}
8162

8263
} // namespace nixproperty

src/nixsection.cc

Lines changed: 2 additions & 22 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>
@@ -69,28 +70,7 @@ void create_property_with_value(const extractor &input, infusor &output)
6970
{
7071
nix::Section currObj = input.entity<nix::Section>(1);
7172

72-
std::vector<nix::Value> currVec;
73-
74-
mxClassID currID = input.class_id(3);
75-
if (currID == mxCELL_CLASS)
76-
{
77-
const mxArray *cell_element_ptr = input.cellElemPtr(3, 0);
78-
79-
if (mxGetClassID(cell_element_ptr) == mxSTRUCT_CLASS)
80-
{
81-
currVec = input.extractFromStruct(3);
82-
}
83-
else
84-
{
85-
currVec = input.vec(3);
86-
}
87-
}
88-
else
89-
{
90-
mexPrintf("Unsupported data type\n");
91-
}
92-
93-
nix::Property p = currObj.createProperty(input.str(2), currVec);
73+
nix::Property p = currObj.createProperty(input.str(2), nixgen::extract_property_values(input, 3));
9474
output.set(0, handle(p));
9575
}
9676

0 commit comments

Comments
 (0)