1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-19 07:21:07 +01:00
nix/src/libutil-c/nix_api_util_internal.h
John Ericson bd42092873
Use less c_str() in the evaluator, and other cleanups
It is better to avoid null termination for performance and memory
safety, wherever possible.

These are good cleanups extracted from the Pascal String work that we
can land by themselves first, shrinking the diff in that PR.

Co-Authored-By: Aspen Smith <root@gws.fyi>
Co-Authored-By: Sergei Zimmerman <sergei@zimmerman.foo>
2025-11-03 15:14:50 +03:00

54 lines
1.6 KiB
C++

#ifndef NIX_API_UTIL_INTERNAL_H
#define NIX_API_UTIL_INTERNAL_H
#include <string>
#include <optional>
#include "nix/util/error.hh"
#include "nix_api_util.h"
extern "C" {
struct nix_c_context
{
nix_err last_err_code = NIX_OK;
/** The last error message. Always check last_err_code. This may not have been cleared, so that clearing is fast. */
std::optional<std::string> last_err = {};
std::optional<nix::ErrorInfo> info = {};
std::string name = "";
};
nix_err nix_context_error(nix_c_context * context);
/**
* Internal use only.
*
* Helper to invoke nix_get_string_callback
* @param context optional, the context to store errors in if this function
* fails
* @param str The string to observe
* @param callback Called with the observed string.
* @param user_data optional, arbitrary data, passed to the callback when it's called.
* @return NIX_OK if there were no errors.
* @see nix_get_string_callback
*/
nix_err call_nix_get_string_callback(const std::string_view str, nix_get_string_callback callback, void * user_data);
#define NIXC_CATCH_ERRS \
catch (...) \
{ \
return nix_context_error(context); \
} \
return NIX_OK;
#define NIXC_CATCH_ERRS_RES(def) \
catch (...) \
{ \
nix_context_error(context); \
return def; \
}
#define NIXC_CATCH_ERRS_NULL NIXC_CATCH_ERRS_RES(nullptr)
} // extern "C"
#endif // NIX_API_UTIL_INTERNAL_H