Skip to content

Commit f9c2e2c

Browse files
committed
Merge pull request #62 from mpsonntag/createDelete
Create and delete source/reference, refactor matlab functions
2 parents b5f4ae6 + b27b585 commit f9c2e2c

File tree

12 files changed

+280
-174
lines changed

12 files changed

+280
-174
lines changed

+nix/Block.m

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@
2828
% -----------------
2929

3030
function retObj = data_array(obj, id_or_name)
31-
handle = nix_mx('Block::openDataArray', obj.nix_handle, id_or_name);
32-
retObj = {};
33-
if handle ~= 0
34-
retObj = nix.DataArray(handle);
35-
end;
31+
retObj = nix.Utils.open_entity(obj, ...
32+
'Block::openDataArray', id_or_name, @nix.DataArray);
3633
end;
3734

3835
function da = create_data_array(obj, name, nixtype, dtype, shape)
@@ -60,21 +57,13 @@
6057
end;
6158

6259
function delCheck = delete_source(obj, del)
63-
if(strcmp(class(del),'nix.Source'))
64-
delID = del.id;
65-
else
66-
delID = del;
67-
end;
68-
delCheck = nix_mx('Block::deleteSource', obj.nix_handle, delID);
69-
obj.sourcesCache.lastUpdate = 0;
60+
[delCheck, obj.sourcesCache] = nix.Utils.delete_entity(obj, ...
61+
del, 'nix.Source', 'Block::deleteSource', obj.sourcesCache);
7062
end;
7163

7264
function retObj = open_source(obj, id_or_name)
73-
handle = nix_mx('Block::openSource', obj.nix_handle, id_or_name);
74-
retObj = {};
75-
if handle ~= 0
76-
retObj = nix.Source(handle);
77-
end;
65+
retObj = nix.Utils.open_entity(obj, ...
66+
'Block::openSource', id_or_name, @nix.Source);
7867
end;
7968

8069
% -----------------
@@ -86,11 +75,8 @@
8675
end;
8776

8877
function retObj = open_tag(obj, id_or_name)
89-
handle = nix_mx('Block::openTag', obj.nix_handle, id_or_name);
90-
retObj = {};
91-
if handle ~= 0
92-
retObj = nix.Tag(handle);
93-
end;
78+
retObj = nix.Utils.open_entity(obj, ...
79+
'Block::openTag', id_or_name, @nix.Tag);
9480
end;
9581

9682
function tag = create_tag(obj, name, type, position)
@@ -109,11 +95,8 @@
10995
end;
11096

11197
function retObj = open_multi_tag(obj, id_or_name)
112-
handle = nix_mx('Block::openMultiTag', obj.nix_handle, id_or_name);
113-
retObj = {};
114-
if handle ~= 0
115-
retObj = nix.MultiTag(handle);
116-
end;
98+
retObj = nix.Utils.open_entity(obj, ...
99+
'Block::openMultiTag', id_or_name, @nix.MultiTag);
117100
end;
118101

119102
% -----------------

+nix/File.m

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@
3333
% ----------------
3434

3535
function retObj = openBlock(obj, id_or_name)
36-
handle = nix_mx('File::openBlock', obj.nix_handle, id_or_name);
37-
retObj = {};
38-
if handle ~= 0
39-
retObj = nix.Block(handle);
40-
end;
36+
retObj = nix.Utils.open_entity(obj, ...
37+
'File::openBlock', id_or_name, @nix.Block);
4138
end
4239

4340
function blocks = get.blocks(obj)
@@ -51,25 +48,17 @@
5148
end;
5249

5350
function delCheck = deleteBlock(obj, del)
54-
if(strcmp(class(del),'nix.Block'))
55-
delID = del.id;
56-
else
57-
delID = del;
58-
end;
59-
delCheck = nix_mx('File::deleteBlock', obj.nix_handle, delID);
60-
obj.blocksCache.lastUpdate = 0;
51+
[delCheck, obj.blocksCache] = nix.Utils.delete_entity(obj, ...
52+
del, 'nix.Block', 'File::deleteBlock', obj.blocksCache);
6153
end;
6254

6355
% ----------------
6456
% Section methods
6557
% ----------------
6658

6759
function retObj = openSection(obj, id_or_name)
68-
handle = nix_mx('File::openSection', obj.nix_handle, id_or_name);
69-
retObj = {};
70-
if handle ~= 0
71-
retObj = nix.Section(handle);
72-
end;
60+
retObj = nix.Utils.open_entity(obj, ...
61+
'File::openSection', id_or_name, @nix.Section);
7362
end
7463

7564
function sections = get.sections(obj)
@@ -83,12 +72,7 @@
8372
end;
8473

