result.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * 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. #include "gear_config.h"
  38. #include "libgearman/result.hpp"
  39. #include "libgearman/assert.hpp"
  40. #include "libgearman-1.0/visibility.h"
  41. #include "libgearman-1.0/result.h"
  42. #include "libgearman/vector.h"
  43. #include <cstdlib>
  44. #include <limits>
  45. #include <memory>
  46. #include <libgearman/result.hpp>
  47. gearman_result_st::gearman_result_st() :
  48. _type(GEARMAN_RESULT_NULL)
  49. {
  50. value._boolean= false;
  51. }
  52. gearman_result_st::gearman_result_st(size_t reserve_size_) :
  53. _type(GEARMAN_RESULT_NULL),
  54. value(reserve_size_)
  55. {
  56. }
  57. bool gearman_result_is_null(const gearman_result_st *self)
  58. {
  59. if (self)
  60. {
  61. return self->is_null();
  62. }
  63. return true;
  64. }
  65. size_t gearman_result_st::size() const
  66. {
  67. switch (_type)
  68. {
  69. case GEARMAN_RESULT_BINARY:
  70. return value.string.size();
  71. case GEARMAN_RESULT_BOOLEAN:
  72. return 1;
  73. case GEARMAN_RESULT_INTEGER:
  74. return sizeof(int64_t);
  75. case GEARMAN_RESULT_NULL:
  76. return 0;
  77. }
  78. assert_msg(false, "Invalid result");
  79. return size_t(0) -1;
  80. }
  81. int64_t gearman_result_st::integer() const
  82. {
  83. switch (_type)
  84. {
  85. case GEARMAN_RESULT_BINARY:
  86. return atoll(value.string.value());
  87. case GEARMAN_RESULT_BOOLEAN:
  88. return value._boolean;
  89. case GEARMAN_RESULT_INTEGER:
  90. return value._integer;
  91. case GEARMAN_RESULT_NULL:
  92. return 0;
  93. }
  94. assert_msg(false, "Invalid result");
  95. return int64_t(0) -1;
  96. }
  97. int64_t gearman_result_integer(const gearman_result_st *self)
  98. {
  99. if (self)
  100. {
  101. return self->integer();
  102. }
  103. return 0;
  104. }
  105. bool gearman_result_st::boolean() const
  106. {
  107. switch (_type)
  108. {
  109. case GEARMAN_RESULT_BINARY:
  110. return value.string.size();
  111. case GEARMAN_RESULT_BOOLEAN:
  112. return value._boolean;
  113. case GEARMAN_RESULT_INTEGER:
  114. return value._integer ? true : false;
  115. case GEARMAN_RESULT_NULL:
  116. return false;
  117. }
  118. assert_msg(false, "Invalid result");
  119. return false;
  120. }
  121. bool gearman_result_boolean(const gearman_result_st *self)
  122. {
  123. if (self)
  124. {
  125. return self->boolean();
  126. }
  127. return false;
  128. }
  129. size_t gearman_result_size(const gearman_result_st *self)
  130. {
  131. if (self and self->_type == GEARMAN_RESULT_BINARY)
  132. {
  133. return gearman_string_length(&self->value.string);
  134. }
  135. return 0;
  136. }
  137. const char *gearman_result_value(const gearman_result_st *self)
  138. {
  139. if (self and self->_type == GEARMAN_RESULT_BINARY)
  140. {
  141. gearman_string_t ret= gearman_string(&self->value.string);
  142. return gearman_c_str(ret);
  143. }
  144. return NULL;
  145. }
  146. gearman_string_t gearman_result_string(const gearman_result_st *self)
  147. {
  148. if (not self or self->_type != GEARMAN_RESULT_BINARY)
  149. {
  150. gearman_string_t ret= {0, 0};
  151. return ret;
  152. }
  153. return gearman_string(&self->value.string);
  154. }
  155. gearman_string_t gearman_result_st::take()
  156. {
  157. if (_type == GEARMAN_RESULT_BINARY and size())
  158. {
  159. gearman_string_t ret_string= value.string.take();
  160. clear();
  161. return ret_string;
  162. }
  163. static gearman_string_t ret= {0, 0};
  164. return ret;
  165. }
  166. gearman_string_t gearman_result_take_string(gearman_result_st *self)
  167. {
  168. if (self)
  169. {
  170. return self->take();
  171. }
  172. static gearman_string_t ret= {0, 0};
  173. return ret;
  174. }
  175. gearman_return_t gearman_result_store_string(gearman_result_st *self, gearman_string_t arg)
  176. {
  177. if (self)
  178. {
  179. if (self->store(gearman_string_param(arg)) == false)
  180. {
  181. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  182. }
  183. return GEARMAN_SUCCESS;
  184. }
  185. return GEARMAN_INVALID_ARGUMENT;
  186. }
  187. bool gearman_result_st::append(const char* arg, const size_t arg_length)
  188. {
  189. if (_type != GEARMAN_RESULT_BINARY)
  190. {
  191. clear();
  192. _type= GEARMAN_RESULT_BINARY;
  193. }
  194. if (value.string.append(arg, arg_length) == false)
  195. {
  196. _type= GEARMAN_RESULT_NULL;
  197. return false;
  198. }
  199. return true;
  200. }
  201. #if 0
  202. bool gearman_result_st::store(bool arg)
  203. {
  204. if (_type != GEARMAN_RESULT_BOOLEAN)
  205. {
  206. value.string.clear();
  207. }
  208. value._boolean= arg;
  209. _type= GEARMAN_RESULT_BOOLEAN;
  210. return true;
  211. }
  212. #endif
  213. bool gearman_result_st::store(int64_t arg)
  214. {
  215. if (_type != GEARMAN_RESULT_INTEGER)
  216. {
  217. value.string.clear();
  218. }
  219. value._integer= arg;
  220. _type= GEARMAN_RESULT_INTEGER;
  221. return true;
  222. }
  223. bool gearman_result_st::store(const gearman_string_t& arg)
  224. {
  225. return store(gearman_c_str(arg), gearman_size(arg));
  226. }
  227. bool gearman_result_st::store(const char* arg, const size_t arg_length)
  228. {
  229. value.string.clear();
  230. if (value.string.store(arg, arg_length) == false)
  231. {
  232. _type= GEARMAN_RESULT_NULL;
  233. return false;
  234. }
  235. _type= GEARMAN_RESULT_BINARY;
  236. return true;
  237. }
  238. gearman_return_t gearman_result_store_value(gearman_result_st *self, const void *value, size_t size)
  239. {
  240. if (self)
  241. {
  242. return self->store((const char*)value, size) == true ? GEARMAN_SUCCESS : GEARMAN_MEMORY_ALLOCATION_FAILURE;
  243. }
  244. return GEARMAN_INVALID_ARGUMENT;
  245. }
  246. bool gearman_result_st::integer(int64_t arg_)
  247. {
  248. if (_type != GEARMAN_RESULT_INTEGER)
  249. {
  250. clear();
  251. _type= GEARMAN_RESULT_INTEGER;
  252. }
  253. value._integer= arg_;
  254. return true;
  255. }
  256. void gearman_result_store_integer(gearman_result_st *self, int64_t arg_)
  257. {
  258. if (self)
  259. {
  260. self->integer(arg_);
  261. }
  262. }
  263. void gearman_result_store_boolean(gearman_result_st *self, const bool arg_)
  264. {
  265. if (self)
  266. {
  267. self->boolean(arg_);
  268. }
  269. }