First, I hate it when people talk about C++ & STL like they’re two
seperate things. Large tracts of the STL are incorporated into the C++
language definition. It’s just the C++ standard library, okay?
Okay, now that I got THAT rant of my chest…
I did a research project on API design, based around SDL. I spent some
time considering the implementation of what you propose, some
thoughts:
Many C-oriented constructs used within SDL can be tricky to
translate directly into C++ constructs in a way that maintains
compatibility - Exceptions are the best example of this; how do
properly support exceptions, while still maintaining the integrity of
C code? Think of the scenario where someone is porting from C to C++
and wants to leave large chunks of code as C; this happens in the real
world. This is particularly important, as exceptions are about the
only reason to actually write a C++ wrapper for SDL - If you’re not
going to use exceptions, then you may as well use the ‘naked’ C calls,
as you’ll have to do C-style error checking anyway…
Wrapping structs in classes exposes some weaknesses in the SDL APIs.
For example, SDL_UnionRect doesn’t translate well into
sdl::Rectangle::union(), for obvious reasons, but actually, more
importantly, it isn’t a union operation; SDL_UnionRect returns an
SDL_Rect that is large enough to enclose both the rectangles passed
in. So, you have 2 weaknesses; a name that is a reserved word in
another language and an API that doesn’t describe acurately what it
does.
Automatic memory management should be used with extreme care. This
is particularly difficult when dealing with C comptibility; what
happens when someone makes a C call to free memory? This makes things
VERY tricky. I would argue that it is not merely sufficient to say
"well if they do that, it’s their own fault" - “A library should be
easy to use and difficult to misuse” - one has to find a way to
enforce behaviour to prevent mixed code from failing. How do you do
that without cripplling the performance?
The conclusion I drew from this, is that, actually, the “best” way
to design a C library, is to design it alongside the bindings for
atleast 2 other languages (rule-of-three). In the case of SDL, the
obvious choices are C++ (real-world) and Python (PyGame being as well
know as it is). It’s not so much about making it work perfectly with
every other language, it’s about thinking about the problem in
different ways to allow you to spot problems you may have otherwise
missed.
I know my tone may seem a little negative. It’s not meant to be; I’m
simply being realistic
Eddy
2009/4/2 Kornel Kisielewicz <kornel.kisielewicz at gmail.com>:> 2009/4/2 Bob Pendleton :
In the past I have rather strongly opposed putting this kind of thing in
SDL for the simple reason that C is easier to embed in more other languages
than is C++. And, right now I still oppose putting it in SDL.
I do not suggest putting it in the main distro, but as a
side-download for people wanting a workable thought out and complete
C++ interface.
OTOH, the proposal as I have been reading it is very interesting. I would
like to see it done just to see how the process worked out and to see how
usable the result was. The design would have to be carried out in the full
light of the mailing list and would require careful monitoring to damp out
the inevitable flames.
This is something that I already have forseen. Before any coding would
start, a preliminary report would be presented to the list. This
report would list the major design decisions, and would include
interfaces for the major classes. Even after coding starts, there’d be
regular status reports – in the case of such a project I think that
the mailing list would be a far better target for such reports than a
blog.
OTTH, I’m believe this could be a very long process. It is unlikely to be
complete by the time GSOC 2009 ends. The usual first approach to this
problem (as I have seen several times) tends to wind up ignoring a huge part
of what you can actually do with SDL.
As long as the unimplemented features would be marked in a TODO list,
and as long as all the basic interfaces would be there, it would
already be a good foundation easy to expand. Also, I quite believe
that it is doable, as long as not too many design flame wars break
out on the list :>. A good idea is to start a design discussion on the
next feature while starting implementing one that has been agreed
upon.
Not to mention that 1.3 may still
contain instabilities that could result in work stoppages in parts of the
project.
I am ready to cope with that. Note that implementing a full-blown C++
API would be a great way to fully coverage-test 1.3.
stabilize during the first few iterations. The person proposing the work
needs to understand that, and the GSOC people need to understand that
successful completion of the project does not imply that the code will be
complete at the end of the sumer.
Agreed.
OTFH, The focus on STL worries me. It may well be that STL approaches are
not applicable.
I’m thinking more STL-aware, than STL-focused. I’ve seen horrible
abuses of STL, and I think I can fend those off ;-).
Ok, I’ve gone through the other hand, the third hand, and the fourth hand…
so maybe I should stop.
Why stop, all comments are really helpful!
–
regards,
Kornel Kisielewicz
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org