8574
function delCheck = deleteSection(obj, del)
86-
if(strcmp(class(del),'nix.Section'))
87-
delID = del.id;
88-
else
89-
delID = del;
90-
end;
91-
delCheck = nix_mx('File::deleteSection', obj.nix_handle, delID);
75+
delCheck = nix.Utils.delete_entity(obj, del, 'nix.Section', 'File::deleteSection');
9276
obj.sectionsCache.lastUpdate = 0;
9377
end;
9478

+nix/MultiTag.m

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,8 @@
7676
% ------------------
7777

7878
function retObj = open_reference(obj, id_or_name)
79-
handle = nix_mx('MultiTag::openReferences', obj.nix_handle, id_or_name);
80-
retObj = {};
81-
if handle ~= 0
82-
retObj = nix.DataArray(handle);
83-
end;
79+
retObj = nix.Utils.open_entity(obj, ...
80+
'MultiTag::openReferences', id_or_name, @nix.DataArray);
8481
end;
8582

8683
function da = get.references(obj)
@@ -105,11 +102,8 @@
105102
% ------------------
106103

107104
function retObj = open_feature(obj, id_or_name)
108-
handle = nix_mx('MultiTag::openFeature', obj.nix_handle, id_or_name);
109-
retObj = {};
110-
if handle ~= 0
111-
retObj = nix.Feature(handle);
112-
end;
105+
retObj = nix.Utils.open_entity(obj, ...
106+
'MultiTag::openFeature', id_or_name, @nix.Feature);
113107
end;
114108

115109
function feat = get.features(obj)
@@ -134,11 +128,8 @@
134128
% ------------------
135129

136130
function retObj = open_source(obj, id_or_name)
137-
handle = nix_mx('MultiTag::openSource', obj.nix_handle, id_or_name);
138-
retObj = {};
139-
if handle ~= 0
140-
retObj = nix.Source(handle);
141-
end;
131+
retObj = nix.Utils.open_entity(obj, ...
132+
'MultiTag::openSource', id_or_name, @nix.Source);
142133
end;
143134

144135
function sources = get.sources(obj)

+nix/Section.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@
5151
% ----------------
5252

5353
function retObj = open_section(obj, id_or_name)
54-
handle = nix_mx('Section::openSection', obj.nix_handle, id_or_name);
55-
retObj = {};
56-
if handle ~= 0
57-
retObj = nix.Section(handle);
58-
end;
54+
retObj = nix.Utils.open_entity(obj, ...
55+
'Section::openSection', id_or_name, @nix.Section);
5956
end;
6057

6158
function hs = has_section(obj, id_or_name)

+nix/Source.m

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,13 @@
5858
end;
5959

6060
function delCheck = delete_source(obj, del)
61-
if(strcmp(class(del),'nix.Source'))
62-
delID = del.id;
63-
else
64-
delID = del;
65-
end;
66-
delCheck = nix_mx('Source::deleteSource', obj.nix_handle, delID);
67-
obj.sourcesCache.lastUpdate = 0;
61+
[delCheck, obj.sourcesCache] = nix.Utils.delete_entity(obj, ...
62+
del, 'nix.Source', 'Source::deleteSource', obj.sourcesCache);
6863
end;
6964

7065
function retObj = open_source(obj, id_or_name)
71-
handle = nix_mx('Source::openSource', obj.nix_handle, id_or_name);
72-
retObj = {};
73-
if handle ~= 0
74-
retObj = nix.Source(handle);
75-
end;
66+
retObj = nix.Utils.open_entity(obj, ...
67+
'Source::openSource', id_or_name, @nix.Source);
7668
end;
7769

7870
function sources = get.sources(obj)

+nix/Tag.m

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,19 @@
8585
% References methods
8686
% ------------------
8787

88+
function [] = add_reference(obj, add_this)
89+
obj.referencesCache = nix.Utils.add_entity(obj, ...
90+
add_this, 'nix.DataArray', 'Tag::addReference', obj.referencesCache);
91+
end;
92+
93+
function delCheck = remove_reference(obj, del)
94+
[delCheck, obj.referencesCache] = nix.Utils.delete_entity(obj, ...
95+
del, 'nix.DataArray', 'Tag::removeReference', obj.referencesCache);
96+
end;
97+
8898
function retObj = open_reference(obj, id_or_name)
89-
handle = nix_mx('Tag::openReferenceDataArray', obj.nix_handle, id_or_name);
90-
retObj = {};
91-
if handle ~= 0
92-
retObj = nix.DataArray(handle);
93-
end;
99+
retObj = nix.Utils.open_entity(obj, ...
100+
'Tag::openReferenceDataArray', id_or_name, @nix.DataArray);
94101
end;
95102

96103
function da = get.references(obj)
@@ -113,11 +120,8 @@
113120
% ------------------
114121

115122
function retObj = open_feature(obj, id_or_name)
116-
handle = nix_mx('Tag::openFeature', obj.nix_handle, id_or_name);
117-
retObj = {};
118-
if handle ~= 0
119-
retObj = nix.Feature(handle);
120-
end;
123+
retObj = nix.Utils.open_entity(obj, ...
124+
'Tag::openFeature', id_or_name, @nix.Feature);
121125
end;
122126

