Merge in conversion to C++.
[m6w6/libmemcached] / libmemcached / result.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38
39 /*
40 memcached_result_st are used to internally represent the return values from
41 memcached. We use a structure so that long term as identifiers are added
42 to memcached we will be able to absorb new attributes without having
43 to addjust the entire API.
44 */
45 #include <libmemcached/common.h>
46
47 static inline void _result_init(memcached_result_st *self,
48 memcached_st *memc)
49 {
50 self->item_flags= 0;
51 self->item_expiration= 0;
52 self->key_length= 0;
53 self->item_cas= 0;
54 self->root= memc;
55 self->item_key[0]= 0;
56 }
57
58 memcached_result_st *memcached_result_create(const memcached_st *memc,
59 memcached_result_st *ptr)
60 {
61 WATCHPOINT_ASSERT(memc);
62
63 /* Saving malloc calls :) */
64 if (ptr)
65 {
66 ptr->options.is_allocated= false;
67 }
68 else
69 {
70 ptr= static_cast<memcached_result_st *>(libmemcached_malloc(memc, sizeof(memcached_result_st)));
71
72 if (ptr == NULL)
73 return NULL;
74
75 ptr->options.is_allocated= true;
76 }
77
78 ptr->options.is_initialized= true;
79
80 _result_init(ptr, (memcached_st *)memc);
81
82 WATCHPOINT_SET(ptr->value.options.is_initialized= false);
83 memcached_string_create(memc, &ptr->value, 0);
84 WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
85 WATCHPOINT_ASSERT(ptr->value.string == NULL);
86
87 return ptr;
88 }
89
90 void memcached_result_reset(memcached_result_st *ptr)
91 {
92 ptr->key_length= 0;
93 memcached_string_reset(&ptr->value);
94 ptr->item_flags= 0;
95 ptr->item_cas= 0;
96 ptr->item_expiration= 0;
97 }
98
99 void memcached_result_free(memcached_result_st *ptr)
100 {
101 if (ptr == NULL)
102 return;
103
104 memcached_string_free(&ptr->value);
105
106 if (memcached_is_allocated(ptr))
107 {
108 WATCHPOINT_ASSERT(ptr->root); // Without a root, that means that result was not properly initialized.
109 libmemcached_free(ptr->root, ptr);
110 }
111 else
112 {
113 ptr->options.is_initialized= false;
114 }
115 }
116
117 memcached_return_t memcached_result_set_value(memcached_result_st *ptr,
118 const char *value,
119 size_t length)
120 {
121 memcached_return_t rc= memcached_string_append(&ptr->value, value, length);
122
123 if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
124 {
125 memcached_set_errno(ptr->root, errno, NULL);
126 }
127
128 return rc;
129 }
130
131 const char *memcached_result_key_value(const memcached_result_st *self)
132 {
133 return self->key_length ? self->item_key : NULL;
134 }
135
136 size_t memcached_result_key_length(const memcached_result_st *self)
137 {
138 return self->key_length;
139 }
140
141 const char *memcached_result_value(const memcached_result_st *self)
142 {
143 const memcached_string_st *sptr= &self->value;
144 return memcached_string_value(sptr);
145 }
146
147 size_t memcached_result_length(const memcached_result_st *self)
148 {
149 const memcached_string_st *sptr= &self->value;
150 return memcached_string_length(sptr);
151 }
152
153 uint32_t memcached_result_flags(const memcached_result_st *self)
154 {
155 return self->item_flags;
156 }
157
158 uint64_t memcached_result_cas(const memcached_result_st *self)
159 {
160 return self->item_cas;
161 }
162
163 void memcached_result_set_flags(memcached_result_st *self, uint32_t flags)
164 {
165 self->item_flags= flags;
166 }
167
168 void memcached_result_set_expiration(memcached_result_st *self, time_t expiration)
169 {
170 self->item_expiration= expiration;
171 }