-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_filter_factory
executable file
·144 lines (121 loc) · 3.63 KB
/
gen_filter_factory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
# This is the static front part of the file
cat >src/filter_factory.cpp <<EOF
/* Auto-generated by gen_filter_factory */
#include <vector>
#include <string>
#include <map>
#include "filter.h"
using namespace std;
struct table_entry {
std::string name;
std::vector<std::string> ¶meters;
filter* (*construct)(scene*, filter_input*);
bool (*calc)(scene*, const vector<string> &);
void (*possible_args)(scene *, vector<vector<string> > &);
};
EOF
# This awk script will parse the cpp files in the filters/ directory
# and output all the necessary function signatures for filter construction
# and standalone calculation. It also outputs the lookup table for
# everything.
awk '
BEGIN {
while (("ls src/filters/*.cpp" | getline file) > 0) {
while ((getline < file) == 1) {
if (match($0, "_make_[a-zA-Z0-9_]+_filter_" ) > 0) {
name = substr($0, RSTART + 6, RLENGTH - 14);
all_funcs[name] = 1
filters[name] = 1
}
if (match($0, "_standalone_[a-zA-Z0-9_]+" ) > 0) {
name = substr($0, RSTART + 12, RLENGTH - 12);
all_funcs[name] = 1
standalones[name] = 1
}
if (match($0, "_possible_params_[a-zA-Z0-9_]+" ) > 0) {
name = substr($0, RSTART + 17, RLENGTH - 17);
all_funcs[name] = 1
params[name] = 1
}
}
}
for (n in filters) {
printf ("filter* _make_%s_filter_(scene *scn, filter_input *input);\n", n)
}
for (n in standalones) {
printf ("bool _standalone_%s(scene *scn, const map<string, string> ¶ms);\n", n)
}
for (n in params) {
printf ("void _possible_params_%s(scene *scn, vector<map<string, string> > ¶ms);\n", n);
}
print "\nstatic table_entry filter_table[] = {"
for (n in all_funcs) {
if (filters[n] == 1) {
f = sprintf("_make_%s_filter_", n);
} else {
f = "NULL";
}
if (standalones[n] == 1) {
s = sprintf("_standalone_%s", n);
} else {
s = "NULL";
}
if (params[n] == 1) {
p = sprintf("_possible_params_%s", n);
} else {
p = "NULL";
}
printf (" { \"%s\", %s, %s, %s },\n", n, f, s, p);
}
print "};"
}
' >>src/filter_factory.cpp
# Static tail part of the file
cat >>src/filter_factory.cpp <<EOFEOF
table_entry *find_entry(const string &name) {
int tabsize = sizeof(filter_table) / sizeof(table_entry);
for(int i = 0; i < tabsize; ++i) {
if (name == filter_table[i].name) {
return filter_table + i;
}
}
return NULL;
}
filter* make_filter(const string &name, scene *scn, filter_input *input) {
table_entry *entry = find_entry(name);
if (entry == NULL || entry->construct == NULL) {
return NULL;
}
return (entry->construct)(scn, input);
}
void get_predicate_table(scene *scn, vector<pair<string, map<string, string> > > &ptable) {
int tabsize = sizeof(filter_table) / sizeof(table_entry);
for(int i = 0; i < tabsize; ++i) {
if (filter_table[i].possible_params != NULL) {
vector<map<string, string> > poss_params;
vector<map<string, string> >::iterator j;
(*filter_table[i].possible_params)(scn, poss_params);
for (j = poss_params.begin(); j != poss_params.end(); ++j) {
ptable.push_back(make_pair(filter_table[i].name, *j));
}
}
}
}
void calc_all_predicates(scene *scn, vector<bool> &results) {
int tabsize = sizeof(filter_table) / sizeof(table_entry);
results.clear();
for(int i = 0; i < tabsize; ++i) {
table_entry &entry = filter_table[i];
string &predname = entry.name;
if (entry.possible_params != NULL) {
vector<map<string, string> > param_sets;
vector<map<string, string> >::const_iterator j;
(*entry.possible_params)(scn, all_params);
for(j = param_sets.begin(); j != param_sets.end(); ++j) {
results.push_back((*entry.calc)(scn, *j));
}
}
}
}
EOFEOF