Add new method which allows someone to "take" a value from a result object.
[m6w6/libmemcached] / 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 *ptr,
24 const memcached_callback_t flag,
25 const void *data)
26 {
27 switch (flag)
28 {
29 case MEMCACHED_CALLBACK_PREFIX_KEY:
30 {
31 return memcached_set_namespace(ptr, (char*)data, data ? strlen((char*)data) : 0);
32 }
33
34 case MEMCACHED_CALLBACK_USER_DATA:
35 {
36 ptr->user_data= const_cast<void *>(data);
37 break;
38 }
39
40 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
41 {
42 memcached_cleanup_fn func= *(memcached_cleanup_fn *)&data;
43 ptr->on_cleanup= func;
44 break;
45 }
46
47 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
48 {
49 memcached_clone_fn func= *(memcached_clone_fn *)&data;
50 ptr->on_clone= func;
51 break;
52 }
53
54 case MEMCACHED_CALLBACK_GET_FAILURE:
55 {
56 memcached_trigger_key_fn func= *(memcached_trigger_key_fn *)&data;
57 ptr->get_key_failure= func;
58 break;
59 }
60
61 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
62 {
63 if (data) // NULL would mean we are disabling.
64 {
65 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS))
66 {
67 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
68 }
69
70 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_NOREPLY))
71 {
72 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
73 }
74 }
75
76 memcached_trigger_delete_key_fn func= *(memcached_trigger_delete_key_fn *)&data;
77 ptr->delete_trigger= func;
78 break;
79 }
80
81 case MEMCACHED_CALLBACK_MAX:
82 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid callback supplied"));
83 }
84
85 return MEMCACHED_SUCCESS;
86 }
87
88 void *memcached_callback_get(memcached_st *ptr,
89 const memcached_callback_t flag,
90 memcached_return_t *error)
91 {
92 memcached_return_t local_error;
93 if (error == NULL)
94 {
95 error = &local_error;
96 }
97
98 if (ptr == NULL)
99 {
100 *error= MEMCACHED_INVALID_ARGUMENTS;
101 return NULL;
102 }
103
104 switch (flag)
105 {
106 case MEMCACHED_CALLBACK_PREFIX_KEY:
107 {
108 *error= MEMCACHED_SUCCESS;
109 if (ptr->_namespace)
110 {
111 return (void *)memcached_array_string(ptr->_namespace);
112 }
113 return NULL;
114 }
115
116 case MEMCACHED_CALLBACK_USER_DATA:
117 {
118 *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
119 return (void *)ptr->user_data;
120 }
121
122 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
123 {
124 *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
125 return *(void **)&ptr->on_cleanup;
126 }
127
128 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
129 {
130 *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
131 return *(void **)&ptr->on_clone;
132 }
133
134 case MEMCACHED_CALLBACK_GET_FAILURE:
135 {
136 *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
137 return *(void **)&ptr->get_key_failure;
138 }
139
140 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
141 {
142 *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
143 return *(void **)&ptr->delete_trigger;
144 }
145
146 case MEMCACHED_CALLBACK_MAX:
147 break;
148 }
149
150 assert_msg(0, "Invalid behavior passed to memcached_behavior_set()");
151 *error= MEMCACHED_FAILURE;
152 return NULL;
153 }