OpenHFT / Chronicle-Values

http://chronicle.software
Other
104 stars 37 forks source link

Simple value interface not working #17

Closed Jire closed 7 years ago

Jire commented 7 years ago

Created like this:

DrawableModelCoords coords = Values.newNativeInstance(DrawableModelCoords.class);

coords.setTranslateX(translateX);
coords.setTranslateY(translateY);
coords.setTranslateZ(translateZ);

coords.setRotationX(xRotation);
coords.setRotationY(yRotation);
coords.setRotationZ(zRotation);

coords.setScaleX(scaleX);
coords.setScaleY(scaleY);
coords.setScaleZ(scaleZ);
public interface DrawableModelCoords {

    int getTranslateX();
    void setTranslateX(int translateX);

    int getTranslateY();
    void setTranslateY(int translateY);

    int getTranslateZ();
    void setTranslateZ(int translateZ);

    int getRotationX();
    void setRotationX(int rotationX);

    int getRotationY();
    void setRotationY(int rotationY);

    int getRotationZ();
    void setRotationZ(int rotationZ);

    float getScaleX();
    void setScaleX(float scaleX);

    float getScaleY();
    void setScaleY(float scaleY);

    float getScaleZ();
    void setScaleZ(float scaleZ);

}
Jire commented 7 years ago

Is there anything special I need to do to use this library? I don't see the generated class in my output.

leventov commented 7 years ago

@Jire could you please post stack traces or a runnable test demonstrating a problem?

Jire commented 7 years ago

@leventov Let me know if you need more information.

Stack trace:

Exception in thread "main" java.lang.NullPointerException
    at test.DrawableModelCoords$$Native.setTranslateX(DrawableModelCoords$$Native.java:107)
    at test.ChronicleValuesTest.main(ChronicleValuesTest.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

Value interface:

package test;

public interface DrawableModelCoords {

    int getTranslateX();
    void setTranslateX(int translateX);

    int getTranslateY();
    void setTranslateY(int translateY);

    int getTranslateZ();
    void setTranslateZ(int translateZ);

    int getRotationX();
    void setRotationX(int rotationX);

    int getRotationY();
    void setRotationY(int rotationY);

    int getRotationZ();
    void setRotationZ(int rotationZ);

    float getScaleX();
    void setScaleX(float scaleX);

    float getScaleY();
    void setScaleY(float scaleY);

    float getScaleZ();
    void setScaleZ(float scaleZ);

}

Test:

package test;

import net.openhft.chronicle.values.Values;

public class ChronicleValuesTest {

    public static void main(String[] args) {
        DrawableModelCoords coords = Values.newNativeReference(DrawableModelCoords.class);

        coords.setTranslateX(1);
        coords.setTranslateY(2);
        coords.setTranslateZ(3);

        coords.setRotationX(4);
        coords.setRotationY(5);
        coords.setRotationZ(6);

        coords.setScaleX(7);
        coords.setScaleY(8);
        coords.setScaleZ(9);

        System.out.println(coords.getRotationX());
    }

}

Gradle:

compile group: 'net.openhft', name: 'chronicle-values', version: '1.5.3'
leventov commented 7 years ago

@Jire newNativeReference() literally creates a reference to off-heap memory, but it is "null" by default, just like on-heap null reference. You should cast the created coords object to Byteable (or maybe more convenient, add DrawableModelCoords extends Byteable) and set bytesStore() to some actual off-heap place, where you need to store your coords.

Or, if you don't need to store it off-heap, use Values.newHeapInstance() instead of newNativeReference().

leventov commented 7 years ago

@Jire see example here: https://github.com/OpenHFT/Chronicle-Values#use

Jire commented 7 years ago

@leventov Ahh, makes much sense! Thank you!

Jire commented 7 years ago

@leventov Sorry if this sounds stupid like the issue, but using newNativeReference creates a heap instance of DrawableModelCoords$$Native. What's the typical way to get a reference without creating an instance of the the generated native class?

What I was hoping for is for a function that gives you a single (or thread local) shared instance, which would use maybe some kind of offset system to give you an appropriate view of the interface. Is there anything like that in this library?

Jire commented 7 years ago

I implemented the above behavior to suit my needs by using Chronicle Bytes in a library here: https://github.com/Jire/Strukt