Functions Code Generator

jgmdev
Created by: jgmdev
Date: 12/23/2011 @ 11:44:44 am
Views: 951

On the effort to improve the extension code generator readability I have started writing some code to generate the php function wrappers of wxWidgets global functions.

The idea is to create some kind of template system that can be also applied to the generation of class methods.

The file that serves as a template and the file that store the functions could be used as follows:

$function_code = "";
foreach($defFunctions as $function_name=>$function_data)
{
ob_start();
include("templates/function.php");
$function_code .= ob_get_contents();
ob_end_clean();
}

if($res =preg_match("/(.*?\/\/ functions --->)[^<]+(\/\/ <--- functions[^ยง]+)/sm",$data,$matches))
{
$data = $matches[1] . "\n" . $function_code . $matches[2];

$hd = fopen("wxwidgets.cpp","w");
fwrite($hd,$data);
fclose($hd);
}

The doxygen docs xml extractor was modified to obtain function information.

By far I have only used wxMessageBox as a test case and the generated code is working pretty well. The generated code should take care of c++ overloaded functions, still I'm not sure if it is the best (or correct) way to do it.

Generated Code

PHP_FUNCTION(php_wxMessageBox)
{
#ifdef USE_WXPHP_DEBUG
php_printf("Invoking function wxMessageBox\n");
#endif

bool already_called = false;

//Parameters for overload 0
char* message0;
long message_len0;
char* caption0;
long caption_len0;
long style0;
zval* parent0;
long x0;
long y0;
bool overload0_called = false;

//Overload 0
if(!already_called)
{
switch(ZEND_NUM_ARGS())
{
case 1:
#ifdef USE_WXPHP_DEBUG
php_printf("Parsing parameters with 's' (&message0, &message_len0)\n");
#endif
if(zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 
ZEND_NUM_ARGS() TSRMLS_CC, "s", &message0, &message_len0 ) == SUCCESS) { overload0_called = true; already_called = true; } break; case 2: #ifdef USE_WXPHP_DEBUG php_printf("Parsing parameters with 'ss' (&message0, &message_len0, &caption0, &caption_len0)\n"); #endif if(zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
 "ss", &message0, &message_len0, &caption0, &caption_len0 ) == SUCCESS) { overload0_called = true; already_called = true; } break; case 3: #ifdef USE_WXPHP_DEBUG php_printf("Parsing parameters with 'ssl' (&message0, &message_len0, &caption0, &caption_len0, &style0)\n"); #endif if(zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
 "ssl", &message0, &message_len0, &caption0, &caption_len0, &style0 ) == SUCCESS) { overload0_called = true; already_called = true; } break; case 4: #ifdef USE_WXPHP_DEBUG php_printf("Parsing parameters with 'sslz!' (&message0, &message_len0, &caption0, &caption_len0, &style0, &parent0)\n"); #endif if(zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
 "sslz!", &message0, &message_len0, &caption0, &caption_len0, &style0, &parent0 ) == SUCCESS) { overload0_called = true; already_called = true; } break; case 5: #ifdef USE_WXPHP_DEBUG php_printf("Parsing parameters with 'sslz!l' (&message0, &message_len0, &caption0, &caption_len0, &style0, &parent0, &x0)\n"); #endif if(zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
 "sslz!l", &message0, &message_len0, &caption0, &caption_len0, &style0, &parent0, &x0 ) == SUCCESS) { overload0_called = true; already_called = true; } break; case 6: #ifdef USE_WXPHP_DEBUG php_printf("Parsing parameters with 'sslz!ll' (&message0, &message_len0, &caption0, &caption_len0, &style0, &parent0, &x0, &y0)\n"); #endif if(zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
 "sslz!ll", &message0, &message_len0, &caption0, &caption_len0, &style0, &parent0, &x0, &y0 ) == SUCCESS) { overload0_called = true; already_called = true; } break; } } int rsrc_type; char _wxResource[] = "wxResource"; zval** tmp; void* object0_3 = 0; int id_to_find0_3; if(overload0_called) { switch(ZEND_NUM_ARGS()) { case 1: #ifdef USE_WXPHP_DEBUG php_printf("Executing RETURN_LONG(wxMessageBox(message0))\n"); #endif RETURN_LONG(wxMessageBox(message0)); break; case 2: #ifdef USE_WXPHP_DEBUG php_printf("Executing RETURN_LONG(wxMessageBox(message0, caption0))\n"); #endif RETURN_LONG(wxMessageBox(message0, caption0)); break; case 3: #ifdef USE_WXPHP_DEBUG php_printf("Executing RETURN_LONG(wxMessageBox(message0, caption0, style0))\n"); #endif RETURN_LONG(wxMessageBox(message0, caption0, style0)); break; case 4: if(Z_TYPE_P(parent0) == IS_OBJECT) { zend_hash_find(Z_OBJPROP_P(parent0), _wxResource , sizeof(_wxResource), (void **)&tmp); id_to_find0_3 = Z_RESVAL_P(*tmp); object0_3 = zend_list_find(id_to_find0_3, &rsrc_type); } #ifdef USE_WXPHP_DEBUG php_printf("Executing RETURN_LONG(wxMessageBox(message0, caption0, style0, (wxWindow*)object0_3))\n"); #endif RETURN_LONG(wxMessageBox(message0, caption0, style0, (wxWindow*)object0_3)); break; case 5: if(Z_TYPE_P(parent0) == IS_OBJECT) { zend_hash_find(Z_OBJPROP_P(parent0), _wxResource , sizeof(_wxResource), (void **)&tmp); id_to_find0_3 = Z_RESVAL_P(*tmp); object0_3 = zend_list_find(id_to_find0_3, &rsrc_type); } #ifdef USE_WXPHP_DEBUG php_printf("Executing RETURN_LONG(wxMessageBox(message0, caption0, style0, (wxWindow*)object0_3, x0))\n"); #endif RETURN_LONG(wxMessageBox(message0, caption0, style0, (wxWindow*)object0_3, x0)); break; case 6: if(Z_TYPE_P(parent0) == IS_OBJECT) { zend_hash_find(Z_OBJPROP_P(parent0), _wxResource , sizeof(_wxResource), (void **)&tmp); id_to_find0_3 = Z_RESVAL_P(*tmp); object0_3 = zend_list_find(id_to_find0_3, &rsrc_type); } #ifdef USE_WXPHP_DEBUG php_printf("Executing RETURN_LONG(wxMessageBox(message0, caption0, style0, (wxWindow*)object0_3, x0, y0))\n"); #endif RETURN_LONG(wxMessageBox(message0, caption0, style0, (wxWindow*)object0_3, x0, y0)); break; } } }

Comments