In a C++ library that I am wrapping, I have to inherit from one of the wrapped classes. So I have this:
type
OcctAisHello* = object of AIS_ViewController
myContext*:Handle[AISInteractiveContext]
myView*:Handle[V3d_View]
Then, I need to override some methods from the class. I did the following:
method processExpose(this:var OcctAisHello) {.exportc.} =
if not this.myView.isNull:
this.flushViewEvents( this.myContext, this.myView, true )
Is this the right way of overriden the methods?
I tried the following, like in the above mentioned thread:
proc processExpose() {.exportcpp:"AIS_ViewController::ProcessExpose",
codegenDecl: """
#ifdef ProcessExpose_SHOULD_BE_DEFINED
$1 $2 $3
#else
#define ProcessExpose_SHOULD_BE_DEFINED
#endif
""".} =
var this {.nodecl, importc, global.}: ptr OcctAisHello
#return (this[]).a
if not (this[]).myView.isNull:
(this[]).flushViewEvents( (this[]).myContext, (this[]).myView, true )
proc processExpose(self: OcctAisHello) {.importcpp: "#.$1(@)", nodecl.}
But when I try to compile it I get:
/home/jose/.cache/nim/viewer04_d/@mviewer04.nim.cpp:787:8: error: redefinition of 'void AIS_ViewController::ProcessExpose()'
787 | void AIS_ViewController::ProcessExpose (void)
| ^~~~~~~~~~~~~~~~~~
In file included from /home/jose/.cache/nim/viewer04_d/@mviewer04.nim.cpp:9:
/usr/include/opencascade/AIS_ViewController.hxx:430:16: note: 'virtual void AIS_ViewController::ProcessExpose()' previously defined here
430 | virtual void ProcessExpose() Standard_OVERRIDE {}
| ^~~~~~~~~~~~~
ยก
Any clue about what I did wrong?
I am revisiting this topic after I noticed the new virtual pragma.
My objective is to get something like this:
virtual void ProcessExpose() override
{
if (!myView.IsNull())
{
FlushViewEvents (myContext, myView, true);
}
}
I am trying the following:
proc processExpose*(self: OcctAisHello) {.virtual:"$1() override".} =
var this = self
if not self.myView.isNull:
this.flushViewEvents( self.myContext, self.myView, true )
and I am getting the following error marked 'override', but does not override:
/home/jose/.cache/nim/viewer04_d/@mviewer04.nim.cpp:118:38: error: 'virtual void tyObject_OcctAisHello__XdxJIqQ0m9cy9c9aT1sCM6pjA::processExpose()' marked 'override', but does not override
118 | N_LIB_PRIVATE N_NOCONV(virtual void, processExpose)() override;
| ^~~~~~~~~~~~~
Why is that?
The generated code looks like:
...
N_LIB_PRIVATE N_NOCONV(virtual void, processExpose)() override;
...
N_LIB_PRIVATE N_NOCONV(void, tyObject_OcctAisHello__XdxJIqQ0m9cy9c9aT1sCM6pjA::processExpose)() {
nimfr_("processExpose", "/home/jose/src/nimlang/occt.nim/examples/06 Visualization/viewer04.nim");
nimln_(80); tyObject_OcctAisHello__XdxJIqQ0m9cy9c9aT1sCM6pjA this_1 = (*this);
nimln_(81); {
NIM_BOOL T3_ = (*this).myView.IsNull();
if (!!(T3_)) goto LA4_;
{ nimln_(82); this_1.FlushViewEvents((*this).myContext, (*this).myView, NIM_TRUE);
} }
LA4_: ;
popFrame();
}
np, if you want to have better ergonomics you can copy my processVirtual and super implementation. It spins out a super function in the body of the proc that accepts the same params as the declared func. processVirtual has support for things like constcpp or constref. As a bonus it also capitalizes the name of the func. It's used like so:
proc editorCanAttachTo(inParent {.constcpp.}: AActorPtr, outReason: var FText): bool {.constcpp, override .} =
UE_Log "EditorCanAttachTo called in the parent"
self.super(inParent, outReason)
As this is part of a larger macro, self (this) is also injected as a first parameter