commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r786868 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/java/org/apache/commons/runtime/exception/ main/native/shared/ test/org/apache/commons/runtime/
Date Sat, 20 Jun 2009 17:44:03 GMT
Author: mturk
Date: Sat Jun 20 17:44:02 2009
New Revision: 786868

URL: http://svn.apache.org/viewvc?rev=786868&view=rev
Log:
Add structure wrap/unwrap mathods

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidStructureElementException.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
    commons/sandbox/runtime/trunk/src/main/native/shared/structure.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
Sat Jun 20 17:44:02 2009
@@ -15,6 +15,7 @@
  */
 
 package org.apache.commons.runtime;
+import org.apache.commons.runtime.exception.InvalidStructureElementException;
 import java.lang.annotation.*;
 import java.lang.reflect.*;
 import java.util.Arrays;
@@ -47,6 +48,7 @@
     }
 
     private void init()
+        throws InvalidStructureElementException
     {
         if (inited)
             return;
@@ -77,6 +79,11 @@
                         if (offset[i] == o.value()) {
                             fields[i] = f;
                             esizes[i] = getElementSize(f, this);
+                            if (esizes[i] == 0) {
+                                throw new InvalidStructureElementException(
+                                        "Field " + f.getName() +
+                                        " has invalid class signature");
+                            }
                             asizes[i] = getArrayElementLength(f, this);
                             align     = Math.max(align, esizes[i]);
                         }
@@ -85,7 +92,16 @@
             }
             sizeof = 0;
             for (i = 0; i < count; i++) {
-                sizeof += (sizeof + esizes[i]) % esizes[i];
+                //
+                // padding    = (align - (offset % align)) % align
+                // new offset = offset + ((align - (offset % align)) % align)
+                // or
+                // padding    = (align + ((offset - 1) & ~(align - 1))) - offset
+                // new offset =  align + ((offset - 1) & ~(align - 1))
+                // where align is primitive element size
+                //
+                // sizeof += (sizeof + esizes[i]) % esizes[i];
+                sizeof = esizes[i] + ((sizeof - 1) & ~(esizes[i] - 1));
                 offset[i] = sizeof;
                 if (asizes[i] > 0)
                     sizeof += (esizes[i] * asizes[i]);
@@ -93,11 +109,11 @@
                     sizeof += esizes[i];
             }
             if (align > Pointer.SIZEOF) {
-            	// Align to the pointer size except
-            	// for the structures which elements are not
-            	// larger the size of a pointer.
-            	align = Pointer.SIZEOF;
-			}
+                // Align to the pointer size except
+                // for the structures which elements are not
+                // larger then a size of a pointer.
+                align = Pointer.SIZEOF;
+            }
             sizeof = align(sizeof, align);
         }
     }
@@ -124,8 +140,10 @@
             n = 8;
         else if (c == double.class)
             n = 8;
-        else
+        else if (c == Pointer.class)
             n = Pointer.SIZEOF;
+        else
+            n = 0;
         return n;
     }
 
@@ -174,6 +192,7 @@
      *         not found.
      */
     public final int offsetof(String name)
