Skip to content

Commit e035b14

Browse files
committed
[c++] Add filter templates
1 parent c80ed6c commit e035b14

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/filters.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2017, German Neuroinformatics Node (G-Node)
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted under the terms of the BSD License. See
7+
// LICENSE file in the root of the Project.
8+
9+
#ifndef NIX_MX_FILTERS
10+
#define NIX_MX_FILTERS
11+
12+
#include <nix.hpp>
13+
#include "mex.h"
14+
#include "datatypes.h"
15+
16+
// filter template
17+
template<typename T, typename FN>
18+
std::vector<T> filterFileEntity(const extractor &input, FN ff) {
19+
std::vector<T> res;
20+
21+
switch (input.num<uint8_t>(2)) {
22+
case switchFilter::AcceptAll:
23+
res = ff(nix::util::AcceptAll<T>());
24+
break;
25+
case switchFilter::Id:
26+
res = ff(nix::util::IdFilter<T>(input.str(3)));
27+
break;
28+
case switchFilter::Ids:
29+
// this will crash matlab, if its not a vector of strings...
30+
res = ff(nix::util::IdsFilter<T>(input.vec<std::string>(3)));
31+
break;
32+
case switchFilter::Type:
33+
res = ff(nix::util::TypeFilter<T>(input.str(3)));
34+
break;
35+
case switchFilter::Name:
36+
res = ff(nix::util::NameFilter<T>(input.str(3)));
37+
break;
38+
default: throw std::invalid_argument("unknown or unsupported filter");
39+
}
40+
41+
return res;
42+
}
43+
44+
template<typename T, typename FN>
45+
std::vector<T> filterEntity(const extractor &input, FN ff) {
46+
std::vector<T> res;
47+
48+
switch (input.num<uint8_t>(2)) {
49+
case switchFilter::AcceptAll:
50+
res = ff(nix::util::AcceptAll<T>());
51+
break;
52+
case switchFilter::Id:
53+
res = ff(nix::util::IdFilter<T>(input.str(3)));
54+
break;
55+
case switchFilter::Ids:
56+
// this will crash matlab, if its not a vector of strings...
57+
res = ff(nix::util::IdsFilter<T>(input.vec<std::string>(3)));
58+
break;
59+
case switchFilter::Type:
60+
res = ff(nix::util::TypeFilter<T>(input.str(3)));
61+
break;
62+
case switchFilter::Name:
63+
res = ff(nix::util::NameFilter<T>(input.str(3)));
64+
break;
65+
case switchFilter::Metadata:
66+
res = ff(nix::util::MetadataFilter<T>(input.str(3)));
67+
break;
68+
case switchFilter::Source:
69+
res = ff(nix::util::SourceFilter<T>(input.str(3)));
70+
break;
71+
default: throw std::invalid_argument("unknown or unsupported filter");
72+
}
73+
74+
return res;
75+
}
76+
77+
#endif

0 commit comments

Comments
 (0)