[glibmm] gmmproc: _WRAP_METHOD: Support wrapping methods with slots.



commit b05333e9119a4548da0db0705b930a37ae7d7fce
Author: Josà Alburquerque <jaalburqu svn gnome org>
Date:   Mon Nov 19 11:22:56 2012 -0500

    gmmproc: _WRAP_METHOD: Support wrapping methods with slots.
    
    	* tools/pm/WrapParser.pm (on_wrap_method): Add code to parse the
    	additional 'slot_name', 'slot_callback' and 'no_slot_copy' options.
    	- The 'slot_name' options specifies the name of the slot parameter in
    	  the C++ method declaration.
    	- The 'slot_callback' option specifies the name of the callback to
    	  pass to the C function.
    	- The 'no_slot_copy' option specifies that the actual slot should be
    	  passed to the C function in the data parameter and not a copy.  By
    	  default, a copy is used.
    	* tools/pm/Output.pm (output_wrap_meth): Pass the new slot options
    	along to the *METHOD() m4 macros.
    	(convert_args_cpp_to_c):
    	- If there is a slot parameter, ignore the final user_data parameter
    	  in the C function when comparing the argument count in the C++
    	  method and the C function.
    	- Convert a possible slot parameter to the address of the specified
    	  slot callback (with the 'slot_callback' option).  Report an error if
    	  no callback has been specified.
    	- Pass a 'slot_copy' variable as the final user data parameter to the
    	  C function.  The variable is declared by the _*METHOD() m4 macros.
    	* tools/m4/method.m4 (_METHOD):
    	(_STATIC_METHOD):
    	- Accept the new 'slot_type', 'slot_name' and 'no_slot_copy' options
    	  which specify the C++ slot type (without the const and the &), the
    	  C++ slot parameter name and whether to create a copy of the slot or
    	  not respectively.
    	- Insert code to declare a 'slot_copy' variable that is either a copy
    	  of the slot or a pointer to the actual C++ slot based on the
    	  'no_slot_copy' option.
    
    	Bug #688587.

 ChangeLog              |   36 +++++++++++++++++++++++++++
 tools/m4/method.m4     |   62 +++++++++++++++++++++++++++++++++++++++++-------
 tools/pm/Output.pm     |   52 ++++++++++++++++++++++++++++++++++++++-
 tools/pm/WrapParser.pm |   19 ++++++++++++++
 4 files changed, 158 insertions(+), 11 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 30eab2e..d715ea5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,39 @@
+2012-11-18  Josà Alburquerque  <jaalburquerque gmail com>
+
+	gmmproc: _WRAP_METHOD: Support wrapping methods with slots.
+
+	* tools/pm/WrapParser.pm (on_wrap_method): Add code to parse the
+	additional 'slot_name', 'slot_callback' and 'no_slot_copy' options.
+	- The 'slot_name' options specifies the name of the slot parameter in
+	  the C++ method declaration.
+	- The 'slot_callback' option specifies the name of the callback to
+	  pass to the C function.
+	- The 'no_slot_copy' option specifies that the actual slot should be
+	  passed to the C function in the data parameter and not a copy.  By
+	  default, a copy is used.
+	* tools/pm/Output.pm (output_wrap_meth): Pass the new slot options
+	along to the *METHOD() m4 macros.
+	(convert_args_cpp_to_c):
+	- If there is a slot parameter, ignore the final user_data parameter
+	  in the C function when comparing the argument count in the C++
+	  method and the C function.
+	- Convert a possible slot parameter to the address of the specified
+	  slot callback (with the 'slot_callback' option).  Report an error if
+	  no callback has been specified.
+	- Pass a 'slot_copy' variable as the final user data parameter to the
+	  C function.  The variable is declared by the _*METHOD() m4 macros.
+	* tools/m4/method.m4 (_METHOD):
+	(_STATIC_METHOD):
+	- Accept the new 'slot_type', 'slot_name' and 'no_slot_copy' options
+	  which specify the C++ slot type (without the const and the &), the
+	  C++ slot parameter name and whether to create a copy of the slot or
+	  not respectively.
+	- Insert code to declare a 'slot_copy' variable that is either a copy
+	  of the slot or a pointer to the actual C++ slot based on the
+	  'no_slot_copy' option.
+
+	Bug #688587.
+
 2012-11-15  Josà Alburquerque  <jaalburquerque gmail com>
 
 	giomm.h: Include the tlspassword.h header file.
diff --git a/tools/m4/method.m4 b/tools/m4/method.m4
index 41a5e76..3c48b3e 100644
--- a/tools/m4/method.m4
+++ b/tools/m4/method.m4
@@ -9,8 +9,8 @@ dnl
 
 dnl
 dnl method
