Skip to content

Commit

Permalink
feat(api): [core] add Addressable
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 17, 2025
1 parent 7a963c6 commit ad9b23c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package overrungl.struct;

import overrungl.util.Addressable;
import overrungl.util.Unmarshal;

import java.lang.foreign.MemorySegment;
Expand All @@ -29,7 +30,7 @@
///
/// @author squid233
/// @since 0.1.0
public abstract class Struct {
public abstract class Struct implements Addressable {
private final MemorySegment segment;
private final StructLayout layout;

Expand Down Expand Up @@ -64,6 +65,7 @@ public long estimateCount() {
}

/// {@return the segment of this struct}
@Override
public MemorySegment segment() {
return segment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package overrungl.struct;

import overrungl.util.Addressable;
import overrungl.util.Unmarshal;

import java.lang.foreign.MemorySegment;
Expand All @@ -25,7 +26,7 @@
///
/// @author squid233
/// @since 0.1.0
public abstract class Union {
public abstract class Union implements Addressable {
private final MemorySegment segment;
private final UnionLayout layout;

Expand Down Expand Up @@ -60,6 +61,7 @@ public long estimateCount() {
}

/// {@return the segment of this union}
@Override
public MemorySegment segment() {
return segment;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* MIT License
*
* Copyright (c) 2025 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrungl.util;

import java.lang.foreign.MemorySegment;

/// An instance that holds a [MemorySegment].
///
/// @author squid233
/// @since 0.1.0
public interface Addressable {
/// {@return the segment of this instance}
MemorySegment segment();
}
13 changes: 6 additions & 7 deletions modules/overrungl.core/src/main/java/overrungl/util/Marshal.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
* Copyright (c) 2024-2025 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -17,7 +17,6 @@
package overrungl.util;

import org.jetbrains.annotations.Nullable;
import overrungl.struct.Struct;
import overrungl.upcall.Upcall;

import java.lang.foreign.Arena;
Expand Down Expand Up @@ -64,14 +63,14 @@ public static MemorySegment marshal(SegmentAllocator allocator, @Nullable String
}

/**
* Converts the given struct to a segment.
* Converts the given addressable object to a segment.
*
* @param struct the struct
* @param addressable the addressable object
* @return the segment
*/
public static MemorySegment marshal(@Nullable Struct struct) {
if (struct == null) return MemorySegment.NULL;
return struct.segment();
public static MemorySegment marshal(@Nullable Addressable addressable) {
if (addressable == null) return MemorySegment.NULL;
return addressable.segment();
}

/**
Expand Down

0 comments on commit ad9b23c

Please sign in to comment.