raw_result_buffer.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (C) 2015 Topology LP
  3. * All rights reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to
  7. * deal in the Software without restriction, including without limitation the
  8. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. * sell copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #ifndef CPPCODEC_DETAIL_RAW_RESULT_BUFFER
  24. #define CPPCODEC_DETAIL_RAW_RESULT_BUFFER
  25. #include <stdint.h> // for size_t
  26. #include <stdlib.h> // for abort()
  27. #include "access.hpp"
  28. namespace cppcodec {
  29. namespace data {
  30. class raw_result_buffer
  31. {
  32. public:
  33. raw_result_buffer(char* data, size_t capacity)
  34. : m_ptr(data + capacity)
  35. , m_begin(data)
  36. {
  37. }
  38. char last() const { return *(m_ptr - 1); }
  39. void push_back(char c) { *m_ptr = c; ++m_ptr; }
  40. size_t size() const { return m_ptr - m_begin; }
  41. void resize(size_t size) { m_ptr = m_begin + size; }
  42. private:
  43. char* m_ptr;
  44. char* m_begin;
  45. };
  46. template <> inline void init<raw_result_buffer>(
  47. raw_result_buffer& result, empty_result_state&, size_t capacity)
  48. {
  49. // This version of init() doesn't do a reserve(), and instead checks whether the
  50. // initial size (capacity) is enough before resizing to 0.
  51. // The codec is expected not to exceed this capacity.
  52. if (capacity > result.size()) {
  53. abort();
  54. }
  55. result.resize(0);
  56. }
  57. template <> inline void finish<raw_result_buffer>(raw_result_buffer&, empty_result_state&) { }
  58. } // namespace data
  59. } // namespace cppcodec
  60. #endif