[Bind] Add int overloads to buffer size (IntPtr) parameters
authorthefiddler <stapostol@gmail.com>
Tue, 2 Sep 2014 07:48:39 +0000 (09:48 +0200)
committerthefiddler <stapostol@gmail.com>
Tue, 2 Sep 2014 07:48:39 +0000 (09:48 +0200)
As a convenience, int overloads are provided for IntPtr size
parameters (corresponding to BufferSize or size_t). In the vast
majority of cases, a 32bit int is sufficient for buffer sizes,
so these overloads avoid the necessity of annoying (IntPtr) casts.

If more than 2^31-1 elements are required, the IntPtr overloads
remain available. (As always, this requires a 64bit runtime
environment.)

Source/Bind/FuncProcessor.cs
Source/Bind/Utilities.cs

index cb126e4..81a55cd 100644 (file)
@@ -324,19 +324,18 @@ namespace Bind
                 }
                 else
                 {
-                    // Todo: what is the point of this here? It is overwritten below.
-                    // A few translations for consistency
-                    switch (type.CurrentType.ToLower())
-                    {
-                        case "string":
-                            type.QualifiedType = "String";
-                            break;
-                    }
-
                     type.QualifiedType = s;
                 }
             }
 
+            if ((type.Array == 0 && type.Pointer == 0 && !type.Reference) &&
+                (type.QualifiedType.ToLower().Contains("buffersize") ||
+                type.QualifiedType.ToLower().Contains("sizeiptr") ||
+                type.QualifiedType.Contains("size_t")))
+            {
+                type.WrapperType |= WrapperTypes.SizeParameter;
+            }
+
             type.CurrentType =
                 Generator.CSTypes.ContainsKey(type.CurrentType) ?
                 Generator.CSTypes[type.CurrentType] : type.CurrentType;
@@ -960,6 +959,27 @@ namespace Bind
                         convenience_wrappers.Add(f);
                     }
                 }
+
+                // Check for IntPtr parameters that correspond to size_t (e.g. GLsizei)
+                // and add Int32 overloads for convenience.
+                {
+                    Function f = null;
+                    int i = 0;
+                    foreach (var p in d.Parameters)
+                    {
+                        if ((p.WrapperType & WrapperTypes.SizeParameter) != 0)
+                        {
+                            f = f ?? new Function(d);
+                            f.Parameters[i].QualifiedType = "Int32";
+                        }
+                        i++;
+                    }
+
+                    if (f != null)
+                    {
+                        convenience_wrappers.Add(f);
+                    }
+                }
             }
             return convenience_wrappers;
         }
index 11b4eb4..a8752b7 100644 (file)
@@ -89,6 +89,11 @@ namespace Bind
         /// Function takes a String[] parameter
         /// </summary>
         StringArrayParameter = 1 << 13,
+        /// <summary>
+        /// Functions takes an IntPtr that corresponds to a size_t.
+        /// Add an int32 overload for convenience.
+        /// </summary>
+        SizeParameter = 1 << 14,
     }
 
     #endregion