The next task in my proposal for GSOC was integrating an Editor into OpenSCAD. This integration of Editor is necessary, as it was observed that many users prefer external editors like notepad++ etc over legacy editor of OpenSCAD. These editors facilitates them with functionality like auto code completion, brace completion, syntax highlighting etc.
A discussion with community over various editors was required to chose the most appropriate editor. Editors API like Qodedit, QEmacs, QScintialla, Atom.io came into the scene and finally QScintilla is chosen. This decision was taken after measuring the functionality of QScintilla editor API and feasibility of integration. During the discussion, some members and specially my mentors suggested to have both the editors retained into the application. They didn’t want to drop their legacy editor completely. A compile time option will be provided to the user to chose one of them.
Such a marvelous idea leads to a need of an editor interface into the code so as to chose one of them. A long discussion on IRC has been done between me and my mentors making different logic to make this editor. Finally after spending so much time, trying different methods and removing various confusions, what logic made is:
There will be three classes
- 1. Editor as base class
- 2. LegacyEditor as derived class of Editor
- 3. ScintillaEditor as derived class of Editor
The base class Editor will have all functions defined as virtual and all those functions are overridden in derived classes. These virtual functions will act as interfaces which can be called abstracting the detail definition of functions in derived class. For example,
virtual void indentSelection() { }
is a function of interface class Editor and this function is called using its object like
editor_object->indentSelection();
.
However, this function doesn’t include any implementation code because it has to be overridden in LegacyEditor class and Scintilla class (derived classes). These functions defined in derived class include respective code which can solve the purpose of indentation. (I hope, I made it clear, still if you have any kind of confusion, feel free to ask in comments)
Next step is to understand the concept of virtual functions. It includes a very strong concept of C++ i.e. POLYMORPHISM. Polymorphism (many forms) includes the definition of functions having same name and parameter in different class which can be called dynamically using base class object also known as Dynamic Polymorphism or Run Time Polymorphism. To make it more clear, let me explain an example program
class Editor { public: virtual void indentSelection(){ } }; class LegacyEditor : public Editor { public: void indentSelection(){ cout << "Derived class function overridden 1"; } }; class Scintilla : public Editor { public: void indentSelection(){ cout << "Derived class function overridden 2"; } };
In the main function, base class reference will be used to call the respective functions of derive class
int main (){ Editor *editor_object; LegacyEditor *legacy = new LegacyEditor; Scintilla *scintilla = new Scintilla; editor_object = legacy; editor_object->indentSelection(); //call LegacyEditor's function editor_object = scintilla; editor_object->indentSelection(); //call Scintialla's function }
If the function in the base class is not defined with keyword ‘virtual’, it will never be able to know the implementation of derive class function.