123127
function feat = get.features(obj)
@@ -138,13 +142,20 @@
138142
% ------------------
139143
% Sources methods
140144
% ------------------
141-
145+
146+
function [] = add_source(obj, add_this)
147+
obj.sourcesCache = nix.Utils.add_entity(obj, ...
148+
add_this, 'nix.Source', 'Tag::addSource', obj.sourcesCache);
149+
end;
150+
151+
function delCheck = remove_source(obj, del)
152+
[delCheck, obj.sourcesCache] = nix.Utils.delete_entity(obj, ...
153+
del, 'nix.Source', 'Tag::removeSource', obj.sourcesCache);
154+
end;
155+
142156
function retObj = open_source(obj, id_or_name)
143-
handle = nix_mx('Tag::openSource', obj.nix_handle, id_or_name);
144-
retObj = {};
145-
if handle ~= 0
146-
retObj = nix.Source(handle);
147-
end;
157+
retObj = nix.Utils.open_entity(obj, ...
158+
'Tag::openSource', id_or_name, @nix.Source);
148159
end;
149160

150161
function sources = get.sources(obj)

+nix/Utils.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,37 @@
3333
end;
3434
retCell = currCache.data;
3535
end;
36+
37+
function currCache = add_entity(obj, add_this, nixEntity, mxMethod, currCache)
38+
if(strcmp(class(add_this), nixEntity))
39+
addID = add_this.id;
40+
else
41+
addID = add_this;
42+
end;
43+
nix_mx(mxMethod, obj.nix_handle, addID);
44+
currCache.lastUpdate = 0;
45+
end;
46+
47+
% function can be used for both nix delete and remove methods
48+
% the first actually removes the entity, the latter
49+
% removes only the reference to the entity
50+
function [delCheck, currCache] = delete_entity(obj, del, nixEntity, mxMethod, currCache)
51+
if(strcmp(class(del), nixEntity))
52+
delID = del.id;
53+
else
54+
delID = del;
55+
end;
56+
delCheck = nix_mx(mxMethod, obj.nix_handle, delID);
57+
currCache.lastUpdate = 0;
58+
end;
59+
60+
function retObj = open_entity(obj, mxMethod, id_or_name, objConstructor)
61+
handle = nix_mx(mxMethod, obj.nix_handle, id_or_name);
62+
retObj = {};
63+
if handle ~= 0
64+
retObj = objConstructor(handle);
65+
end;
66+
end;
3667
end;
3768
end
3869

nix_mx.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,13 @@ void mexFunction(int nlhs,
142142
.reg("openReferenceDataArray", GETBYSTR(nix::DataArray, nix::Tag, getReference))
143143
.reg("openFeature", GETBYSTR(nix::Feature, nix::Tag, getFeature))
144144
.reg("openSource", GETBYSTR(nix::Source, nix::Tag, getSource))
145-
.reg("openMetadataSection", GETCONTENT(nix::Section, nix::Tag, metadata));
145+
.reg("openMetadataSection", GETCONTENT(nix::Section, nix::Tag, metadata))
146+
.reg("removeReference", REMOVER(nix::DataArray, nix::Tag, removeReference))
147+
.reg("removeSource", REMOVER(nix::Source, nix::Tag, removeSource));
146148
methods->add("Tag::retrieveData", nixtag::retrieve_data);
147149
methods->add("Tag::featureRetrieveData", nixtag::retrieve_feature_data);
150+
methods->add("Tag::addReference", nixtag::add_reference);
151+
methods->add("Tag::addSource", nixtag::add_source);
148152

149153
classdef<nix::MultiTag>("MultiTag", methods)
150154
.desc(&nixmultitag::describe)

src/nixtag.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ namespace nixtag {
2626
return sb.array();
2727
}
2828

29+
void add_reference(const extractor &input, infusor &output)
30+
{
31+
nix::Tag currObj = input.entity<nix::Tag>(1);
32+
currObj.addReference(input.str(2));
33+
}
34+
35+
void add_source(const extractor &input, infusor &output)
36+
{
37+
nix::Tag currObj = input.entity<nix::Tag>(1);
38+
currObj.addSource(input.str(2));
39+
}
40+
2941
void retrieve_data(const extractor &input, infusor &output) {
3042
nix::Tag currObj = input.entity<nix::Tag>(1);
3143
double index = input.num<double>(2);

src/nixtag.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace nixtag {
77

88
mxArray *describe(const nix::Tag &tag);
99

10+
void add_reference(const extractor &input, infusor &output);
11+
12+
void add_source(const extractor &input, infusor &output);
13+
1014
void retrieve_data(const extractor &input, infusor &output);
1115

1216
void retrieve_feature_data(const extractor &input, infusor &output);

0 commit comments

Comments
 (0)