From 9dfc7bfe5ca57d86935423e515dff0260f53d121 Mon Sep 17 00:00:00 2001 From: andri lim Date: Sat, 30 Jan 2016 11:25:45 +0700 Subject: [PATCH] Updated Playing with CPP VTABLE from Nim (markdown) --- Playing-with-CPP--VTABLE-from-Nim.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Playing-with-CPP--VTABLE-from-Nim.md b/Playing-with-CPP--VTABLE-from-Nim.md index d74b7fa..33e4402 100644 --- a/Playing-with-CPP--VTABLE-from-Nim.md +++ b/Playing-with-CPP--VTABLE-from-Nim.md @@ -7,6 +7,7 @@ Although that external lexer we want to create must also be reside in the same d OK, so far no C++ feature we already met. But wait, that's not the real interface to Scintilla, it's only entry point for a more complicated interface. +### The C++ interface ```C++ class IDocument { public: @@ -49,6 +50,8 @@ public: }; ``` +### How Nim emulate C++ VTABLE + ILexer is the real interface to external lexer that we must provide from our dll. IDocument is the interface we need to communicate with Scintilla. Both of them are pure virtual functions classes. Nim not support C++ classes nor virtual functions, really? yes, no C++ equivalent class in Nim, no virtual functions in Nim. That's why this wiki is written, to overcome this kind problem. first, we will look at how Scintilla obtain pointer to ILexer instance from our dll. @@ -107,6 +110,8 @@ proc lexFactory(): ptr ILexer {.stdcall.} = See, VTABLE is only an array of pointers to functions, Nim can handle that easily. +### How to call pure virtual functions from Nim + Now, we already know how to provide Ilexer implementation from our dll, but what about IDocument? IDocument implementation is provided by Scintilla, how can we call IDocument member functions from Nim? Armed with knowledge that C++ virtual functions tables is no more than an array of pointers. we can do something like this: