René Scharfe <l....@web.de> writes:

> Apart from that the macro is simple and doesn't use any tricks or
> added checks.  It just sets up boilerplate functions to offer type-safe
> sorting.
> ...
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 5f2e90932f..491230fc57 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -1066,6 +1066,21 @@ static inline void sane_qsort(void *base, size_t 
> nmemb, size_t size,
>               qsort(base, nmemb, size, compar);
>  }
>  
> +#define DECLARE_SORT(scope, name, elemtype) \
> +scope void name(elemtype, size_t)
> +
> +#define DEFINE_SORT(scope, name, elemtype, one, two)                 \
> +static int name##_compare(const elemtype, const elemtype);           \
> +static int name##_compare_void(const void *a, const void *b)         \
> +{                                                                    \
> +     return name##_compare(a, b);                                    \
> +}                                                                    \
> +scope void name(elemtype base, size_t nmemb)                         \
> +{                                                                    \
> +     QSORT(base, nmemb, name##_compare_void);                        \
> +}                                                                    \
> +static int name##_compare(const elemtype one, const elemtype two)

... and here comes the body of the comparison function that takes
two "things" we are about, i.e. elements of the array being sorted.

Quite cleanly done and the result looks pleasant, at least to me.

Reply via email to