Development concepts for libtio

As a follow-up for libtio concepts in the user guide, here’s a few concepts and decisions that have been taken for the development.

File organization

There are many, many files in libtio, and that’s because I try to keep the project clean and organized. This file is an attempt at describing how the files are organized and where you can find or should put anything here.

There are a few main files and folders:

docs/

The library documentation, written using Sphinx.

include/

The public headers to use with the library (not including internal headers).

src/

The sources and internal headers.

tools/, configure, Makefile, Makefile.vars, Makefile.msg

Build utilities.

README.md, CONTRIBUTING.md, LICENSE.md

Basic to-read files.

In the include/ folder, there is the main header the user is supposed to include, libtio.h, and a subfolder, which contains all of the headers specific to modules. It is organized as in src/, in modules which represent the abstraction the module defines. For example, src/stream/ and include/libtio/stream.h are related to the libtio-specific stream which defines platform-agnostic functions to the system-specific utilities.

In the source folder, the internals.h header is the general internal header which is included in every source file in the project (sometimes using a module or submodule specific header which defines some more specific things afterwards). It contains general utilities such as reliable endian management macros, reliable integer types, and so on.

Function declarations and definitions

For portability (e.g. call conventions and other compiler-specific mess), libtio uses a few macros for declaring and defining functions that you are expected to use if you ought to add some stuff:

OF(ARGS)

This is a macro originally from Zlib. It is used on function declarations for compatibility with K&R (pre-ANSI) C, which didn’t support arguments definition for them. Without the macro, you might have done:

#if defined(__STDC__) && __STDC__
int my_function(int arg, char const *carg);
#else
int my_function(); /* K&R */
#endif

Instead, with this macro, you can just do:

int my_function
        OF((int arg, char const *carg));
TIO_EXTERN(TYPE)

Declare or define a function exported or ought to be. For example, with a declaration and a definition:

TIO_EXTERN(int) my_function
        OF((int arg1, int arg2));

TIO_EXTERN(int) my_function(int arg1, int arg2)
{
        return (arg1 + arg2);
}

Which can be resolved as one of the following (not exhaustive):

/* Within normal circumstances. */
extern int my_function(int arg1, int arg2);

/* Oh, we're on good ol' Windows! Let's set a call convention
 * explicitely so we don't run into problems. */
extern int WINAPI my_function(int arg1, int arg2);
TIO_NORETURN()

Use instead of TIO_EXTERN() for functions that are not supposed to return, e.g. if they exit() or abort(). For example:

TIO_NORETURN panic_and_set_fire_to_the_computer
        OF((char const *msg));
TIO_LOCAL(TYPE)

Use for defining a function internal to the file. For example, with a declaration and a definition:

TIO_LOCAL(int) my_function(int arg1, int arg2);

TIO_LOCAL(int) my_function(int arg1, int arg2)
{
        return (arg1 - arg2);
}

Which can be resolved as one of the following (not exhaustive):

/* Within normal circumstances. */
static int my_function(int arg1, int arg2);
TIO_HOOK(TYPE)

Equivalent of TIO_LOCAL() for functions that ought to be used as hooks, i.e. callbacks for libtio.

TIO_HOOK_TYPE(TYPE)

Extern function as a type, for using hook functions as callbacks within typedef or other type definitions. For example:

typedef TIO_EXTERN_TYPE(int) my_function_t
        OF((int arg1, int arg2));
TIO_EXTERN_TYPE(TYPE)

Equivalent of TIO_HOOK_TYPE() for exported functions to be used and stored as hooks.

TIO_DEPRECATED()

Prefix for function declarations and definitions which will be marked as deprecated for the compiler (if it supports it). For example:

TIO_DEPRECATED TIO_EXTERN(char const *) do_not_use_this_function
        OF((char const *s));

TIO_DEPRECATED TIO_EXTERN(char const *) do_not_use_this_function(char const *s)
{
        return (s + strlen(s));
}