cpack: fix packaging directory for CMake <=3.11
[awesomized/libmemcached] / src / libmemcached / callback.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17 #include <sys/types.h>
18
19 /*
20 These functions provide data and function callback support
21 */
22
23 memcached_return_t memcached_callback_set(memcached_st *shell, const memcached_callback_t flag,
24 const void *data) {
25 Memcached *ptr = memcached2Memcached(shell);
26 if (ptr) {
27 switch (flag) {
28 case MEMCACHED_CALLBACK_PREFIX_KEY: {
29 return memcached_set_namespace(*ptr, (char *) data, data ? strlen((char *) data) : 0);
30 }
31
32 case MEMCACHED_CALLBACK_USER_DATA: {
33 ptr->user_data = const_cast<void *>(data);
34 break;
35 }
36
37 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: {
38 memcached_cleanup_fn func = *(memcached_cleanup_fn *) &data;
39 ptr->on_cleanup = func;
40 break;
41 }
42
43 case MEMCACHED_CALLBACK_CLONE_FUNCTION: {
44 memcached_clone_fn func = *(memcached_clone_fn *) &data;
45 ptr->on_clone = func;
46 break;
47 }
48
49 case MEMCACHED_CALLBACK_GET_FAILURE: {
50 memcached_trigger_key_fn func = *(memcached_trigger_key_fn *) &data;
51 ptr->get_key_failure = func;
52 break;
53 }
54
55 case MEMCACHED_CALLBACK_DELETE_TRIGGER: {
56 if (data) // NULL would mean we are disabling.
57 {
58 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS)) {
59 return memcached_set_error(
60 *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
61 memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
62 }
63
64 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_NOREPLY)) {
65 return memcached_set_error(
66 *ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
67 memcached_literal_param(
68 "Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
69 }
70 }
71
72 memcached_trigger_delete_key_fn func = *(memcached_trigger_delete_key_fn *) &data;
73 ptr->delete_trigger = func;
74 break;
75 }
76
77 case MEMCACHED_CALLBACK_MAX:
78 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
79 memcached_literal_param("Invalid callback supplied"));
80 }
81
82 return MEMCACHED_SUCCESS;
83 }
84
85 return MEMCACHED_INVALID_ARGUMENTS;
86 }
87
88 void *memcached_callback_get(memcached_st *shell, const memcached_callback_t flag,
89 memcached_return_t *error) {
90 Memcached *ptr = memcached2Memcached(shell);
91 memcached_return_t local_error;
92 if (error == NULL) {
93 error = &local_error;
94 }
95
96 if (ptr == NULL) {
97 *error = MEMCACHED_INVALID_ARGUMENTS;
98 return NULL;
99 }
100
101 switch (flag) {
102 case MEMCACHED_CALLBACK_PREFIX_KEY: {
103 *error = MEMCACHED_SUCCESS;
104 if (ptr->_namespace) {
105 return (void *) memcached_array_string(ptr->_namespace);
106 }
107 return NULL;
108 }
109
110 case MEMCACHED_CALLBACK_USER_DATA: {
111 *error = ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
112 return (void *) ptr->user_data;
113 }
114
115 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: {
116 *error = ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
117 return *(void **) &ptr->on_cleanup;
118 }
119
120 case MEMCACHED_CALLBACK_CLONE_FUNCTION: {
121 *error = ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
122 return *(void **) &ptr->on_clone;
123 }
124
125 case MEMCACHED_CALLBACK_GET_FAILURE: {
126 *error = ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
127 return *(void **) &ptr->get_key_failure;
128 }
129
130 case MEMCACHED_CALLBACK_DELETE_TRIGGER: {
131 *error = ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
132 return *(void **) &ptr->delete_trigger;
133 }
134
135 case MEMCACHED_CALLBACK_MAX:
136 break;
137 }
138
139 assert_msg(0, "Invalid callback passed to memcached_callback_get()");
140 *error = MEMCACHED_FAILURE;
141 return NULL;
142 }