Updating for 1.0.2 release
[awesomized/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
94 if (error == NULL)
95 {
96 error = &local_error;
97 }
98
99 if (ptr == NULL)
100 {
101 *error= MEMCACHED_INVALID_ARGUMENTS;
102 return NULL;
103 }
104
105 switch (flag)
106 {
107 case MEMCACHED_CALLBACK_PREFIX_KEY:
108 {
109 *error= MEMCACHED_SUCCESS;
110 if (ptr->_namespace)
111 {
112 return (void *)memcached_array_string(ptr->_namespace);
113 }
114 return NULL;
115 }
116
117 case MEMCACHED_CALLBACK_USER_DATA:
118 {
119 *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
120 return (void *)ptr->user_data;
121 }
122
123 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
124 {
125 *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
126 return *(void **)&ptr->on_cleanup;
127 }
128
129 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
130 {
131 *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
132 return *(void **)&ptr->on_clone;
133 }
134
135 case MEMCACHED_CALLBACK_GET_FAILURE:
136 {
137 *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
138 return *(void **)&ptr->get_key_failure;
139 }
140
141 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
142 {
143 *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
144 return *(void **)&ptr->delete_trigger;
145 }
146
147 case MEMCACHED_CALLBACK_MAX:
148 break;
149 }
150
151 assert_msg(0, "Invalid behavior passed to memcached_behavior_set()");
152 *error= MEMCACHED_FAILURE;
153 return NULL;
154 }