-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This is a collection of thought on trying to implement fltk3 - again.
FLTK1 was written a long time ago to quickly implement a missing library. It's basically "C++ with classes". Modern C++, even if it is only C++11, can offer so much more type safety and early warnings to the user at compile time. I would love to upgrade FLTK1 to use a much better API, but there are so many changes, I find a new major version number very appropriate.
At the same time, I don't want to lose and existing users and make moving to fltk3 as painless as possible.
So here is the idea: move the existing FLTK1 code into a new fltk3 namespace, and then write a header-only shim that maps every FLTK1 code to the fltk3 implementation via an inline function or method. If that works, FLTK1 and fltk3 could be freely intermixed. Users can start with fltk3, or continue using FLTK1, or keep existing FLTK1 code and continue with fltk3 within the same app, and benefit from the new API without sacrificing any existing code.
The initial fltk3 API and implementation will simply replace all Fl references (Fl_, fl_, Fl::) with a new fltk3:: namespace and completely clean up the global namespace.
Major changes will include:
- get rid of any
const char*andchar*types where we meant to saystd::string, avoid doubling APIs and providing bothconst char*andstd::stringeverywhere for back compatibility - get rid of all
#definestatements, cludges, and trickery - use
constexprand enum classes wherever it makes sense (do you know the difference betweenFL_Caps_LockandFL_CAPS_LOCK? Wouldn't if be better if they were namedKey::Caps_LockandModifier::Caps_Lock?) - use classes even if we just represent integers, so they are not intermixed (do you know what
myWidget->color(16711680)does? MaybemyWidget->color(0xff0000)is better? But is that red or green? How aboutmyWidget->color(RGBColorI(0, 255, 0)? Oh, it's green!) - again, type safety:
if (Fl::event_key()==FL_RED)should generate an error at compile time! - be perfectly clear about ownership. Who owns the text in
const char* Fl_Widget::label()? Hint: it's notFl_Widget, at least not always.
This is the big premise her. Trust the compiler to optimize a ton of stuff away, and still get fast and lean code. The shim implementation looks complex, but it really is just a simple trampoline. Fltk3 implementations may look complex to achieve type safety, but if implemented right, all that will be optimized away, and the binary will be as lean as the original FLTK1 code, but with all the compile time checks to keep thing save, and run time check if we want them.