I am porting some C library. There are 2 types as below, MC_TABLECELLW for unicode and MC_TABLECELLA for ANSI. winimUnicode is bool value which is defined in system library(winim) . The purpose is to automatically use MC_TABLECELL as uniform interface, and select MC_TABLECELLW or MC_TABLECELLA at compile time. How could it work? the template below has problem.
type
MC_TABLECELLW* {.bycopy.} = object
fMask*: DWORD
pszText*: ptr WCHAR
cchTextMax*: int
lParam*: LPARAM
dwFlags*: DWORD
MC_TABLECELLA* {.bycopy.} = object
fMask*: DWORD
pszText*: ptr char
cchTextMax*: int
lParam*: LPARAM
dwFlags*: DWORD
template MC_TABLECELL* =
when winimUnicode: MC_TABLECELLW
elif winimAnsi: MC_TABLECELLA
var tc : MC_TABLECELL
There are two ways to approach this, depending on whether you can have two different tables at a time.
import winim
type
MC_TABLECELL*[wide: static bool] {.bycopy.} = object
fMask*: DWORD
when wide:
pszText*: ptr WChar
else:
pszText*: ptr char
cchTextMax*: int
lParam*: LPARAM
dwFlags*: DWORD
TableCellUnicode = McTableCell[true]
TableCellAnsi = McTableCell[false]
In this case we can use both unicode and ansi tablecells in a single runtime without issue if this is required.
import winim
when winimUnicode:
type CharType* = WChar
else:
type CharType* = char
type
MC_TABLECELL* {.bycopy.} = object
fMask*: DWORD
pszText*: ptr CharType
cchTextMax*: int
lParam*: LPARAM
dwFlags*: DWORD
In this case the type is changed whether we're using unicode or ansi for the entire project, so if we need the other we're not capable of it.