raw_result_buffer.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_samty {
  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. CPPCODEC_ALWAYS_INLINE void push_back(char c) { *m_ptr = c; ++m_ptr; }
  39. CPPCODEC_ALWAYS_INLINE size_t size() const { return m_ptr - m_begin; }
  40. CPPCODEC_ALWAYS_INLINE void resize(size_t size) { m_ptr = m_begin + size; }
  41. private:
  42. char* m_ptr;
  43. char* m_begin;
  44. };
  45. template <> inline void init<raw_result_buffer>(
  46. raw_result_buffer& result, empty_result_state&, size_t capacity)
  47. {
  48. // This version of init() doesn't do a reserve(), and instead checks whether the
  49. // initial size (capacity) is enough before resetting m_ptr to m_begin.
  50. // The codec is expected not to exceed this capacity.
  51. if (capacity > result.size()) {
  52. abort();
  53. }
  54. result.resize(0);
  55. }
  56. template <> inline void finish<raw_result_buffer>(raw_result_buffer&, empty_result_state&) { }
  57. } // namespace data
  58. } // namespace cppcodec
  59. #endif