+        throws InvalidStructureElementException
     {
         init();
         for (int i = 0; i < count; i++) {
@@ -192,6 +211,7 @@
      * @return element count.
      */
     public final int count()
+        throws InvalidStructureElementException
     {
         init();
         return count;
@@ -203,6 +223,7 @@
      * @return structure size.
      */
     public final int sizeof()
+        throws InvalidStructureElementException
     {
         init();
         return sizeof;
@@ -219,7 +240,7 @@
      * @return structure size.
      */
     public final int sizeof(int index)
-        throws IndexOutOfBoundsException
+        throws IndexOutOfBoundsException, InvalidStructureElementException
     {
         init();
         if (index < 0 || index >= count)
@@ -240,9 +261,98 @@
      * @return structure alignment in bytes.
      */
     public final int alignment()
-    {
+         throws InvalidStructureElementException
+   {
         init();
         return align;
     }
 
+    /**
+     * Wrap {@code this} abstract structure elements to {@code struct}.
+     *
+     * @param struct Structure into which to copy {@code} this
+     *        structure data.
+     *
+     */
+    public final void wrap(Structure struct)
+        throws IndexOutOfBoundsException, IllegalAccessException,
+               InvalidStructureElementException, NullPointerException
+    {
+        init();
+        if (struct == null)
+            throw new NullPointerException();
+        if (sizeof > struct.sizeof())
+            throw new IndexOutOfBoundsException();
+        for (int i = 0; i < count; i++) {
+            Field f = fields[i];
+            Class c = f.getType();
+            if (c.isArray()) {
+
+            }
+            else {
+                if (c == byte.class)
+                    struct.poke(offset[i], f.getByte(this));
+                else if (c == char.class)
+                    struct.poke16(offset[i], f.getChar(this));
+                else if (c == short.class)
+                    struct.poke16(offset[i], f.getShort(this));
+                else if (c == int.class)
+                    struct.poke32(offset[i], f.getInt(this));
+                else if (c == float.class)
+                    struct.poke32(offset[i], f.getFloat(this));
+                else if (c == long.class)
+                    struct.poke64(offset[i], f.getLong(this));
+                else if (c == double.class)
+                    struct.poke64(offset[i], f.getDouble(this));
+                else {
+                    struct.poke(offset[i], (Pointer)f.get(this));
+                }
+            }
+        }
+    }
+
+    /**
+     * Unwrap {@code struct} to {@code this} abstract structure.
+     *
+     * @param struct Structure from which to copy {@code data} to
+     *        {@code this} structure elements.
+     *
+     */
+    public final void unwrap(Structure struct)
+        throws IndexOutOfBoundsException, IllegalAccessException,
+               InvalidStructureElementException, NullPointerException
+    {
+        init();
+        if (struct == null)
+            throw new NullPointerException();
+        if (sizeof > struct.sizeof())
+            throw new IndexOutOfBoundsException();
+        for (int i = 0; i < count; i++) {
+            Field f = fields[i];
+            Class c = f.getType();
+            if (c.isArray()) {
+
+            }
+            else {
+                if (c == byte.class)
+                    f.setByte(this, (byte)struct.peek(offset[i]));
+                else if (c == char.class)
+                    f.setChar(this, (char)struct.peek16(offset[i]));
+                else if (c == short.class)
+                    f.setShort(this, (short)struct.peek16(offset[i]));
+                else if (c == int.class)
+                    f.setInt(this, struct.peek32(offset[i]));
+                else if (c == float.class)
+                    f.setFloat(this, struct.peek32f(offset[i]));
+                else if (c == long.class)
+                    f.setLong(this, struct.peek64(offset[i]));
+                else if (c == double.class)
+                    f.setDouble(this, struct.peek64f(offset[i]));
+                else {
+                    f.set(this, struct.get(offset[i]));
+                }
+            }
+        }
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java Sat
Jun 20 17:44:02 2009
@@ -313,7 +313,7 @@
             throw new NullPointerException();
         if (offset < 0L || length < 1L)
             throw new IllegalArgumentException();
-            
+
         clear0(ptr, offset, length);
     }
 
@@ -343,7 +343,7 @@
             throw new NullPointerException();
         if (offset < 0L || length < 1L)
             throw new IllegalArgumentException();
-            
+
         set0(ptr, offset, length, val);
     }
 
@@ -373,7 +373,7 @@
             throw new NullPointerException();
         if (offset < 0L || length < 1)
             throw new IllegalArgumentException();
-            
+
         return array0(ptr, offset, length);
     }
 
@@ -403,7 +403,7 @@
             throw new NullPointerException();
         if (offset < 0L || length < 1)
             throw new IllegalArgumentException();
-            
+
         return array1(ptr, offset, length);
     }
 
@@ -433,7 +433,7 @@
             throw new NullPointerException();
         if (offset < 0L || length < 1)
             throw new IllegalArgumentException();
-            
+
         return array2(ptr, offset, length);
     }
 
@@ -463,7 +463,7 @@
             throw new NullPointerException();
         if (offset < 0L || length < 1)
             throw new IllegalArgumentException();
-            
+
         return array3(ptr, offset, length);
     }
 
@@ -493,7 +493,7 @@
             throw new NullPointerException();
         if (offset < 0L || length < 1)
             throw new IllegalArgumentException();
-            
+
         return array4(ptr, offset, length);
     }
 

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java Sat
Jun 20 17:44:02 2009
@@ -64,7 +64,7 @@
      */
     public static final int align(int size, int boundary)
     {
-        return (((size) + ((boundary) - 1)) & ~((boundary) - 1));
+        return ((size + (boundary - 1)) & ~(boundary - 1));
     }
 
     /** Alignment to default {@code boundary} function.
@@ -80,7 +80,7 @@
      */
     public static final long align(long size, int boundary)
     {
-        return (((size) + ((boundary) - 1)) & ~((boundary) - 1));
+        return ((size + (boundary - 1)) & ~(boundary - 1));
     }
 
     /** Alignment to default {@code boundary} function.

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure.java?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure.java
Sat Jun 20 17:44:02 2009
@@ -42,6 +42,21 @@
     public abstract int sizeof();
 
     /**
+     * Create a new {@code Pointer} object with value this {@code structure}
+     * contains at the {@code index}.
+     *
+     * @return a {@code Pointer} at {@code index}.
+     * @throws IndexOutOfBoundsException if {@code index} would cause access
+     *          outside the structure address space.
+     * @throws NullPointerException if structure pointer is {@code null}.
+     * @throws UnalignedParameterException if attempt is made to access the
+     *          address that is aligned to the {@code value} primitive type.
+     */
+    public abstract Pointer get(int index)
+        throws IndexOutOfBoundsException, UnalignedParameterException,
+               NullPointerException;
+
+    /**
      * Get a {@code byte} value this {@code structure} contains at the
      * {@code index}.
      *
@@ -129,6 +144,23 @@
                NullPointerException;
 
     /**
+     * Set a {@code Pointer} value to this {@code structure} at the
+     * {@code index} location.
+     *
+     * @param value Value to set at {@code index}.
+     *
+     * @return a {@code Pointer} at {@code index}.
+     * @throws IndexOutOfBoundsException if {@code index} would cause access
+     *          outside the structure address space.
+     * @throws NullPointerException if structure pointer is {@code null}.
+     * @throws UnalignedParameterException if attempt is made to access the
+     *          address that is aligned to the {@code value} primitive type.
+     */
+    public abstract void poke(int index, Pointer v)
+        throws IndexOutOfBoundsException, UnalignedParameterException,
+               NullPointerException;
+
+    /**
      * Set a {@code byte} value to this {@code structure} at the
      * {@code index} location.
      *

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
Sat Jun 20 17:44:02 2009
@@ -46,6 +46,8 @@
     private static native void   poke4(int addr, float  v);
     private static native void   poke5(int addr, double v);
 
+    private static native Pointer get0(int addr);
+
     public int sizeof()
     {
         return POINTER.PLENGTH;
@@ -62,6 +64,32 @@
         return POINTER.equals(((Structure32)other).POINTER);
     }
 
+    public Pointer get(int index)
+        throws IndexOutOfBoundsException, UnalignedParameterException,
+               NullPointerException
+    {
+        if (POINTER.POINTER == 0)
+            throw new NullPointerException();
+        else if (index < 0 || (index + 4) > POINTER.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if ((index & 0x03) != 0)
+            throw new UnalignedParameterException();
+        return get0(POINTER.POINTER + index);
+	}
+
+    public void poke(int index, Pointer v)
+        throws IndexOutOfBoundsException, UnalignedParameterException,
+               NullPointerException
+    {
+        if (POINTER.POINTER == 0)
+            throw new NullPointerException();
+        else if (index < 0 || (index + 4) > POINTER.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if ((index & 0x03) != 0)
+            throw new UnalignedParameterException();
+        poke2(POINTER.POINTER + index, ((Pointer32)v).POINTER);
+	}
+
     public int peek(int index)
         throws IndexOutOfBoundsException, NullPointerException
     {

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
Sat Jun 20 17:44:02 2009
@@ -46,6 +46,8 @@
     private static native void   poke4(long addr, float  v);
     private static native void   poke5(long addr, double v);
 
+    private static native Pointer get0(long addr);
+
     public int sizeof()
     {
         return (int)POINTER.PLENGTH;
@@ -62,6 +64,32 @@
         return POINTER.equals(((Structure64)other).POINTER);
     }
 
+    public Pointer get(int index)
+        throws IndexOutOfBoundsException, UnalignedParameterException,
+               NullPointerException
+    {
+        if (POINTER.POINTER == 0L)
+            throw new NullPointerException();
+        else if (index < 0L || (index + 8) > POINTER.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if ((index & 0x07) != 0)
+            throw new UnalignedParameterException();
+        return get0(POINTER.POINTER + index);
+	}
+
+    public void poke(int index, Pointer v)
+        throws IndexOutOfBoundsException, UnalignedParameterException,
+               NullPointerException
+    {
+        if (POINTER.POINTER == 0L)
+            throw new NullPointerException();
+        else if (index < 0L || (index + 8) > POINTER.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if ((index & 0x07) != 0)
+            throw new UnalignedParameterException();
+        poke3(POINTER.POINTER + index, ((Pointer64)v).POINTER);
+	}
+
     public int peek(int index)
         throws IndexOutOfBoundsException, UnalignedParameterException
     {
@@ -82,7 +110,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x01) != 0)
             throw new UnalignedParameterException();
-        return peek1(POINTER.POINTER + index / 2);
+        return peek1(POINTER.POINTER + index);
     }
 
     public int peek32(int index)
@@ -95,7 +123,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x03) != 0)
             throw new UnalignedParameterException();
-        return peek2(POINTER.POINTER + index / 4);
+        return peek2(POINTER.POINTER + index);
     }
 
     public long peek64(int index)
@@ -108,7 +136,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x07) != 0)
             throw new UnalignedParameterException();
-        return peek3(POINTER.POINTER + index / 8);
+        return peek3(POINTER.POINTER + index);
     }
 
     public float peek32f(int index)
@@ -121,7 +149,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x03) != 0)
             throw new UnalignedParameterException();
-        return peek4(POINTER.POINTER + index / 4);
+        return peek4(POINTER.POINTER + index);
     }
 
     public double peek64f(int index)
@@ -134,7 +162,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x07) != 0)
             throw new UnalignedParameterException();
-        return peek5(POINTER.POINTER + index / 8);
+        return peek5(POINTER.POINTER + index);
     }
 
     public void poke(int index, int value)
@@ -157,7 +185,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x01) != 0)
             throw new UnalignedParameterException();
-        poke1(POINTER.POINTER + index / 2, value);
+        poke1(POINTER.POINTER + index, value);
     }
 
     public void poke32(int index, int value)
@@ -170,7 +198,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x03) != 0)
             throw new UnalignedParameterException();
-        poke2(POINTER.POINTER + index / 4, value);
+        poke2(POINTER.POINTER + index, value);
     }
 
     public void poke32(int index, float value)
@@ -183,7 +211,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x03) != 0)
             throw new UnalignedParameterException();
-        poke4(POINTER.POINTER + index / 4, value);
+        poke4(POINTER.POINTER + index, value);
     }
 
     public void poke64(int index, long value)
@@ -196,7 +224,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x07) != 0)
             throw new UnalignedParameterException();
-        poke3(POINTER.POINTER + index / 8, value);
+        poke3(POINTER.POINTER + index, value);
     }
 
     public void poke64(int index, double value)
@@ -209,7 +237,7 @@
             throw new IndexOutOfBoundsException();
         else if ((index & 0x07) != 0)
             throw new UnalignedParameterException();
-        poke5(POINTER.POINTER + index / 8, value);
+        poke5(POINTER.POINTER + index, value);
     }
 
     public String toString()
@@ -223,3 +251,4 @@
     }
 
 }
+

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidStructureElementException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidStructureElementException.java?rev=786868&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidStructureElementException.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidStructureElementException.java
Sat Jun 20 17:44:02 2009
@@ -0,0 +1,39 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.exception;
+
+/**
+ * InvalidStructureElementException is thrown when an
+ * {@code AbstractStructure} field is invalid.
+ *
+ * @author Mladen Turk
+ *
+ */
+
+public class InvalidStructureElementException extends RuntimeException
+{
+
+    public InvalidStructureElementException()
+    {
+        super();
+    }
+
+    public InvalidStructureElementException(String msg)
+    {
+        super(msg);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidStructureElementException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/structure.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/structure.c?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/structure.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/structure.c Sat Jun 20 17:44:02 2009
@@ -120,3 +120,13 @@
 
     *(N2P(a, jdouble *)) = v;
 }
+
+ACR_STR_EXPORT_DECLARE(jobject, get0)(ACR_JNISTDARGS, jniptr a)
+{
+	jniptr p;
+    UNREFERENCED_O;
+
+	p = *(N2P(a, jniptr *));
+    return ACR_PointerCreate(_E, (void *)p, sizeof(void *), NULL);
+}
+

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java?rev=786868&r1=786867&r2=786868&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java Sat
Jun 20 17:44:02 2009
@@ -16,6 +16,7 @@
 
 package org.apache.commons.runtime;
 
+import org.apache.commons.runtime.exception.*;
 import java.lang.annotation.*;
 import java.lang.reflect.*;
 import java.lang.System;
@@ -39,14 +40,14 @@
     {
         System.loadLibrary("acr");
     }
-    
+
 
     protected class My1Structure extends AbstractStructure
     {
 
         @OffsetOf(0)
         public int IFIELD;
-        
+
         // This field has no annotation, so not visible
         public int SFIELD;
 
@@ -58,8 +59,8 @@
     {
 
         @OffsetOf(0)
-        public int IFIELD;
-        
+        public int I1FIELD;
+
         @OffsetOf(4)
         public Pointer P1FIELD;
 
@@ -73,11 +74,11 @@
         @OffsetOf(0)
         public int   I1FIELD;
         @OffsetOf(4)  // 8
-        public long  L1FIELD;        
+        public long  L1FIELD;
         @OffsetOf(8)  // 16
-        public long  L2FIELD;        
+        public long  L2FIELD;
         @OffsetOf(12) // 20
-        public char  C1FIELD;        
+        public char  C1FIELD;
     }
 
     protected class MyAStructure extends AbstractStructure
@@ -86,9 +87,19 @@
         @OffsetOf(0)
         public int      I1FIELD;
         @OffsetOf(1)
-        public byte []  BAFIELD = new byte[10];        
+        public byte []  BAFIELD = new byte[10];
         @OffsetOf(2)
-        public int  []  AAFIELD;        
+        public int  []  AAFIELD;
+    }
+
+    protected class MyInvalidStructure extends AbstractStructure
+    {
+
+        @OffsetOf(0)
+        public int I1FIELD;
+
+        @OffsetOf(4)
+        public String P1FIELD;
     }
 
     public void testAnnotation()
@@ -99,6 +110,24 @@
         assertEquals("Sizeof", 4, s.sizeof());
     }
 
