vector.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Libgearman library
  4. *
  5. * Copyright (C) 2011-2013 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 <cstdarg>
  40. #include "libgearman-1.0/string.h"
  41. constexpr auto GEARMAN_VECTOR_BLOCK_SIZE = 1024 * 4;
  42. /**
  43. vectors are always under our control so we make some assumptions about them.
  44. 1) is_initialized is always valid.
  45. 2) A string once intialized will always be, until free where we
  46. unset this flag.
  47. */
  48. struct gearman_vector_st {
  49. char *end;
  50. char *string;
  51. size_t current_size;
  52. struct Options {
  53. bool is_allocated;
  54. bool is_initialized;
  55. Options() :
  56. is_allocated{false},
  57. is_initialized{true}
  58. { }
  59. } options;
  60. gearman_vector_st() :
  61. end{nullptr},
  62. string{nullptr},
  63. current_size{0}
  64. {
  65. }
  66. gearman_vector_st(const gearman_vector_st& copy) :
  67. end{nullptr},
  68. string{nullptr},
  69. current_size{0}
  70. {
  71. store(copy);
  72. }
  73. gearman_vector_st(const size_t reserve);
  74. ~gearman_vector_st();
  75. gearman_vector_st& operator=(const gearman_vector_st&) = delete;
  76. bool resize(const size_t);
  77. bool reserve(const size_t);
  78. int vec_printf(const char *format__, ...); // __printflike(1, 2);
  79. int vec_append_printf(const char *format__, ...); // __printflike(1, 2);
  80. void clear()
  81. {
  82. end= string;
  83. if (current_size)
  84. {
  85. string[0]= 0;
  86. }
  87. }
  88. const char* value() const
  89. {
  90. return string;
  91. }
  92. const char* c_str() const
  93. {
  94. return string;
  95. }
  96. const void* void_ptr() const
  97. {
  98. return (const void*)string;
  99. }
  100. bool empty() const
  101. {
  102. return string == end;
  103. }
  104. size_t capacity() const
  105. {
  106. // We tell a white lie about size since we always keep things null
  107. // terminated
  108. if (current_size == 1)
  109. {
  110. return 0;
  111. }
  112. return current_size;
  113. }
  114. char* ptr(size_t expect)
  115. {
  116. if (resize(expect +1))
  117. {
  118. end= string +expect;
  119. string[expect]= 0;
  120. return string;
  121. }
  122. return nullptr;
  123. }
  124. bool store(const gearman_vector_st&);
  125. bool store(const char*, const size_t);
  126. bool append(const char* arg_, const size_t arg_length_);
  127. bool append_character(const char character);
  128. size_t size() const;
  129. gearman_string_t take();
  130. private:
  131. void init();
  132. int vec_size_printf(const char *format__, va_list args__);
  133. int vec_ptr_printf(const int required_size, const char *format__, va_list args__);
  134. };
  135. gearman_vector_st *gearman_string_create(gearman_vector_st *string,
  136. size_t initial_size);
  137. gearman_vector_st *gearman_string_create(gearman_vector_st *self,
  138. const char *str, size_t initial_size);