0

I have various Scala native methods. Some examples:

@native protected def xOpen(): Long
@native protected def flushNative(xServPtr: Long): Unit
@native protected def drawLineNative(xServPtr: Long, winPtr: Long, x1: Int, y1: Int, x2: Int, y2: Int): Unit
@native protected def newGraphicsContextNative(xServPtr: Long, winPtr: Long): Long

//And a couple of the signatures from the C++ file.
extern "C" JNIEXPORT void JNICALL Java_pXClient_XCClass_flushNative(JNIEnv * env, jobject c1, jlong displayPtr)
extern "C" JNIEXPORT jlong JNICALL Java_pXClient_XCClass_newGraphicsContextNative(JNIEnv * env, jobject c1,
    jlong displayPtr, jlong winPtr)

I would like to have stronger typing on the pointers rather than passing them all as longs. Is there a way to use AnyVal classes here eg:

class XServPtr(val value: Long) extends AnyVal
class WinPtr(val value: Long) extends AnyVal

and can I add custom types to JNI? Then I could do custom c++ implicit type conversions from the JNI type to the specific c++ pointer type and have type safety at both ends.

Rich Oliver
  • 6,001
  • 4
  • 34
  • 57

1 Answers1

0

If you want to be explicit in your types, you can use something like

type wPtr = Any
type HANDLE = wPtr
type HWND = wPtr
@native def SHGetFolderPath(hwndOwner: HWND, nFolder: Int, hToken: HANDLE, dwFlags: Int, pszPath: Array[Char]): Int

...at least, I'm about to give that a go :)

EDIT: In the end I made a Scala version of @585534 to do what I wanted.

Community
  • 1
  • 1
Peter
  • 71
  • 1
  • 2