C - Exposing 3rd party data structures in a library's public header file

I'm writing a library that wraps around a REST API and representing some of the data requires use of data structures like hash maps that are not available in the C standard library so internally I'm using 3rd party libraries for their implementations. Now I'd like to expose them in a response struct, I've come up with a couple of solutions:

  1. Just expose the type as it is and add a dependency on the 3rd party lib and make the user call the appropriate API functions (Eg hashmap_lib_get(users, "key") to get a value):
// header.h (Include guards excluded)
#include <hashmap_lib.h>

struct api_response {
    int field1;
    char *field2;
    HASHMAP_LIB_HM *users; // Map of users to their email. (example)
};

Issue: Requires extra user intervention, even more problematic in case the library is implemented using generic macros.

  1. Expose the type as a void pointer inside the struct and write duplicated wrappers for all appropriate functions:
// header.h
struct api_response {
    int field1;
    char *field2;
    void *users; // Don't touch
};

char *api_hashmap_get(void *hm, const char *key);

The internal implementation of api_hashmap_get would just be return hashmap_lib_get((HASHMAP_LIB_HM *) hm, key);

Issue: Requires duplication of various function definitions and requires the use of void pointers.

How is this problem usually solved in libraries ? The first solution would make sense if both the libraries were "related" with each other (Eg using an eventing system such as libevent to work with the user's application), but in this case it's just about general data structures so it doesn't make sense to me since the user would be using other libs for the same type of data structures aswell.



from Recent Questions - Stack Overflow https://ift.tt/3ouF1bJ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation