Provides a default value if the expression is NULL, 0, or negative.
Provides a default value if the expression is NULL, 0, or negative.The valueOr macro is a convenient way to handle potentially unset or error values returned by libzdb functions. It works with pointers, integers, and floating-point types.
This macro evaluates the expression only once, making it safe to use with function calls or expressions that may have side effects.
For pointers:
- Returns the default value if the expression evaluates to NULL.
- Otherwise, returns the original pointer value.
For integers and floating-point types:
- Returns the default value if the expression evaluates to 0 or any negative value.
- Otherwise, returns the original numeric value.
- Parameters
-
expr | The expression to evaluate (typically a libzdb function call) |
default_value | The value to return if expr is NULL, 0, or negative |
- Returns
- If expr is not NULL, 0, or negative, returns expr. Otherwise, returns default_value.
- Note
- This macro uses a GNU C extension and is compatible with GCC and Clang. It may not work with other C compilers.
// Usage with string (pointer) return type const char* host = valueOr(URL_getHost(url), "localhost"); printf("Host: %s\n", host);
// Usage with integer return type int port = valueOr(ResultSet_getInt(r, 1), 5432); printf("Port: %d\n", port);
// Usage with floating-point return type double percent = valueOr(ResultSet_getDouble(r, 1), 1.0); printf("Percent: %.1f\n", percent);
#ifndef ZDB_INCLUDED
#define ZDB_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#define LIBZDB_MAJOR 3
#define LIBZDB_MINOR 4
#define LIBZDB_REVISION 0
#define LIBZDB_VERSION "3.4.0"
#define LIBZDB_VERSION_NUMBER ((LIBZDB_MAJOR * 1000000) + (LIBZDB_MINOR * 1000) + LIBZDB_REVISION)
#ifdef __cplusplus
}
#endif
#ifndef __cplusplus
#define valueOr(expr, default_value) \
({ \
__typeof__(expr) _t = (expr); \
(_t < 0 || _t == 0) ? (default_value) : _t; \
})
#endif
#endif
A ConnectionPool represents a database connection pool.
A Connection represents a connection to a SQL database system.
A PreparedStatement represents a single SQL statement pre-compiled into byte code for later execution...
A ResultSet represents a database result set.
Signals that an SQL specific exception has occurred.
URL represents an immutable Uniform Resource Locator.