Merge.
2 // http://www.codeproject.com/cpp/FastDelegate.asp
4 // Modified to be run from shworker by PM
7 // HOPTER.EXE -- A simple header generator for vararg templates
9 C++ doesn't have a vararg feature for templates. This can be a
10 nuisance when writing class libraries. There are a few options:
11 (a) manually repeat code. This is tedious and error prone.
12 (b) Use macros. Messy, can't cope with template
13 arguments, and users get hard-to-interpret error messages.
14 (c) Use the boost preprocessor library. This slows compilation,
15 and introduces a dependency on boost.
16 (d) Write a program to automatically generate the code.
17 I've never seen a general program to do this job, so this is
18 a very simple command-line program to do it.
20 Why is it called Hopter?
21 I wanted a word that started with h, to be reminiscent of header
22 files. At the time, my 2yo son kept talking about 'Hopter Copters'
23 (he couldn't pronounce 'helicopter').
24 It also reflects the level of sophistication of this program --
25 it was named by a two year old.
29 When you analyze the problem, you find that the requirements
31 Varargs are needed in two places: templates and functions.
32 You want to use them in two ways: when declaring the template
33 or function, and when invoking the function / selecting the
36 It's a brain-dead implementation, which just does a search-and-replace
37 of specific strings. It could be done with a few lines using sed on
38 a *NIX platform. Because I use Windows, I've done a quick and dirty
39 implementation using CString.
41 In a file, @VARARGS at the start of a line indicates the start of the code
42 to be expanded. @ENDVAR at the start of a line marks the end of the
45 declare template -- template <@CLASSARGS> class SomeClass
46 template<class Param1, class Param2>
49 declare function -- somefunc(@FUNCARGS)
50 somefunc(Param1 p1, Param2 p2)
52 select template -- SomeClass<@SELARGS>
53 SomeClass<Param1, Param2>
55 invoke function -- somefunc(@INVOKEARGS)
58 I also provide @NUM which is the number of arguments.
59 The case where @NUM is zero is a special case: for template declarations,
60 the entire template<> bit needs to disappear. When other arguments are
61 involved, either at the beginning or end of the list, there a commas
62 which might need to be removed.
64 I've used .hxx as the extension for these files. This enables C++
65 I wanted to use an unusual file extension (.HOH) for these files, because they
66 aren't real header files. But I also like to use the C++ syntax highlighting
67 in Visual Studio. I chose .hxx because MSVC treats it as C++, but it
68 seems to very rarely used.
70 Installation (for VC6, if you want to use .HOH as the extension instead):
71 Go into the registry and change
72 HKEY_CURRENT_USER\Software\Microsoft\DevStudio\6.0\Text Editor\Tabs/Language Settings\C/C++\FileExtensions from cpp;cxx;c;h;hxx;hpp;inl;tlh;tli;rc;rc2
73 to cpp;cxx;c;h;hxx;hpp;inl;tlh;tli;rc;rc2;hoh by adding �;hoh� to the end.
74 Then add this as a custom build step to the main file of your project:
75 hopter $(InputDir)\*.hoh $(InputDir)\*.h
83 // This is a quick-n-dirty implementation of a CString replacement.
84 // It promises nothing more than to provide enough functionality to get
85 // this program to run.
86 // Note that I have really never used CString, so the information to
87 // implement the members that are used in this program come from
88 // online documentation at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cstring.asp
89 // Also, I have only used VC++ a few times, a long time ago, so
90 // I really do not know what all their alphabet soup is suppoed to
91 // mean, so I just made a guess...
97 enum { MAX_FORMAT_STRING_SIZE = 8 * 1044 };
147 char buffer[ MAX_FORMAT_STRING_SIZE ];
150 vsprintf(buffer, fmt, ap);
161 CString & operator+=(
164 str_ += x.str_; return *this;
172 std::string::size_type pos = 0;
173 while (pos < str_.length())
175 pos = str_.find(lhs.str_, pos);
176 if (pos != std::string::npos)
178 str_.replace(pos, lhs.GetLength(), rhs.str_);
179 pos += rhs.GetLength();
186 int GetLength() const
188 return str_.length();
191 operator char const * () const
211 /* Example: This is an excerpt from boost::function_template.hpp
213 #define BOOST_FUNCTION_FUNCTION_INVOKER \
214 BOOST_JOIN(function_invoker,BOOST_FUNCTION_NUM_ARGS)
215 #define BOOST_FUNCTION_VOID_FUNCTION_INVOKER \
216 BOOST_JOIN(void_function_invoker,BOOST_FUNCTION_NUM_ARGS)
219 typename FunctionPtr,
220 typename R BOOST_FUNCTION_COMMA
221 BOOST_FUNCTION_TEMPLATE_PARMS
223 struct BOOST_FUNCTION_GET_FUNCTION_INVOKER
225 typedef typename ct_if<(is_void<R>::value),
226 BOOST_FUNCTION_VOID_FUNCTION_INVOKER<
228 R BOOST_FUNCTION_COMMA
229 BOOST_FUNCTION_TEMPLATE_ARGS
231 BOOST_FUNCTION_FUNCTION_INVOKER<
233 R BOOST_FUNCTION_COMMA
234 BOOST_FUNCTION_TEMPLATE_ARGS
239 Using HOPTER, this can be written as:
241 template< typename FunctionPtr, typename R, @CLASSARGS>
242 struct get_function_invoker@NUM
244 typedef typename ct_if<(is_void<R>::value),
245 void_function_invoker@NUM<FunctionPtr, R, @SELARGS>,
246 function_invoker@NUM<FunctionPtr,R, @SELARGS>
253 /// Makes the necessary subsititutions in 'bigblock', and writes to fout.
254 void PrintVarArgs(FILE *fout, CString bigblock, int num) {
261 numstr.Format("%d", num);
270 funclist="Param1 p1";
271 selectlist = "Param1";
272 classlist = "class Param1";
275 for (int k=2; k<=num; k++) {
276 str.Format(", p%d", k);
278 str.Format(", Param%d p%d", k, k);
280 str.Format(", Param%d", k);
282 str.Format(", class Param%d", k);
287 // Simple replacement of number of arguments
288 bigblock.Replace("@NUM", numstr);
290 // Template declarations
291 if (num==0) bigblock.Replace("template<@CLASSARGS>", "");
292 else bigblock.Replace("template<@CLASSARGS>",
293 (CString)"template<" + classlist+">");
294 if (num==0) bigblock.Replace("template <@CLASSARGS>", "");
295 else bigblock.Replace("template <@CLASSARGS>",
296 (CString)"template<" + classlist+">");
297 bigblock.Replace(",@CLASSARGS", commastr + classlist);
298 bigblock.Replace(", @CLASSARGS", commastr + classlist);
299 bigblock.Replace("@CLASSARGS, ", classlist + commastr);
300 bigblock.Replace("@CLASSARGS,", classlist + commastr);
301 bigblock.Replace("@CLASSARGS", classlist);
303 // Template selections
305 if (num==0) selargstr = "";
306 else selargstr = (CString)"<"+selectlist+">";
308 bigblock.Replace("<@SELARGS>", selargstr);
309 bigblock.Replace("< @SELARGS >", selargstr);
310 bigblock.Replace(",@SELARGS", commastr + selectlist);
311 bigblock.Replace(", @SELARGS", commastr + selectlist);
312 bigblock.Replace("@SELARGS, ", selectlist + commastr);
313 bigblock.Replace("@SELARGS,", selectlist + commastr);
314 bigblock.Replace("@SELARGS", selectlist);
316 // Function declarations
317 bigblock.Replace(",@FUNCARGS", commastr + funclist);
318 bigblock.Replace(", @FUNCARGS", commastr + funclist);
319 bigblock.Replace("@FUNCARGS, ", funclist + commastr);
320 bigblock.Replace("@FUNCARGS,", funclist + commastr);
321 bigblock.Replace("@FUNCARGS", funclist);
323 // Function invocation
324 bigblock.Replace(",@INVOKEARGS", commastr + invokelist);
325 bigblock.Replace(", @INVOKEARGS", commastr + invokelist);
326 bigblock.Replace("@INVOKEARGS, ", invokelist + commastr);
327 bigblock.Replace("@INVOKEARGS,", invokelist + commastr);
328 bigblock.Replace("@INVOKEARGS", invokelist);
330 fprintf(fout, bigblock);
333 int action_hopter(int numargs, const char *filenamein, const char *filenameout)
336 MaxNumArgs = numargs;
341 fin =fopen(filenamein,"rt");
342 if (!fin) { printf("Error, can't open %s\n", filenamein); return 1; };
343 fout = fopen (filenameout, "wt");
344 if (!fout) { printf("Error, can't open %s\n", filenameout); return 1; };
347 if (!fgets(buf, 5000, fin)) break;
348 if (!strncmp(buf, "@VARARGS", 8)) {
352 if (feof(fin)) { printf("No matching @ENDVAR !!??\n"); return 1; };
353 fgets(buf, 5000, fin);
354 if (!strncmp(buf, "@ENDVAR", 7)) break;
357 // fprintf(fout, "// Auto-generated code [[[\n");
358 for (int k=0; k<=MaxNumArgs; k++) {
359 fprintf(fout, "//N=%d\n", k);
360 PrintVarArgs(fout, bigblock, k);
362 // fprintf(fout, "// ]]] End auto-generated code\n");
364 fprintf(fout, "%s", buf);