fbuffer.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <cstdio>
  14. #include <stdexcept>
  15. namespace msgpack {
  16. /// @cond
  17. MSGPACK_API_VERSION_NAMESPACE(v1) {
  18. /// @endcond
  19. class fbuffer {
  20. public:
  21. explicit fbuffer(FILE* file) : m_file(file) { }
  22. public:
  23. void write(const char* buf, unsigned int len)
  24. {
  25. if (1 != fwrite(buf, len, 1, m_file)) {
  26. throw std::runtime_error("fwrite() failed");
  27. }
  28. }
  29. FILE* file() const
  30. {
  31. return m_file;
  32. }
  33. #if defined(MSGPACK_USE_CPP03)
  34. private:
  35. fbuffer(const fbuffer&);
  36. fbuffer& operator=(const fbuffer&);
  37. #else // defined(MSGPACK_USE_CPP03)
  38. fbuffer(const fbuffer&) = delete;
  39. fbuffer& operator=(const fbuffer&) = delete;
  40. #endif // defined(MSGPACK_USE_CPP03)
  41. private:
  42. FILE* m_file;
  43. };
  44. /// @cond
  45. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  46. /// @endcond
  47. } // namespace msgpack
  48. #endif // MSGPACK_V1_FBUFFER_HPP