C API: Use nix_get_string_callback typedef

This commit is contained in:
José Luis Lafuente 2024-04-15 12:05:57 +02:00
parent 76444a3958
commit 774e7213e8
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
6 changed files with 19 additions and 62 deletions

View file

@ -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)

View file

@ -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

View file

@ -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;
}

View file

@ -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

View file

@ -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 (...) \

View file

@ -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)
}