Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing references in records #18

Open
Neiko2002 opened this issue Sep 21, 2016 · 0 comments
Open

Storing references in records #18

Neiko2002 opened this issue Sep 21, 2016 · 0 comments
Assignees
Milestone

Comments

@Neiko2002
Copy link
Member

It should be possible to store references to other Java objects in a record.

interface ImageData {
    @Reference
    public BufferedImage getImage();
    public void setImage(BufferedImage image);
}

The generated source should look like this

public class ImageDataRecord implements ImageData {
    public static BufferedImage[] images;
    public BufferedImage getImage() {
        return images[index()];
    }
    public void setImage(BufferedImage image) {
        images[index()] = image;
    }

    // these functions will be added if a reference is needed somewhere in the record
    public void init(int index) {
        memory.setInt(address(), index);
    }

    public int index() {
        return memory.getInt(address());
    }

    public static void ensureCapacity(int size) {
        images = new BufferedImage[size];
    }

    public void release() {
        images[index()] = null;
    }
}

The init function gets called every time a record is allocated. The passed index is handled by the RecordAdapter ensuring it is not used by another record of this type. The capacity of the underlying array gets increased by the adapter with a step size of 16. Release() will also be called by the adapter.

This features needs a lot of managing functions in order to be fast, a simple but slow version could look like this.

public class ImageDataRecord implements ImageData {
    public static Map<Long, BufferedImage> images = new HashMap<>();
    public BufferedImage getImage() {
        return images.get(address());
    }
    public void setImage(BufferedImage image) {
        images.put(address(), image);
    }

    // these functions will be added if a reference is needed somewhere in the record
    public void release() {
        images.remove(address());
    }
}
@Neiko2002 Neiko2002 added this to the Release 1.2 milestone Sep 21, 2016
@Neiko2002 Neiko2002 self-assigned this Sep 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant