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