From 774e7213e85447e159b93437f5f269a2f14621ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Mon, 15 Apr 2024 12:05:57 +0200 Subject: [PATCH] C API: Use `nix_get_string_callback` typedef --- src/libstore-c/nix_api_store.cc | 20 +++++------------- src/libstore-c/nix_api_store.h | 18 ++++------------ src/libutil-c/nix_api_util.cc | 21 +++++-------------- src/libutil-c/nix_api_util.h | 16 +++----------- src/libutil-c/nix_api_util_internal.h | 3 +-- .../libutil-support/tests/string_callback.hh | 3 +-- 6 files changed, 19 insertions(+), 62 deletions(-) diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index aa4ab521a..6ce4d01bb 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -56,11 +56,7 @@ void nix_store_free(Store * store) delete store; } -nix_err nix_store_get_uri( - nix_c_context * context, - Store * store, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data) +nix_err nix_store_get_uri(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data) { if (context) context->last_err_code = NIX_OK; @@ -71,11 +67,8 @@ nix_err nix_store_get_uri( NIXC_CATCH_ERRS } -nix_err nix_store_get_version( - nix_c_context * context, - Store * store, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data) +nix_err +nix_store_get_version(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data) { if (context) context->last_err_code = NIX_OK; @@ -136,13 +129,10 @@ nix_err nix_store_realise( NIXC_CATCH_ERRS } -void nix_store_path_name( - const StorePath * store_path, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data) +void nix_store_path_name(const StorePath * store_path, nix_get_string_callback callback, void * user_data) { std::string_view name = store_path->path.name(); - ((nix_get_string_callback) callback)(name.data(), name.size(), user_data); + callback(name.data(), name.size(), user_data); } void nix_store_path_free(StorePath * sp) diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index efeedbf7b..c83aca3f7 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -76,11 +76,7 @@ void nix_store_free(Store * store); * @see nix_get_string_callback * @return error code, NIX_OK on success. */ -nix_err nix_store_get_uri( - nix_c_context * context, - Store * store, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data); +nix_err nix_store_get_uri(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data); // returns: owned StorePath* /** @@ -101,10 +97,7 @@ StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const c * @param[in] callback called with the name * @param[in] user_data arbitrary data, passed to the callback when it's called. */ -void nix_store_path_name( - const StorePath * store_path, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data); +void nix_store_path_name(const StorePath * store_path, nix_get_string_callback callback, void * user_data); /** * @brief Copy a StorePath @@ -163,11 +156,8 @@ nix_err nix_store_realise( * @see nix_get_string_callback * @return error code, NIX_OK on success. */ -nix_err nix_store_get_version( - nix_c_context * context, - Store * store, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data); +nix_err +nix_store_get_version(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data); // cffi end #ifdef __cplusplus diff --git a/src/libutil-c/nix_api_util.cc b/src/libutil-c/nix_api_util.cc index 4999e28e9..0a9b49345 100644 --- a/src/libutil-c/nix_api_util.cc +++ b/src/libutil-c/nix_api_util.cc @@ -64,11 +64,7 @@ const char * nix_version_get() // Implementations -nix_err nix_setting_get( - nix_c_context * context, - const char * key, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data) +nix_err nix_setting_get(nix_c_context * context, const char * key, nix_get_string_callback callback, void * user_data) { if (context) context->last_err_code = NIX_OK; @@ -120,10 +116,7 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_con } nix_err nix_err_name( - nix_c_context * context, - const nix_c_context * read_context, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data) + nix_c_context * context, const nix_c_context * read_context, nix_get_string_callback callback, void * user_data) { if (context) context->last_err_code = NIX_OK; @@ -134,10 +127,7 @@ nix_err nix_err_name( } nix_err nix_err_info_msg( - nix_c_context * context, - const nix_c_context * read_context, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data) + nix_c_context * context, const nix_c_context * read_context, nix_get_string_callback callback, void * user_data) { if (context) context->last_err_code = NIX_OK; @@ -153,9 +143,8 @@ nix_err nix_err_code(const nix_c_context * read_context) } // internal -nix_err call_nix_get_string_callback( - const std::string str, void (*callback)(const char * start, unsigned int n, void * user_data), void * user_data) +nix_err call_nix_get_string_callback(const std::string str, nix_get_string_callback callback, void * user_data) { - ((nix_get_string_callback) callback)(str.c_str(), str.size(), user_data); + callback(str.c_str(), str.size(), user_data); return NIX_OK; } diff --git a/src/libutil-c/nix_api_util.h b/src/libutil-c/nix_api_util.h index 36a3f76cb..e0ca04e69 100644 --- a/src/libutil-c/nix_api_util.h +++ b/src/libutil-c/nix_api_util.h @@ -175,11 +175,7 @@ nix_err nix_libutil_init(nix_c_context * context); * @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was retrieved * successfully. */ -nix_err nix_setting_get( - nix_c_context * context, - const char * key, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data); +nix_err nix_setting_get(nix_c_context * context, const char * key, nix_get_string_callback callback, void * user_data); /** * @brief Sets a setting in the nix global configuration. @@ -246,10 +242,7 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, uns * @return NIX_OK if there were no errors, an error code otherwise. */ nix_err nix_err_info_msg( - nix_c_context * context, - const nix_c_context * read_context, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data); + nix_c_context * context, const nix_c_context * read_context, nix_get_string_callback callback, void * user_data); /** * @brief Retrieves the error name from a context. @@ -268,10 +261,7 @@ nix_err nix_err_info_msg( * @return NIX_OK if there were no errors, an error code otherwise. */ nix_err nix_err_name( - nix_c_context * context, - const nix_c_context * read_context, - void (*callback)(const char * start, unsigned int n, void * user_data), - void * user_data); + nix_c_context * context, const nix_c_context * read_context, nix_get_string_callback callback, void * user_data); /** * @brief Retrieves the most recent error code from a nix_c_context diff --git a/src/libutil-c/nix_api_util_internal.h b/src/libutil-c/nix_api_util_internal.h index fe5d5db18..aa829feaf 100644 --- a/src/libutil-c/nix_api_util_internal.h +++ b/src/libutil-c/nix_api_util_internal.h @@ -29,8 +29,7 @@ nix_err nix_context_error(nix_c_context * context); * @return NIX_OK if there were no errors. * @see nix_get_string_callback */ -nix_err call_nix_get_string_callback( - const std::string str, void (*callback)(const char * start, unsigned int n, void * user_data), void * user_data); +nix_err call_nix_get_string_callback(const std::string str, nix_get_string_callback callback, void * user_data); #define NIXC_CATCH_ERRS \ catch (...) \ diff --git a/tests/unit/libutil-support/tests/string_callback.hh b/tests/unit/libutil-support/tests/string_callback.hh index 6420810b6..3a3e545e9 100644 --- a/tests/unit/libutil-support/tests/string_callback.hh +++ b/tests/unit/libutil-support/tests/string_callback.hh @@ -11,7 +11,6 @@ inline void * observe_string_cb_data(std::string & out) }; #define OBSERVE_STRING(str) \ - (void (*)(const char *, unsigned int, void *)) nix::testing::observe_string_cb, \ - nix::testing::observe_string_cb_data(str) + (nix_get_string_callback) nix::testing::observe_string_cb, nix::testing::observe_string_cb_data(str) }