+    public void testInvalidStructure()
+        throws Throwable
+    {
+        MyInvalidStructure s = new MyInvalidStructure();
+        try {
+        	int c = s.count();
+        	fail("Exception not thrown");
+        }
+        catch (InvalidStructureElementException i)
+        {
+        	// OK.
+        }
+        catch (Exception e)
+        {
+        	fail("Wrong exception thrown : " + e);
+        }
+    }
+
     public void testSize()
         throws Throwable
     {
@@ -108,7 +137,7 @@
             assertEquals("Sizeof", 12, s.sizeof());
         }
         else {
-            assertEquals("Sizeof", 24, s.sizeof());        
+            assertEquals("Sizeof", 24, s.sizeof());
         }
     }
 
@@ -137,5 +166,51 @@
         assertEquals("Fields",  3, s.count());
     }
 
+    public void testWrap()
+        throws Throwable
+    {
+        MyUStructure a = new MyUStructure();
+        MyUStructure b = new MyUStructure();
+        assertEquals("Fields",  4, a.count());
+
+        Pointer p = Memory.calloc(1000);
+        assertNotNull("Pointer", p);
+        Structure s = p.asStructure();
+
+        a.I1FIELD = 2303;
+        a.L1FIELD = 23031964;
+        a.L2FIELD = 27111970;
+        a.C1FIELD = 'M';
+        a.wrap(s);
+        b.unwrap(s);
+        assertEquals("Int  Value",  a.I1FIELD, b.I1FIELD);
+        assertEquals("Long Value",  a.L1FIELD, b.L1FIELD);
+        assertEquals("Long Value",  a.L2FIELD, b.L2FIELD);
+        assertEquals("Char Value",  a.C1FIELD, b.C1FIELD);
+
+   }
+
+    public void testWrapPointer()
+        throws Throwable
+    {
+        My2Structure a = new My2Structure();
+        My2Structure b = new My2Structure();
+        assertEquals("Fields",  3, a.count());
+
+        Pointer p = Memory.calloc(1000);
+        assertNotNull("Pointer", p);
+        Structure s = p.asStructure();
+
+        a.I1FIELD = 2303;
+        a.P1FIELD = Memory.slice(p, 128, Pointer.SIZEOF);
+        a.P2FIELD = Pointer.NULL;
+        a.wrap(s);
+        b.unwrap(s);
+        assertEquals("Int  Value",  a.I1FIELD, b.I1FIELD);
+        assertTrue("Pointer Value",  a.P1FIELD.equals(b.P1FIELD));
+        assertTrue("Null Value",  a.P2FIELD.equals(b.P2FIELD));
+
+   }
+
 }
 



Mime
View raw message