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_FBUFFER_HPP__
11#define MSGPACK_FBUFFER_HPP__
12
13#include "msgpack/versioning.hpp"
14
15#include <cstdio>
16#include <stdexcept>
17
18namespace msgpack {
19
20/// @cond
21MSGPACK_API_VERSION_NAMESPACE(v1) {
22/// @endcond
23
24class fbuffer {
25public:
26    explicit fbuffer(FILE* file) : m_file(file) { }
27
28public:
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)
42private:
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
50private:
51    FILE* m_file;
52};
53
54/// @cond
55}  // MSGPACK_API_VERSION_NAMESPACE(v1)
56/// @endcond
57
58}  // namespace msgpack
59
60#endif /* msgpack/fbuffer.hpp */
61