I believe I’m on record on this blog saying that I usually don’t see the point in writing wrappers for C libraries. That’s still very much true. I’ve seen a number of wrappers in C++, and a few in D, that objectify a C API without adding any beneficial functionality in the process. In my opinion, if you’re going to wrap something, there really ought to be a better reason than “objects”. In fact, wrappers need not have anything to do with object orientation at all.
Lately, I’ve been making use of the PhysicsFS library for a project I’m working on. Physfs is one of those libraries that I’ve known about for years, but never really felt the need to use. Now I can look back and wonder why I felt that way. It’s a very, very useful little library, for game developers especially (but not exclusively!). My new-found love of the library is why I created a D binding for it. Having used it in a handful of throwaway projects now, I’ve found myself wrapping up some of its functionality so that I don’t have to deal with it in user code. That eventually led me to this announcement.
Defile is a small, single-module library that wraps PhysicsFS in such a way as to be useful beyond “objects”. It contains a single type, Defile, which serves both as a wrapper for a PHYSFS_File handle and as a namespace for functions that act on global state. In other words, functions which manipulate global state (such as PHYSFS_setWriteDir) are wrapped as public static methods of the Defile struct, whereas functions that manipulate individual files are normal instance methods.
Most method names map directly to physfs function names, but a few do not. In the cases where the physfs function returns an error code, errors are converted into DefileExceptions. Other methods cache results or convert D args to C args. I’ve added a few convenience functions as well, which wrap up multiple physfs function calls to carry out operations I found myself doing more than once. And a few of the methods are implemented as properties.
This is not a complete wrapper at the moment. A number of functions are missing. I will get around to adding them eventually, mostly as I find I need them myself. I would like to point out that I put this together first and foremost for me, but I figured others would find it useful. So I split it out of the main project into a separate package, tossed it on to github and dubbed it up (it has a dependency on derelict-physfs from DerelictOrg). I actually did add ddoc comments, which is rather unusual for me. If you need any help with it, I’ll be happy to do what I can when I can. But please bear in mind that as far as priorities go, this one is rather toward the bottom.