1.1 --- a/sourcehook/generate/shworker/fd_hopter.cpp Thu Oct 04 19:46:28 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,370 +0,0 @@
1.4 -// From fastdelegate
1.5 -// http://www.codeproject.com/cpp/FastDelegate.asp
1.6 -
1.7 -// Modified to be run from shworker by PM
1.8 -
1.9 -
1.10 -// HOPTER.EXE -- A simple header generator for vararg templates
1.11 -/* MOTIVATION
1.12 - C++ doesn't have a vararg feature for templates. This can be a
1.13 - nuisance when writing class libraries. There are a few options:
1.14 - (a) manually repeat code. This is tedious and error prone.
1.15 - (b) Use macros. Messy, can't cope with template
1.16 - arguments, and users get hard-to-interpret error messages.
1.17 - (c) Use the boost preprocessor library. This slows compilation,
1.18 - and introduces a dependency on boost.
1.19 - (d) Write a program to automatically generate the code.
1.20 - I've never seen a general program to do this job, so this is
1.21 - a very simple command-line program to do it.
1.22 -
1.23 - Why is it called Hopter?
1.24 - I wanted a word that started with h, to be reminiscent of header
1.25 - files. At the time, my 2yo son kept talking about 'Hopter Copters'
1.26 - (he couldn't pronounce 'helicopter').
1.27 - It also reflects the level of sophistication of this program --
1.28 - it was named by a two year old.
1.29 -
1.30 - IMPLEMENTATION
1.31 -
1.32 - When you analyze the problem, you find that the requirements
1.33 - are very simple.
1.34 - Varargs are needed in two places: templates and functions.
1.35 - You want to use them in two ways: when declaring the template
1.36 - or function, and when invoking the function / selecting the
1.37 - template.
1.38 -
1.39 - It's a brain-dead implementation, which just does a search-and-replace
1.40 - of specific strings. It could be done with a few lines using sed on
1.41 - a *NIX platform. Because I use Windows, I've done a quick and dirty
1.42 - implementation using CString.
1.43 -
1.44 -In a file, @VARARGS at the start of a line indicates the start of the code
1.45 -to be expanded. @ENDVAR at the start of a line marks the end of the
1.46 -expansion.
1.47 -
1.48 - declare template -- template <@CLASSARGS> class SomeClass
1.49 - template<class Param1, class Param2>
1.50 - class SomeClass
1.51 -
1.52 - declare function -- somefunc(@FUNCARGS)
1.53 - somefunc(Param1 p1, Param2 p2)
1.54 -
1.55 - select template -- SomeClass<@SELARGS>
1.56 - SomeClass<Param1, Param2>
1.57 -
1.58 - invoke function -- somefunc(@INVOKEARGS)
1.59 - somefunc(p1, p2)
1.60 -
1.61 - I also provide @NUM which is the number of arguments.
1.62 - The case where @NUM is zero is a special case: for template declarations,
1.63 - the entire template<> bit needs to disappear. When other arguments are
1.64 - involved, either at the beginning or end of the list, there a commas
1.65 - which might need to be removed.
1.66 -
1.67 - I've used .hxx as the extension for these files. This enables C++
1.68 - I wanted to use an unusual file extension (.HOH) for these files, because they
1.69 - aren't real header files. But I also like to use the C++ syntax highlighting
1.70 - in Visual Studio. I chose .hxx because MSVC treats it as C++, but it
1.71 - seems to very rarely used.
1.72 -
1.73 - Installation (for VC6, if you want to use .HOH as the extension instead):
1.74 - Go into the registry and change
1.75 - 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
1.76 - to cpp;cxx;c;h;hxx;hpp;inl;tlh;tli;rc;rc2;hoh by adding �;hoh� to the end.
1.77 - Then add this as a custom build step to the main file of your project:
1.78 - hopter $(InputDir)\*.hoh $(InputDir)\*.h
1.79 -
1.80 -*/
1.81 -
1.82 -#include <string>
1.83 -#include <stdarg.h>
1.84 -#include <stdio.h>
1.85 -
1.86 -// This is a quick-n-dirty implementation of a CString replacement.
1.87 -// It promises nothing more than to provide enough functionality to get
1.88 -// this program to run.
1.89 -// Note that I have really never used CString, so the information to
1.90 -// implement the members that are used in this program come from
1.91 -// online documentation at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cstring.asp
1.92 -// Also, I have only used VC++ a few times, a long time ago, so
1.93 -// I really do not know what all their alphabet soup is suppoed to
1.94 -// mean, so I just made a guess...
1.95 -
1.96 -
1.97 -class CString
1.98 -{
1.99 -public:
1.100 - enum { MAX_FORMAT_STRING_SIZE = 8 * 1044 };
1.101 - CString()
1.102 - : str_()
1.103 - {
1.104 - }
1.105 -
1.106 - CString(
1.107 - CString const & rhs)
1.108 - : str_(rhs.str_)
1.109 - {
1.110 - }
1.111 -
1.112 - CString(
1.113 - char ch,
1.114 - int repeat = 1)
1.115 - : str_(repeat, ch)
1.116 - {
1.117 - }
1.118 -
1.119 - CString(
1.120 - char const * s)
1.121 - : str_(s)
1.122 - {
1.123 - }
1.124 -
1.125 - CString & operator=(
1.126 - CString const & rhs)
1.127 - {
1.128 - str_ = rhs.str_;
1.129 - return *this;
1.130 - }
1.131 -
1.132 - CString & operator=(
1.133 - char const * s)
1.134 - {
1.135 - str_ = s;
1.136 - return *this;
1.137 - }
1.138 -
1.139 - CString & operator=(
1.140 - char ch)
1.141 - {
1.142 - str_ = ch;
1.143 - return *this;
1.144 - }
1.145 -
1.146 - void Format(
1.147 - char const * fmt,
1.148 - ...)
1.149 - {
1.150 - char buffer[ MAX_FORMAT_STRING_SIZE ];
1.151 - va_list ap;
1.152 - va_start(ap, fmt);
1.153 - vsprintf(buffer, fmt, ap);
1.154 - va_end(ap);
1.155 - str_ = buffer;
1.156 - }
1.157 -
1.158 - char operator[](
1.159 - int x) const
1.160 - {
1.161 - return str_[x];
1.162 - }
1.163 -
1.164 - CString & operator+=(
1.165 - CString const & x)
1.166 - {
1.167 - str_ += x.str_; return *this;
1.168 - }
1.169 -
1.170 - int Replace(
1.171 - CString const & lhs,
1.172 - CString const & rhs)
1.173 - {
1.174 - int rval = 0;
1.175 - std::string::size_type pos = 0;
1.176 - while (pos < str_.length())
1.177 - {
1.178 - pos = str_.find(lhs.str_, pos);
1.179 - if (pos != std::string::npos)
1.180 - {
1.181 - str_.replace(pos, lhs.GetLength(), rhs.str_);
1.182 - pos += rhs.GetLength();
1.183 - ++rval;
1.184 - }
1.185 - }
1.186 - return rval;
1.187 - }
1.188 -
1.189 - int GetLength() const
1.190 - {
1.191 - return str_.length();
1.192 - }
1.193 -
1.194 - operator char const * () const
1.195 - {
1.196 - return str_.c_str();
1.197 - }
1.198 -
1.199 -private:
1.200 - std::string str_;
1.201 -};
1.202 -
1.203 -
1.204 -CString operator+(
1.205 - CString const & x,
1.206 - CString const & y)
1.207 -{
1.208 - CString result(x);
1.209 - result += y;
1.210 - return result;
1.211 -}
1.212 -
1.213 -
1.214 -/* Example: This is an excerpt from boost::function_template.hpp
1.215 -
1.216 -#define BOOST_FUNCTION_FUNCTION_INVOKER \
1.217 - BOOST_JOIN(function_invoker,BOOST_FUNCTION_NUM_ARGS)
1.218 -#define BOOST_FUNCTION_VOID_FUNCTION_INVOKER \
1.219 - BOOST_JOIN(void_function_invoker,BOOST_FUNCTION_NUM_ARGS)
1.220 -
1.221 - template<
1.222 - typename FunctionPtr,
1.223 - typename R BOOST_FUNCTION_COMMA
1.224 - BOOST_FUNCTION_TEMPLATE_PARMS
1.225 - >
1.226 - struct BOOST_FUNCTION_GET_FUNCTION_INVOKER
1.227 - {
1.228 - typedef typename ct_if<(is_void<R>::value),
1.229 - BOOST_FUNCTION_VOID_FUNCTION_INVOKER<
1.230 - FunctionPtr,
1.231 - R BOOST_FUNCTION_COMMA
1.232 - BOOST_FUNCTION_TEMPLATE_ARGS
1.233 - >,
1.234 - BOOST_FUNCTION_FUNCTION_INVOKER<
1.235 - FunctionPtr,
1.236 - R BOOST_FUNCTION_COMMA
1.237 - BOOST_FUNCTION_TEMPLATE_ARGS
1.238 - >
1.239 - >::type type;
1.240 - };
1.241 -
1.242 -Using HOPTER, this can be written as:
1.243 -
1.244 - template< typename FunctionPtr, typename R, @CLASSARGS>
1.245 - struct get_function_invoker@NUM
1.246 - {
1.247 - typedef typename ct_if<(is_void<R>::value),
1.248 - void_function_invoker@NUM<FunctionPtr, R, @SELARGS>,
1.249 - function_invoker@NUM<FunctionPtr,R, @SELARGS>
1.250 - >::type type;
1.251 - };
1.252 -*/
1.253 -
1.254 -int MaxNumArgs;
1.255 -
1.256 -/// Makes the necessary subsititutions in 'bigblock', and writes to fout.
1.257 -void PrintVarArgs(FILE *fout, CString bigblock, int num) {
1.258 - CString numstr;
1.259 - CString invokelist;
1.260 - CString funclist;
1.261 - CString selectlist;
1.262 - CString classlist;
1.263 - CString commastr;
1.264 - numstr.Format("%d", num);
1.265 - if (num==0) {
1.266 - invokelist="";
1.267 - funclist="";
1.268 - selectlist="";
1.269 - commastr="";
1.270 - classlist="";
1.271 - }else {
1.272 - invokelist="p1";
1.273 - funclist="Param1 p1";
1.274 - selectlist = "Param1";
1.275 - classlist = "class Param1";
1.276 - commastr=", ";
1.277 - CString str;
1.278 - for (int k=2; k<=num; k++) {
1.279 - str.Format(", p%d", k);
1.280 - invokelist+=str;
1.281 - str.Format(", Param%d p%d", k, k);
1.282 - funclist+=str;
1.283 - str.Format(", Param%d", k);
1.284 - selectlist += str;
1.285 - str.Format(", class Param%d", k);
1.286 - classlist += str;
1.287 - }
1.288 - }
1.289 -
1.290 - // Simple replacement of number of arguments
1.291 - bigblock.Replace("@NUM", numstr);
1.292 -
1.293 - // Template declarations
1.294 - if (num==0) bigblock.Replace("template<@CLASSARGS>", "");
1.295 - else bigblock.Replace("template<@CLASSARGS>",
1.296 - (CString)"template<" + classlist+">");
1.297 - if (num==0) bigblock.Replace("template <@CLASSARGS>", "");
1.298 - else bigblock.Replace("template <@CLASSARGS>",
1.299 - (CString)"template<" + classlist+">");
1.300 - bigblock.Replace(",@CLASSARGS", commastr + classlist);
1.301 - bigblock.Replace(", @CLASSARGS", commastr + classlist);
1.302 - bigblock.Replace("@CLASSARGS, ", classlist + commastr);
1.303 - bigblock.Replace("@CLASSARGS,", classlist + commastr);
1.304 - bigblock.Replace("@CLASSARGS", classlist);
1.305 -
1.306 - // Template selections
1.307 - CString selargstr;
1.308 - if (num==0) selargstr = "";
1.309 - else selargstr = (CString)"<"+selectlist+">";
1.310 -
1.311 - bigblock.Replace("<@SELARGS>", selargstr);
1.312 - bigblock.Replace("< @SELARGS >", selargstr);
1.313 - bigblock.Replace(",@SELARGS", commastr + selectlist);
1.314 - bigblock.Replace(", @SELARGS", commastr + selectlist);
1.315 - bigblock.Replace("@SELARGS, ", selectlist + commastr);
1.316 - bigblock.Replace("@SELARGS,", selectlist + commastr);
1.317 - bigblock.Replace("@SELARGS", selectlist);
1.318 -
1.319 - // Function declarations
1.320 - bigblock.Replace(",@FUNCARGS", commastr + funclist);
1.321 - bigblock.Replace(", @FUNCARGS", commastr + funclist);
1.322 - bigblock.Replace("@FUNCARGS, ", funclist + commastr);
1.323 - bigblock.Replace("@FUNCARGS,", funclist + commastr);
1.324 - bigblock.Replace("@FUNCARGS", funclist);
1.325 -
1.326 - // Function invocation
1.327 - bigblock.Replace(",@INVOKEARGS", commastr + invokelist);
1.328 - bigblock.Replace(", @INVOKEARGS", commastr + invokelist);
1.329 - bigblock.Replace("@INVOKEARGS, ", invokelist + commastr);
1.330 - bigblock.Replace("@INVOKEARGS,", invokelist + commastr);
1.331 - bigblock.Replace("@INVOKEARGS", invokelist);
1.332 -
1.333 - fprintf(fout, bigblock);
1.334 -}
1.335 -
1.336 -int action_hopter(int numargs, const char *filenamein, const char *filenameout)
1.337 -{
1.338 -
1.339 - MaxNumArgs = numargs;
1.340 -
1.341 - FILE * fin;
1.342 - FILE *fout;
1.343 -
1.344 - fin =fopen(filenamein,"rt");
1.345 - if (!fin) { printf("Error, can't open %s\n", filenamein); return 1; };
1.346 - fout = fopen (filenameout, "wt");
1.347 - if (!fout) { printf("Error, can't open %s\n", filenameout); return 1; };
1.348 - char buf[5000];
1.349 - for (;;) {
1.350 - if (!fgets(buf, 5000, fin)) break;
1.351 - if (!strncmp(buf, "@VARARGS", 8)) {
1.352 - // found a match...
1.353 - CString bigblock;
1.354 - for (;;) {
1.355 - if (feof(fin)) { printf("No matching @ENDVAR !!??\n"); return 1; };
1.356 - fgets(buf, 5000, fin);
1.357 - if (!strncmp(buf, "@ENDVAR", 7)) break;
1.358 - bigblock+=buf;
1.359 - }
1.360 -// fprintf(fout, "// Auto-generated code [[[\n");
1.361 - for (int k=0; k<=MaxNumArgs; k++) {
1.362 - fprintf(fout, "//N=%d\n", k);
1.363 - PrintVarArgs(fout, bigblock, k);
1.364 - }
1.365 -// fprintf(fout, "// ]]] End auto-generated code\n");
1.366 - } else {
1.367 - fprintf(fout, "%s", buf);
1.368 - }
1.369 - }
1.370 - fclose(fin);
1.371 - fclose(fout);
1.372 - return 0;
1.373 -}