b7cc127b158335e91c0e0a054b9844dbb61e4f22
[m6w6/libmemcached] / src / libmemcached / callback.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Change any of the possible callbacks.
9 *
10 */
11
12 #include "libmemcached/common.h"
13 #include <sys/types.h>
14
15 #ifndef __INTEL_COMPILER
16 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
17 #endif
18
19 /*
20 These functions provide data and function callback support
21 */
22
23 memcached_return_t memcached_callback_set(memcached_st *shell,
24 const memcached_callback_t flag,
25 const void *data)
26 {
27 Memcached* ptr= memcached2Memcached(shell);
28 if (ptr)
29 {
30 switch (flag)
31 {
32 case MEMCACHED_CALLBACK_PREFIX_KEY:
33 {
34 return memcached_set_namespace(*ptr, (char*)data, data ? strlen((char*)data) : 0);
35 }
36
37 case MEMCACHED_CALLBACK_USER_DATA:
38 {
39 ptr->user_data= const_cast<void *>(data);
40 break;
41 }
42
43 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
44 {
45 memcached_cleanup_fn func= *(memcached_cleanup_fn *)&data;
46 ptr->on_cleanup= func;
47 break;
48 }
49
50 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
51 {
52 memcached_clone_fn func= *(memcached_clone_fn *)&data;
53 ptr->on_clone= func;
54 break;
55 }
56
57 case MEMCACHED_CALLBACK_GET_FAILURE:
58 {
59 memcached_trigger_key_fn func= *(memcached_trigger_key_fn *)&data;
60 ptr->get_key_failure= func;
61 break;
62 }
63
64 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
65 {
66 if (data) // NULL would mean we are disabling.
67 {
68 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS))
69 {
70 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
71 }
72
73 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_NOREPLY))
74 {
75 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
76 }
77 }
78
79 memcached_trigger_delete_key_fn func= *(memcached_trigger_delete_key_fn *)&data;
80 ptr->delete_trigger= func;
81 break;
82 }
83
84 case MEMCACHED_CALLBACK_MAX:
85 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid callback supplied"));
86 }
87
88 return MEMCACHED_SUCCESS;
89 }
90
91 return MEMCACHED_INVALID_ARGUMENTS;
92 }
93
94 void *memcached_callback_get(memcached_st *shell,
95 const memcached_callback_t flag,
96 memcached_return_t *error)
97 {
98 Memcached* ptr= memcached2Memcached(shell);
99 memcached_return_t local_error;
100 if (error == NULL)
101 {
102 error = &local_error;
103 }
104
105 if (ptr == NULL)
106 {
107 *error= MEMCACHED_INVALID_ARGUMENTS;
108 return NULL;
109 }
110
111 switch (flag)
112 {
113 case MEMCACHED_CALLBACK_PREFIX_KEY:
114 {
115 *error= MEMCACHED_SUCCESS;
116 if (ptr->_namespace)
117 {
118 return (void *)memcached_array_string(ptr->_namespace);
119 }
120 return NULL;
121 }
122
123 case MEMCACHED_CALLBACK_USER_DATA:
124 {
125 *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
126 return (void *)ptr->user_data;
127 }
128
129 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
130 {
131 *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
132 return *(void **)&ptr->on_cleanup;
133 }
134
135 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
136 {
137 *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
138 return *(void **)&ptr->on_clone;
139 }
140
141 case MEMCACHED_CALLBACK_GET_FAILURE:
142 {
143 *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
144 return *(void **)&ptr->get_key_failure;
145 }
146
147 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
148 {
149 *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
150 return *(void **)&ptr->delete_trigger;
151 }
152
153 case MEMCACHED_CALLBACK_MAX:
154 break;
155 }
156
157 assert_msg(0, "Invalid callback passed to memcached_callback_get()");
158 *error= MEMCACHED_FAILURE;
159 return NULL;
160 }