-dnl           $1       $2     $3         $4       $5        $6        $7         $8          $9     $10       $11       $12         $13       $14         $15               $16        $17             $18
-dnl  _METHOD(cppname,cname,cpprettype,crettype,arglist,cdeclarations,cargs,cinitializations,const,refreturn,errthrow,deprecated,constversion,ifdef,arglist_without_types,out_param,out_param_cpptype,wrap_line)
+dnl           $1       $2     $3         $4       $5        $6        $7         $8          $9     $10       $11       $12         $13       $14         $15               $16        $17              $18        $19        $20         $21
+dnl  _METHOD(cppname,cname,cpprettype,crettype,arglist,cdeclarations,cargs,cinitializations,const,refreturn,errthrow,deprecated,constversion,ifdef,arglist_without_types,out_param,out_param_cpptype,slot_type,slot_name,no_slot_copy,wrap_line)
 define(`_METHOD',`dnl
 _PUSH(SECTION_CC)
 ifelse(`$12',,,`_DEPRECATE_IFDEF_START
@@ -21,8 +21,19 @@ $3 __CPPNAME__::$1`'($5)ifelse(`$9',1,` const')
 {
 ifelse(`$13',,dnl
 `ifelse(`$10'`$11',,dnl If it is not errthrow or refreturn
+dnl If a slot type has been specified insert a slot copy declaration.
+`ifelse(`$18',,,dnl
+dnl See if the slot should or should not be copied
+`ifelse(`$20',,dnl
+`  // Create a copy of the slot.
+  $18* slot_copy = new $18($19); ',dnl
+dnl
+`  // Use the original slot (not a copy).
+  $18* slot_copy = const_cast<$18*>(&$19);')
+
+')`'dnl
 dnl Insert the declarations for C output parameters
-`ifelse(`$6',,,`$6
+ifelse(`$6',,,`$6
 ')`'dnl
 ifelse(`$16',,dnl If no C++ output parameter is specified
 `ifelse(`$3',void,dnl If the C function returns voids:
@@ -41,7 +52,7 @@ ifelse(`$6',,,`  return retvalue;
 ')dnl
 ')'dnl End if it returns voids.
 dnl A C++ output parameter is specified:
-,`  _INITIALIZE($17,$4,`$16',`$2`'(ifelse(`$9',1,const_cast<__CNAME__*>(gobj()),gobj())`'ifelse(`$7',,,`, ')$7)',$18);
+,`  _INITIALIZE($17,$4,`$16',`$2`'(ifelse(`$9',1,const_cast<__CNAME__*>(gobj()),gobj())`'ifelse(`$7',,,`, ')$7)',$21);
 dnl
 dnl Insert the initializations for the C output parameters
 ifelse(`$8',,,`$8
@@ -50,6 +61,17 @@ ifelse(`$8',,,`$8
 dnl If is errthrow or refreturn
 `ifelse(`$11',,,`  GError* gerror = 0;
 ')dnl
+dnl If a slot type has been specified insert a slot copy declaration.
+ifelse(`$18',,,dnl
+dnl See if the slot should or should not be copied
+`ifelse(`$20',,dnl
+`  // Create a copy of the slot.
+  $18* slot_copy = new $18($19); ',dnl
+dnl
+`  // Use the original slot (not a copy).
+  $18* slot_copy = const_cast<$18*>(&$19);')
+
+')`'dnl
 dnl Insert the declarations for C output parameters
 ifelse(`$6',,,`$6
 ')`'dnl
@@ -57,7 +79,7 @@ ifelse(`$16',,dnl If no C++ output parameter is specified:
 `  ifelse(`$3',void,,``$3' retvalue = ')_CONVERT($4,`$3',`$2`'(ifelse(`$9',1,const_cast<__CNAME__*>(gobj()),gobj())`'ifelse(`$7',,,`, ')$7)');
 'dnl
 ,dnl A C++ output parameter is specified:
-`  _INITIALIZE($17,$4,`$16',`$2`'(ifelse(`$9',1,const_cast<__CNAME__*>(gobj()),gobj())`'ifelse(`$7',,,`, ')$7)',$18);
+`  _INITIALIZE($17,$4,`$16',`$2`'(ifelse(`$9',1,const_cast<__CNAME__*>(gobj()),gobj())`'ifelse(`$7',,,`, ')$7)',$21);
 'dnl
 )dnl
 ifelse(`$11',,,`dnl
@@ -87,8 +109,8 @@ _POP()')
 
 dnl
 dnl static method
-dnl                  $1       $2     $3         $4      $5        $6         $7         $8            $9      $10         $11       $12     $13        $14          $15
-dnl  _STATIC_METHOD(cppname,cname,cpprettype,crettype,arglist,cdeclarations,cargs,cinitializations,refreturn,errthrow,deprecated,ifdef,out_param,out_param_type,wrap_line)
+dnl                  $1       $2     $3         $4      $5        $6         $7         $8            $9      $10         $11       $12     $13        $14         $15      $16          $17       $18
+dnl  _STATIC_METHOD(cppname,cname,cpprettype,crettype,arglist,cdeclarations,cargs,cinitializations,refreturn,errthrow,deprecated,ifdef,out_param,out_param_type,slot_type,slot_name,no_slot_copy,wrap_line)
 define(`_STATIC_METHOD',`dnl
 _PUSH(SECTION_CC)
 ifelse(`$11',,,`_DEPRECATE_IFDEF_START
@@ -98,6 +120,17 @@ ifelse(`$12',,,`#ifdef $12'
 $3 __CPPNAME__::$1($5)
 {
 ifelse(`$9'`$10',,dnl
+dnl If a slot type has been specified insert a slot copy declaration.
+ifelse(`$15',,,dnl
+dnl See if the slot should or should not be copied
+`ifelse(`$17',,dnl
+`  // Create a copy of the slot.
+  $15* slot_copy = new $15($16); ',dnl
+dnl
+`  // Use the original slot (not a copy).
+  $15* slot_copy = const_cast<$15*>(&$16);')
+
+')`'dnl
 dnl Insert declarations for C the output parameters
 ifelse(`$6',,,`$6
 ')`'dnl
@@ -108,7 +141,7 @@ dnl Returns non-void:
 dnl Store the return if there are C output parameters
 ifelse(`$6',,`return ',``$3' retval = '))_CONVERT($4,`$3',`$2`'($7)');'dnl
 dnl A C++ output parameter is specified so initialize it from C return
-,`  _INITIALIZE($14,$4,`$13',`$2`'($7)',$15);'dnl
+,`  _INITIALIZE($14,$4,`$13',`$2`'($7)',$18);'dnl
 )
 dnl Insert the initializations for the C output parameters if there are any
 ifelse(`$8',,,`$8
@@ -118,13 +151,24 @@ ifelse(`$3',void,,`ifelse(`$6',,,`  return retval;
 ')')dnl
 ',dnl End if a C++ output parameter is specified.
 `ifelse(`$10',,,`  GError* gerror = 0;')
+dnl If a slot type has been specified insert a slot copy declaration.
+ifelse(`$15',,,dnl
+dnl See if the slot should or should not be copied
+`ifelse(`$17',,dnl
+`  // Create a copy of the slot.
+  $15* slot_copy = new $15($16); ',dnl
+dnl
+`  // Use the original slot (not a copy).
+  $15* slot_copy = const_cast<$15*>(&$16);')
+
+')`'dnl
 dnl Insert the declarations for the C output parameters
 ifelse(`$6',,,`$6
 ')`'dnl
 ifelse(`$13',,dnl If no C++ output parameter is specified:
   ifelse(`$3',void,,``$3' retvalue = ')_CONVERT($4,`$3',`$2`'($7)');dnl
 dnl A C++ output parameter is specified:
-,`  _INITIALIZE($14,$4,`$13',`$2`'($7)',$15);'dnl
+,`  _INITIALIZE($14,$4,`$13',`$2`'($7)',$18);'dnl
 )dnl
 ifelse(`$10',,,`
   if(gerror)
diff --git a/tools/pm/Output.pm b/tools/pm/Output.pm
index 1efad7b..a994325 100644
--- a/tools/pm/Output.pm
+++ b/tools/pm/Output.pm
@@ -390,7 +390,10 @@ sub output_wrap_meth($$$$$$$)
         convert_args_cpp_to_c($objCppfunc, $objCDefsFunc, 1, $line_num,
         $errthrow, $arg_list); #1 means it's static, so it has 'object'.
 
-      $str = sprintf("_STATIC_METHOD(%s,%s,\`%s\',%s,\`%s\',\`%s\',\`%s\',\`%s\',%s,%s,%s,%s,%s,%s,%s)dnl\n",
+      my $no_slot_copy = "";
+      $no_slot_copy = "no_slot_copy" if ($$objCppfunc{no_slot_copy});
+
+      $str = sprintf("_STATIC_METHOD(%s,%s,\`%s\',%s,\`%s\',\`%s\',\`%s\',\`%s\',%s,%s,%s,%s,%s,%s,`%s',`%s',`%s',%s)dnl\n",
         $$objCppfunc{name},
         $$objCDefsFunc{c_name},
         $$objCppfunc{rettype},
@@ -405,6 +408,9 @@ sub output_wrap_meth($$$$$$$)
         $ifdef,
         $output_var_name,
         $output_var_type,
+        $$objCppfunc{slot_type},
+        $$objCppfunc{slot_name},
+        $no_slot_copy,
         $line_num
         );
     } else {
@@ -412,7 +418,10 @@ sub output_wrap_meth($$$$$$$)
         convert_args_cpp_to_c($objCppfunc, $objCDefsFunc, 0, $line_num,
         $errthrow, $arg_list);
 
-      $str = sprintf("_METHOD(%s,%s,\`%s\',%s,\`%s\',\`%s\',\`%s\',\`%s\',%s,%s,%s,%s,%s,\`%s\',%s,%s,%s,%s)dnl\n",
+      my $no_slot_copy = "";
+      $no_slot_copy = "no_slot_copy" if ($$objCppfunc{no_slot_copy});
+
+      $str = sprintf("_METHOD(%s,%s,\`%s\',%s,\`%s\',\`%s\',\`%s\',\`%s\',%s,%s,%s,%s,%s,\`%s\',%s,%s,%s,`%s',`%s',`%s',%s)dnl\n",
         $$objCppfunc{name},
         $$objCDefsFunc{c_name},
         $$objCppfunc{rettype},
@@ -430,6 +439,9 @@ sub output_wrap_meth($$$$$$$)
         $ifdef,
         $output_var_name,
         $output_var_type,
+        $$objCppfunc{slot_type},
+        $$objCppfunc{slot_name},
+        $no_slot_copy,
         $line_num
         );
     }
@@ -926,6 +938,11 @@ sub convert_args_cpp_to_c($$$$$)
     $$cpp_param_mappings{ $c_param_names[$num_c_args_expected]} = $cpp_index;
   }
 
+  # If the method has a slot temporarily decrement the C arg count when
+  # comparing the C++ and C argument count because the C function would
+  # have a final 'gpointer data' parameter.
+  $num_c_args_expected-- if ($$objCppfunc{slot_name});
+
   if ( $num_cpp_args != $num_c_args_expected )
   {
     Output::error( "convert_args_cpp_to_c(): Incorrect number of arguments. (%d != %d)\n",
@@ -937,6 +954,9 @@ sub convert_args_cpp_to_c($$$$$)
     return ("", "", "");
   }
 
+  # Reincrement the expected C argument count if there is a slot.
+  $num_c_args_expected++ if ($$objCppfunc{slot_name});
+
   # If there is an output parameter it must be processed so re-increment (now)
   # the number of C++ arguments.
   $num_cpp_args++ if($has_output_param);
@@ -1018,6 +1038,29 @@ sub convert_args_cpp_to_c($$$$$)
       next;
     }
 
+    # If dealing with a slot.
+    if ($$objCppfunc{slot_name} eq $cppParamName)
+    {
+      if ($$objCppfunc{slot_callback})
+      {
+        # The conversion for the slot is the address of the callback.
+        push(@conversions, "&" . $$objCppfunc{slot_callback});
+      }
+      else
+      {
+        Output::error(
+          "convert_args_cpp_to_c(): Missing a slot callback.  " .
+          "Specify it with the 'slot_callback' option.\n",);
+      }
+
+      # Get the slot type without the const and the & and store it so
+      # it can be passed to the m4 _*METHOD macros.
+      $cppParamType =~ /^const\s+(.*)&/;
+      $$objCppfunc{slot_type} = $1;
+
+      next;
+    }
+
     if ($cppParamType ne $cParamType) #If a type conversion is needed.
     {
 
@@ -1034,6 +1077,11 @@ sub convert_args_cpp_to_c($$$$$)
     }
   }
 
+  # Append the final slot copy parameter to the C function if the
+  # method has a slot.  The parameter name is consistent with the name
+  # in the _*METHOD() m4 macros.
+  push(@conversions, "slot_copy") if ($$objCppfunc{slot_name});
+
   return ( join(", ", @conversions), join("\n", @declarations),
     join("\n  ", @initializations) );
 }
diff --git a/tools/pm/WrapParser.pm b/tools/pm/WrapParser.pm
index 4ce03a7..f538b96 100644
--- a/tools/pm/WrapParser.pm
+++ b/tools/pm/WrapParser.pm
@@ -942,6 +942,25 @@ sub on_wrap_method($)
     {
     	$ifdef = $1;
     }
+    # The "slot_name" option tells gmmproc the name of the parameter
+    # that is a slot in the method if there is one.
+    elsif($argRef =~ /^slot_name\s+(\w+)/)
+    {
+      $$objCppfunc{slot_name} = $1;
+    }
+    # The "slot_callback" option tells gmmproc the name of the
+    # callback function that should be passed to the C function if the
+    # method has a slot.
+    elsif($argRef =~ /^slot_callback\s+(\w+)/)
+    {
+      $$objCppfunc{slot_callback} = $1;
+    }
+    # The "no_slot_copy" options tells gmmproc to pass the actual slot
+    # and not a copy of it to the C function in the data parameter.
+    if($argRef eq "no_slot_copy")
+    {
+      $$objCppfunc{no_slot_copy} = 1;
+    }
   }
 
   if ($commentblock ne '')



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]