< prev index next >

src/java.base/share/classes/java/nicl/types/LayoutType.java

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nicl.types;
  27 
  28 import jdk.internal.nicl.types.DescriptorParser;
  29 import jdk.internal.nicl.types.Reference;
  30 import jdk.internal.nicl.types.References;
  31 
  32 import java.nicl.layout.Address;
  33 import java.nicl.layout.Group;
  34 import java.nicl.layout.Layout;
  35 import java.nicl.layout.Sequence;
  36 
  37 import java.lang.invoke.MethodHandle;
  38 import java.nicl.metadata.NativeType;
  39 
  40 /**
  41  * This class describes the relationship between a memory layout (usually described in bits) and a Java carrier
  42  * (e.g. {@code int}, {@code long}, or any Java reference type. A {@code LayoutType} defines operation for getting/setting
  43  * the layout contents using a given Java carrier (see {@link LayoutType#getter()} and {@link LayoutType#setter()}).
  44  * Moreover, a {@code LayoutType} defines operation for creating array and pointer derived {@code LayoutType} instances
  45  * (see {@link LayoutType#array()}, {@link LayoutType#array(int)} and {@link LayoutType#pointer()}).
  46  */
  47 public class LayoutType<X> {
  48 
  49     private final Reference reference;
  50     private final Layout layout;
  51 
  52     /* package */ LayoutType(Layout layout, Reference reference) {
  53         this.reference = reference;
  54         this.layout = layout;
  55     }
  56 
  57     public long bytesSize() {
  58         return layout().bitsSize() / 8;


 202      */
 203     public static LayoutType<?> ofVoid(Layout layout) {
 204         return new LayoutType<>(layout, null) {
 205             @Override
 206             public MethodHandle getter() throws UnsupportedOperationException {
 207                 throw new UnsupportedOperationException();
 208             }
 209 
 210             @Override
 211             public MethodHandle setter() throws UnsupportedOperationException {
 212                 throw new UnsupportedOperationException();
 213             }
 214         };
 215     }
 216 
 217     /**
 218      * Create a {@code LayoutType} from a {@link Struct} interface carrier.
 219      * @param <T> the struct type.
 220      * @param carrier the struct carrier.
 221      * @return the {@code LayoutType}.
 222      * @throws IllegalArgumentException if the given carrier is not annotated with the {@link java.nicl.metadata.NativeType} annotation.
 223      */
 224     public static <T extends Struct<T>> LayoutType<T> ofStruct(Class<T> carrier) throws IllegalArgumentException {
 225         NativeType nativeStruct = carrier.getAnnotation(NativeType.class);
 226         if (nativeStruct == null) {
 227             throw new IllegalArgumentException("Not a struct type!");
 228         }
 229         Group type = (Group) new DescriptorParser(nativeStruct.layout()).parseLayout().findFirst().get();
 230         return new LayoutType<>(type, References.ofStruct(carrier));
 231     }
 232 }


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nicl.types;
  27 
  28 import jdk.internal.nicl.types.DescriptorParser;
  29 import jdk.internal.nicl.types.Reference;
  30 import jdk.internal.nicl.types.References;
  31 
  32 import java.nicl.layout.Address;
  33 import java.nicl.layout.Group;
  34 import java.nicl.layout.Layout;
  35 import java.nicl.layout.Sequence;
  36 
  37 import java.lang.invoke.MethodHandle;
  38 import java.nicl.metadata.NativeStruct;
  39 
  40 /**
  41  * This class describes the relationship between a memory layout (usually described in bits) and a Java carrier
  42  * (e.g. {@code int}, {@code long}, or any Java reference type. A {@code LayoutType} defines operation for getting/setting
  43  * the layout contents using a given Java carrier (see {@link LayoutType#getter()} and {@link LayoutType#setter()}).
  44  * Moreover, a {@code LayoutType} defines operation for creating array and pointer derived {@code LayoutType} instances
  45  * (see {@link LayoutType#array()}, {@link LayoutType#array(int)} and {@link LayoutType#pointer()}).
  46  */
  47 public class LayoutType<X> {
  48 
  49     private final Reference reference;
  50     private final Layout layout;
  51 
  52     /* package */ LayoutType(Layout layout, Reference reference) {
  53         this.reference = reference;
  54         this.layout = layout;
  55     }
  56 
  57     public long bytesSize() {
  58         return layout().bitsSize() / 8;


 202      */
 203     public static LayoutType<?> ofVoid(Layout layout) {
 204         return new LayoutType<>(layout, null) {
 205             @Override
 206             public MethodHandle getter() throws UnsupportedOperationException {
 207                 throw new UnsupportedOperationException();
 208             }
 209 
 210             @Override
 211             public MethodHandle setter() throws UnsupportedOperationException {
 212                 throw new UnsupportedOperationException();
 213             }
 214         };
 215     }
 216 
 217     /**
 218      * Create a {@code LayoutType} from a {@link Struct} interface carrier.
 219      * @param <T> the struct type.
 220      * @param carrier the struct carrier.
 221      * @return the {@code LayoutType}.
 222      * @throws IllegalArgumentException if the given carrier is not annotated with the {@link java.nicl.metadata.NativeStruct} annotation.
 223      */
 224     public static <T extends Struct<T>> LayoutType<T> ofStruct(Class<T> carrier) throws IllegalArgumentException {
 225         NativeStruct nativeStruct = carrier.getAnnotation(NativeStruct.class);
 226         if (nativeStruct == null) {
 227             throw new IllegalArgumentException("Not a struct type!");
 228         }
 229         Group type = (Group) new DescriptorParser(nativeStruct.value()).parseLayout().findFirst().get();
 230         return new LayoutType<>(type, References.ofStruct(carrier));
 231     }
 232 }
< prev index next >