aboutsummaryrefslogtreecommitdiff
path: root/src/include/fst/script/arg-packs.h
blob: 8ebf8d8eb614dff31db6739c461a8043f71fc90d (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright 2005-2010 Google, Inc.
// Author: jpr@google.com (Jake Ratkiewicz)

// Convenience templates for defining arg packs for the FstClass operations.

// See operation-templates.h for a discussion about why these are needed; the
// short story is that all FstClass operations must be implemented by a version
// that takes one argument, most likely a struct bundling all the
// logical arguments together. These template structs provide convenient ways
// to specify these bundles (e.g. by means of appropriate typedefs).

// The ArgPack template is sufficient for bundling together all the args for
// a particular function. The function is assumed to be void-returning. If
// you want a space for a return value, use the WithReturnValue template
// as follows:

// WithReturnValue<bool, ArgPack<...> >

#ifndef FST_SCRIPT_ARG_PACKS_H_
#define FST_SCRIPT_ARG_PACKS_H_

namespace fst {
namespace script {
namespace args {

// Sentinel value that means "no arg here."
class none_type { };

// Base arg pack template class. Specializations follow that allow
// fewer numbers of arguments (down to 2). If the maximum number of arguments
// increases, you will need to change three things:
//   1) Add more template parameters to this template
//   2) Add more specializations to allow fewer numbers of parameters than
//      the new max.
//   3) Add extra none_types to all existing specializations to fill
//      the new slots.


// 9 args (max)
template<class T1,
         class T2 = none_type,
         class T3 = none_type,
         class T4 = none_type,
         class T5 = none_type,
         class T6 = none_type,
         class T7 = none_type,
         class T8 = none_type,
         class T9 = none_type>
struct Package {
  T1 arg1;
  T2 arg2;
  T3 arg3;
  T4 arg4;
  T5 arg5;
  T6 arg6;
  T7 arg7;
  T8 arg8;
  T9 arg9;

  Package(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6,
          T7 arg7, T8 arg8, T9 arg9) :
      arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5),
      arg6(arg6), arg7(arg7), arg8(arg8), arg9(arg9) { }
};

// 8 args
template<class T1,
         class T2,
         class T3,
         class T4,
         class T5,
         class T6,
         class T7,
         class T8>
struct Package<T1, T2, T3, T4, T5, T6, T7, T8, none_type> {
  T1 arg1;
  T2 arg2;
  T3 arg3;
  T4 arg4;
  T5 arg5;
  T6 arg6;
  T7 arg7;
  T8 arg8;

  Package(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6,
          T7 arg7, T8 arg8) :
      arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5),
      arg6(arg6), arg7(arg7), arg8(arg8) { }
};

// 7 args
template<class T1,
         class T2,
         class T3,
         class T4,
         class T5,
         class T6,
         class T7>
struct Package<T1, T2, T3, T4, T5, T6, T7,
               none_type, none_type> {
  T1 arg1;
  T2 arg2;
  T3 arg3;
  T4 arg4;
  T5 arg5;
  T6 arg6;
  T7 arg7;

  Package(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6,
          T7 arg7) :
      arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5),
      arg6(arg6), arg7(arg7) { }
};

// 6 args
template<class T1,
         class T2,
         class T3,
         class T4,
         class T5,
         class T6>
struct Package<T1, T2, T3, T4, T5, T6, none_type,
               none_type, none_type> {
  T1 arg1;
  T2 arg2;
  T3 arg3;
  T4 arg4;
  T5 arg5;
  T6 arg6;

  Package(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) :
      arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5),
      arg6(arg6) { }
};

// 5 args
template<class T1,
         class T2,
         class T3,
         class T4,
         class T5>
struct Package<T1, T2, T3, T4, T5, none_type, none_type,
               none_type, none_type> {
  T1 arg1;
  T2 arg2;
  T3 arg3;
  T4 arg4;
  T5 arg5;

  Package(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) :
      arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) { }
};

// 4 args
template<class T1,
         class T2,
         class T3,
         class T4>
struct Package<T1, T2, T3, T4, none_type, none_type,
               none_type, none_type, none_type> {
  T1 arg1;
  T2 arg2;
  T3 arg3;
  T4 arg4;

  Package(T1 arg1, T2 arg2, T3 arg3, T4 arg4) :
      arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) { }
};

// 3 args
template<class T1,
         class T2,
         class T3>
struct Package<T1, T2, T3, none_type, none_type,
               none_type, none_type, none_type,
               none_type> {
  T1 arg1;
  T2 arg2;
  T3 arg3;

  Package(T1 arg1, T2 arg2, T3 arg3) :
      arg1(arg1), arg2(arg2), arg3(arg3) { }
};

// 2 args (minimum)
template<class T1,
         class T2>
struct Package<T1, T2, none_type, none_type,
               none_type, none_type, none_type,
               none_type, none_type> {
  T1 arg1;
  T2 arg2;

  Package(T1 arg1, T2 arg2) :
      arg1(arg1), arg2(arg2) { }
};

// Tack this on to an existing arg pack to add a return value.
// The syntax for accessing the args is then slightly more stilted,
// as you must do an extra member access (since the args are stored
// as a member of this class).
// The alternative is to declare another slew of templates for functions
// that return a value, analogous to the above.

template<class Retval, class ArgPackage>
struct WithReturnValue {
  Retval retval;
  const ArgPackage &args;

  explicit WithReturnValue(const ArgPackage &args) : args(args) { }
};

// We don't want to store a reference to a reference, if ArgPackage is
// already some reference type.
template<class Retval, class ArgPackage>
struct WithReturnValue<Retval, ArgPackage&> {
  Retval retval;
  const ArgPackage &args;

  explicit WithReturnValue(const ArgPackage &args) : args(args) { }
};

}  // namespace args
}  // namespace script
}  // namespace fst

#endif  // FST_SCRIPT_ARG_PACKS_H_