result.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 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 <libgearman/common.h>
  38. #include <cassert>
  39. #include <cstdlib>
  40. #include <limits>
  41. #include <memory>
  42. #include <libgearman/common.h>
  43. #include <libgearman/result.hpp>
  44. bool gearman_result_is_null(const gearman_result_st *self)
  45. {
  46. return self->is_null();
  47. }
  48. int64_t gearman_result_integer(const gearman_result_st *self)
  49. {
  50. if (not self)
  51. return false;
  52. switch (self->type)
  53. {
  54. case GEARMAN_RESULT_BINARY:
  55. return atoll(gearman_string_value(&self->value.string));
  56. case GEARMAN_RESULT_BOOLEAN:
  57. return self->value.boolean;
  58. case GEARMAN_RESULT_INTEGER:
  59. return self->value.integer;
  60. }
  61. return false;
  62. }
  63. bool gearman_result_boolean(const gearman_result_st *self)
  64. {
  65. if (not self)
  66. return false;
  67. switch (self->type)
  68. {
  69. case GEARMAN_RESULT_BINARY:
  70. return gearman_string_length(&self->value.string);
  71. case GEARMAN_RESULT_BOOLEAN:
  72. return self->value.boolean;
  73. case GEARMAN_RESULT_INTEGER:
  74. return self->value.integer ? true : false;
  75. }
  76. return false;
  77. }
  78. size_t gearman_result_size(const gearman_result_st *self)
  79. {
  80. if (not self or self->type != GEARMAN_RESULT_BINARY)
  81. return 0;
  82. return gearman_string_length(&self->value.string);
  83. }
  84. const char *gearman_result_value(const gearman_result_st *self)
  85. {
  86. if (not self or self->type != GEARMAN_RESULT_BINARY)
  87. return NULL;
  88. gearman_string_t ret= gearman_string(&self->value.string);
  89. return gearman_c_str(ret);
  90. }
  91. gearman_string_t gearman_result_string(const gearman_result_st *self)
  92. {
  93. if (not self or self->type != GEARMAN_RESULT_BINARY)
  94. {
  95. gearman_string_t ret= {0, 0};
  96. return ret;
  97. }
  98. return gearman_string(&self->value.string);
  99. }
  100. gearman_string_t gearman_result_take_string(gearman_result_st *self)
  101. {
  102. assert(self); // Programming error
  103. if (self->type == GEARMAN_RESULT_BINARY and gearman_result_size(self))
  104. {
  105. return gearman_string_take_string(&self->value.string);
  106. }
  107. self->_is_null= false;
  108. static gearman_string_t ret= {0, 0};
  109. return ret;
  110. }
  111. gearman_return_t gearman_result_store_string(gearman_result_st *self, gearman_string_t arg)
  112. {
  113. return gearman_result_store_value(self, gearman_string_param(arg));
  114. }
  115. gearman_return_t gearman_result_store_value(gearman_result_st *self, const void *value, size_t size)
  116. {
  117. if (not self)
  118. return GEARMAN_INVALID_ARGUMENT;
  119. if (self->type == GEARMAN_RESULT_BINARY)
  120. {
  121. gearman_string_reset(&self->value.string);
  122. }
  123. else
  124. {
  125. self->type= GEARMAN_RESULT_BINARY;
  126. gearman_string_create(&self->value.string, size);
  127. }
  128. self->_is_null= false;
  129. return gearman_string_append(&self->value.string, static_cast<const char *>(value), size);
  130. }
  131. void gearman_result_store_integer(gearman_result_st *self, int64_t value)
  132. {
  133. if (not self)
  134. return;
  135. if (self->type == GEARMAN_RESULT_BINARY)
  136. {
  137. gearman_string_free(&self->value.string);
  138. }
  139. self->type= GEARMAN_RESULT_INTEGER;
  140. self->value.integer= value;
  141. self->_is_null= false;
  142. }