fbuffer.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // MessagePack for C++ FILE* buffer adaptor
  3. //
  4. // Copyright (C) 2013 Vladimir Volodko
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef MSGPACK_V1_FBUFFER_HPP
  11. #define MSGPACK_V1_FBUFFER_HPP
  12. #include "msgpack/v1/fbuffer_decl.hpp"
  13. #include "msgpack/assert.hpp"
  14. #include <cstdio>
  15. #include <stdexcept>
  16. namespace msgpack {
  17. /// @cond
  18. MSGPACK_API_VERSION_NAMESPACE(v1) {
  19. /// @endcond
  20. class fbuffer {
  21. public:
  22. explicit fbuffer(FILE* file) : m_file(file) { }
  23. public:
  24. void write(const char* buf, unsigned int len)
  25. {
  26. MSGPACK_ASSERT(buf || len == 0);
  27. if (!buf) return;
  28. if (len == 0) return;
  29. if (1 != fwrite(buf, len, 1, m_file)) {
  30. throw std::runtime_error("fwrite() failed");
  31. }
  32. }
  33. FILE* file() const
  34. {
  35. return m_file;
  36. }
  37. #if defined(MSGPACK_USE_CPP03)
  38. private:
  39. fbuffer(const fbuffer&);
  40. fbuffer& operator=(const fbuffer&);
  41. #else // defined(MSGPACK_USE_CPP03)
  42. fbuffer(const fbuffer&) = delete;
  43. fbuffer& operator=(const fbuffer&) = delete;
  44. #endif // defined(MSGPACK_USE_CPP03)
  45. private:
  46. FILE* m_file;
  47. };
  48. /// @cond
  49. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  50. /// @endcond
  51. } // namespace msgpack
  52. #endif // MSGPACK_V1_FBUFFER_HPP