Many questions

Denis Koroskin 2korden at gmail.com
Tue May 5 23:53:04 PDT 2009


On Wed, 06 May 2009 02:36:21 +0400, Christopher Wright <dhasenan at gmail.com> wrote:

> I see no use case for having one module in multiple files, though -- the  
> only benefit would be private access to things defined in other files.  
> I've never been big on making stuff private, though.

There is a use-case. Imagine you want to define a Thread class. It has a common interface, but its implementation is heavily platform-specific, and sometimes involves manual assembly.

Putting all the implementation into one file would make it very big, source code a complete mess and following it would be very hard. Instead, you may wish to define its interface in Thread.d(i), yet put its implementation in multiple files: Thread_win32.d, Thread_posix.d etc.

I use the same trick to unify socket API. For some reason, every platform introduce small API changes. Some call a function closesocket, others - socketclose etc. That's why I have SocketApi.di which contains the following declarations:

    SocketDescriptor socketCreate(SocketAddressFamily addressFamily, SocketType type, SocketProtocol protocol);
    SocketDescriptor socketAccept(SocketDescriptor listenSd);
    bool socketClose(SocketDescriptor sd);

    bool socketSetupListen(SocketDescriptor sd, uint maxQueue = 0);
    bool socketSetupNonBlocking(SocketDescriptor sd, bool nonBlocking);
    ....

Each platform implements these functions a bit differently. Their implementation is stored in SocketApi_win32.d, SocketApi_linux.d etc. And, yes, all of them have "module SocketApi;" on top of the file.




More information about the Digitalmars-d mailing list