1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 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 package wrap; 26 27 import java.lang.foreign.GroupLayout; 28 import java.lang.foreign.MemoryLayout; 29 import java.lang.foreign.ValueLayout; 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.function.Consumer; 33 34 import static java.lang.foreign.MemoryLayout.structLayout; 35 import static java.lang.foreign.MemoryLayout.unionLayout; 36 import static java.lang.foreign.ValueLayout.JAVA_INT; 37 import static java.lang.foreign.ValueLayout.JAVA_LONG; 38 39 public class LayoutBuilder { 40 //String name; 41 List<MemoryLayout> layouts = new ArrayList<>(); 42 43 // final public MemoryLayout memoryLayout; 44 LayoutBuilder() { 45 //this.memoryLayout = memoryLayout; 46 } 47 48 LayoutBuilder struct(String name, Consumer<LayoutBuilder> consumer) { 49 LayoutBuilder lb = new LayoutBuilder(); 50 consumer.accept(lb); 51 MemoryLayout layout = structLayout(lb.layouts.toArray(new MemoryLayout[0])); 52 if (name != null) { 53 layout.withName(name); 54 } 55 layouts.add(layout); 56 return this; 57 } 58 59 LayoutBuilder union(String name, Consumer<LayoutBuilder> consumer) { 60 LayoutBuilder lb = new LayoutBuilder(); 61 consumer.accept(lb); 62 MemoryLayout layout = unionLayout(lb.layouts.toArray(new MemoryLayout[0])); 63 if (name != null) { 64 layout.withName(name); 65 } 66 layouts.add(layout); 67 return this; 68 } 69 70 public LayoutBuilder i32(String name) { 71 layouts.add(JAVA_INT.withName(name)); 72 return this; 73 } 74 public LayoutBuilder i64(String name) { 75 layouts.add(JAVA_LONG.withName(name)); 76 return this; 77 } 78 79 public MemoryLayout memoryLayout(){ 80 return layouts.getFirst(); 81 } 82 83 public LayoutBuilder i8Seq(String name, long elementCount) { 84 layouts.add(MemoryLayout.sequenceLayout(elementCount, ValueLayout.JAVA_BYTE).withName(name)); 85 return this; 86 } 87 public static LayoutBuilder structBuilder(String name, Consumer<LayoutBuilder> consumer) { 88 return new LayoutBuilder().struct(name, consumer); 89 } 90 public static GroupLayout structOf(String name, Consumer<LayoutBuilder> consumer) { 91 return (GroupLayout) structBuilder(name, consumer).memoryLayout(); 92 } 93 }