vector.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Libgearman library
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #pragma once
  37. #include <cstddef>
  38. #include <cstdlib>
  39. #include "libgearman-1.0/string.h"
  40. #define GEARMAN_VECTOR_BLOCK_SIZE 1024*4
  41. /**
  42. vectors are always under our control so we make some assumptions about them.
  43. 1) is_initialized is always valid.
  44. 2) A string once intialized will always be, until free where we
  45. unset this flag.
  46. */
  47. struct gearman_vector_st {
  48. char *end;
  49. char *string;
  50. size_t current_size;
  51. struct Options {
  52. bool is_allocated;
  53. bool is_initialized;
  54. Options() :
  55. is_allocated(false),
  56. is_initialized(true)
  57. { }
  58. } options;
  59. gearman_vector_st() :
  60. end(NULL),
  61. string(NULL),
  62. current_size(0)
  63. {
  64. }
  65. gearman_vector_st(size_t initial_size);
  66. ~gearman_vector_st();
  67. void resize(size_t);
  68. void reserve(size_t);
  69. void clear()
  70. {
  71. end= string;
  72. if (current_size)
  73. {
  74. string[0]= 0;
  75. }
  76. }
  77. const char* value() const
  78. {
  79. return string;
  80. }
  81. size_t capacity() const
  82. {
  83. // We tell a white lie about size since we always keep things null
  84. // terminated
  85. if (current_size == 1)
  86. {
  87. return 0;
  88. }
  89. return current_size;
  90. }
  91. bool store(const char*, const size_t);
  92. bool append(const char* arg_, const size_t arg_length_);
  93. size_t size() const;
  94. void init();
  95. };
  96. gearman_vector_st *gearman_string_create(gearman_vector_st *string,
  97. size_t initial_size);
  98. gearman_vector_st *gearman_string_create(gearman_vector_st *self, const char *str, size_t initial_size);
  99. #ifdef __cplusplus
  100. extern "C" {
  101. #endif
  102. gearman_vector_st *gearman_string_clone(const gearman_vector_st *);
  103. bool gearman_string_reserve(gearman_vector_st *string, size_t need);
  104. char *gearman_string_c_copy(gearman_vector_st *string);
  105. bool gearman_string_append_character(gearman_vector_st *string,
  106. char character);
  107. bool gearman_string_append(gearman_vector_st *string,
  108. const char *value, size_t length);
  109. void gearman_string_clear(gearman_vector_st *string);
  110. void gearman_string_free(gearman_vector_st *string);
  111. size_t gearman_string_length(const gearman_vector_st *self);
  112. const char *gearman_string_value(const gearman_vector_st *self);
  113. char *gearman_string_value_mutable(const gearman_vector_st *self);
  114. gearman_string_t gearman_string(const gearman_vector_st *self);
  115. gearman_string_t gearman_string_take_string(gearman_vector_st *self);
  116. #ifdef __cplusplus
  117. }
  118. #endif