Does anyone have any experience on how to provide ...
# kotlin-native
m
Does anyone have any experience on how to provide the type of a numeric constant defined by a macro? Example: there is a function in the c library that takes Short Integer (16 bits) as a parameter. Next, a constant is defined via macro, so de facto without type. cinterop generates this constant as Integer (32 bits). So I have to use
.toShort()
every time I use it. Options: redefine each constant directly in the def file or subsequently in the kotlin code.
u
you can use .convert()
m
Thanks, I didn't know this function 🙂 Ok,
.convert()
helps me because I don't need to know what the type of the constant is, but it doesn't solve the problem I have (maybe I wasn't very precise): the constant is always used as a Short Integer, so I don't have to convert it to the correct type every time. I'm looking for a solution where I can specify the type of the constant. Ideally without having to redefine all types in the def / kt file.
u
Perhaps you need to provide some source code examples
m
sql.h and sqlucode.h
Copy code
./sql.h:117:#define SQL_DIAG_RETURNCODE        1
./sql.h:118:#define SQL_DIAG_NUMBER            2
./sql.h:119:#define SQL_DIAG_ROW_COUNT         3
./sql.h:120:#define SQL_DIAG_SQLSTATE          4
./sql.h:121:#define SQL_DIAG_NATIVE            5
./sql.h:122:#define SQL_DIAG_MESSAGE_TEXT      6
./sql.h:123:#define SQL_DIAG_DYNAMIC_FUNCTION  7
./sql.h:124:#define SQL_DIAG_CLASS_ORIGIN      8
./sql.h:125:#define SQL_DIAG_SUBCLASS_ORIGIN   9
./sql.h:126:#define SQL_DIAG_CONNECTION_NAME  10

./sqlucode.h:136:SQLRETURN SQL_API SQLGetDiagRecW(
./sqlucode.h-137-    SQLSMALLINT        fHandleType,
./sqlucode.h-138-    SQLHANDLE          handle,
...
knm file
Copy code
public const val SQL_DIAG_INSERT: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const val SQL_DIAG_MESSAGE_TEXT: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const val SQL_DIAG_NATIVE: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const val SQL_DIAG_NUMBER: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const val SQL_DIAG_RETURNCODE: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const val SQL_DIAG_REVOKE: <http://kotlin.Int|kotlin.Int> /* compiled code */
public const val SQL_DIAG_ROW_COUNT: <http://kotlin.Int|kotlin.Int> /* compiled code */

@kotlinx.cinterop.internal.CCall public external fun SQLGetDiagRecW(fHandleType: unixODBC.SQLSMALLINT /* = kotlin.Short */, handle: unixODBC.SQLHANDLE? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */, iRecord: unixODBC.SQLSMALLINT /* = kotlin.Short */, szSqlState: kotlinx.cinterop.CValuesRef<unixODBC.SQLWCHARVar /* = kotlinx.cinterop.UShortVarOf<kotlin.UShort> */>?, pfNativeError: kotlinx.cinterop.CValuesRef<unixODBC.SQLINTEGERVar /* = kotlinx.cinterop.IntVarOf<kotlin.Int> */>?, szErrorMsg: kotlinx.cinterop.CValuesRef<unixODBC.SQLWCHARVar /* = kotlinx.cinterop.UShortVarOf<kotlin.UShort> */>?, cbErrorMsgMax: unixODBC.SQLSMALLINT /* = kotlin.Short */, pcbErrorMsg: kotlinx.cinterop.CValuesRef<unixODBC.SQLSMALLINTVar /* = kotlinx.cinterop.ShortVarOf<kotlin.Short> */>?): unixODBC.SQLRETURN /* = kotlin.Short */ { /* compiled code */ }
kotlin code
Copy code
SQLGetDiagRecW(SQL_HANDLE_STMT.convert(), ...
so basically to tell the cinterop that the
SQL_DIAG_RETURNCODE
is a type of
SQLSMALLINT
which is type of
kotlin.Short