Updated Playing with CPP VTABLE from Nim (markdown)

This commit is contained in:
andri lim 2016-01-30 11:25:45 +07:00
parent c057492929
commit 9dfc7bfe5c
1 changed files with 5 additions and 0 deletions

View File

@ -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: