Open haxscramper opened 3 years ago
After some testing with codegen I think it is the best solution overall, but there are some issues, like making nim-declared types available in generated C++ code. If NewCxx
uses non-trivial nim type - how to make it available in generated header?
Also, 'derived' classes still won't behave as proper OOP on the nim side - e.g. I'd need to provide additional overloads for all procs. -- this seems to be partially covered by #1, but needs more feature-testing.
Base C++ class
cppbase.cpp
Automatically derived generated file - generated for every class definition during wrapping process.
Generated derived class definition
cppderived.hpp
Generated implementation for method implementations
cppderived.cpp
Wrappers
callbacks.nim
To override behavior of the class, you can set implementation callback to a new function:
main.nim
But I still can provide override for the behavior of the object without actually overriding anything, which might be quite useful for various 'DelegatePainter' OOP patterns, where you actually only want to override implementation of a single method and nothing else. With support for passing user data, and setting closures as implementation (and not just
{.cdecl.}
callbacks) it won't be necessary to derive from C++ classes in most cases anyway.-—
standalone classes
In rarer cases where you'd actually need to provide a full-fledged derived class, it is possible to implement some codegen facilities.
I couldn't find a way to generate standalone files that can be injected in nim object hierarchy (at least without some ugly hacks). In order to derive from C++ class, I would generate actual C++ class via nim macros, similarly to nim by example macros.
Will generate following C++ code:
Actually generating C++ code also helps with Qt - I no longer need to reimplement MOC, and instead can just use it as-is.