Couchbase C Client  2.10.7
Asynchronous C Client for Couchbase
User Cookies

Detailed Description

Associate user-defined data with operations.

User-defined pointers may be passed to all operations in the form of a cookie parameter. This cookie parameter allows any kind of application context to be accessible via the callback (in lcb_RESPBASE::cookie).

The library will not inspect or manage the address or contents of the cookie; it may live on the stack (especially if using the library synchronously), on the heap, or may be NULL altogether.

In addition to per-operation cookies, the library allows the instance itself (i.e. the lcb_t object) to contain its own cookie. This is helpful when there is a wrapper object which needs to be accessed from within the callback

Functions

void lcb_set_cookie (lcb_t instance, const void *cookie)
 Associate a cookie with an instance of lcb. More...
 
const void * lcb_get_cookie (lcb_t instance)
 Retrieve the cookie associated with this instance. More...
 

Function Documentation

◆ lcb_set_cookie()

void lcb_set_cookie ( lcb_t  instance,
const void *  cookie 
)

Associate a cookie with an instance of lcb.

The cookie is a user defined pointer which will remain attached to the specified lcb_t for its duration. This is the way to associate user data with the lcb_t.

Parameters
instancethe instance to associate the cookie to
cookiethe cookie to associate with this instance.
Attention
There is no destructor for the specified cookie stored with the instance; thus you must ensure to manually free resources to the pointer (if it was dynamically allocated) when it is no longer required.
Stability
Committed:
typedef struct {
const char *status;
// ....
} instance_info;
static void bootstrap_callback(lcb_t instance, lcb_error_t err) {
instance_info *info = (instance_info *)lcb_get_cookie(instance);
if (err == LCB_SUCCESS) {
info->status = "Connected";
} else {
info->status = "Error";
}
}
static void do_create(void) {
instance_info *info = calloc(1, sizeof(*info));
// info->status is currently NULL
// .. create the instance here
lcb_set_cookie(instance, info);
lcb_set_bootstrap_callback(instance, bootstrap_callback);
lcb_connect(instance);
lcb_wait(instance);
printf("Status of instance is %s\n", info->status);
}
Examples
example/libeventdirect/main.c.

◆ lcb_get_cookie()

const void* lcb_get_cookie ( lcb_t  instance)

Retrieve the cookie associated with this instance.

Parameters
instancethe instance of lcb
Returns
The cookie associated with this instance or NULL
See also
lcb_set_cookie()
Stability
Committed:
Examples
example/libeventdirect/main.c.
lcb_wait
lcb_error_t lcb_wait(lcb_t instance)
Wait for the execution of all batched requests.
lcb_connect
lcb_error_t lcb_connect(lcb_t instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
lcb_error_t
lcb_error_t
Error codes returned by the library.
Definition: error.h:476
lcb_set_bootstrap_callback
lcb_bootstrap_callback lcb_set_bootstrap_callback(lcb_t instance, lcb_bootstrap_callback callback)
Set the callback for notification of success or failure of initial connection.
lcb_t
struct lcb_st * lcb_t
Definition: couchbase.h:41
LCB_SUCCESS
@ LCB_SUCCESS
Success.
Definition: error.h:478