/builds/MusicScience37Projects/utility-libraries/cpp-stat-bench/src/stat_bench/reporter/msgpack_reporter.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023 MusicScience37 (Kenta Kabashima) |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | /*! |
17 | | * \file |
18 | | * \brief Implementation of MsgPackReporter class. |
19 | | */ |
20 | | #include "stat_bench/reporter/msgpack_reporter.h" |
21 | | |
22 | | #include <cstdio> |
23 | | #include <utility> |
24 | | |
25 | | #include <msgpack_light/output_stream.h> |
26 | | #include <msgpack_light/serialize.h> |
27 | | |
28 | | #include "stat_bench/reporter/msgpack_data_file_helper.h" // IWYU pragma: keep |
29 | | #include "stat_bench/stat_bench_exception.h" |
30 | | #include "stat_bench/util/prepare_directory.h" |
31 | | |
32 | | namespace stat_bench { |
33 | | namespace reporter { |
34 | | |
35 | | /*! |
36 | | * \brief Class of stream to write to file used in cpp-msgpack-light library. |
37 | | */ |
38 | | class MsgpackOutputFileStream : public msgpack_light::output_stream { |
39 | | public: |
40 | | /*! |
41 | | * \brief Constructor. |
42 | | * |
43 | | * \param[in] file_path File path. |
44 | | */ |
45 | | explicit MsgpackOutputFileStream(const std::string& file_path) |
46 | 2 | : file_(std::fopen(file_path.c_str(), "wb")) {} |
47 | | |
48 | | MsgpackOutputFileStream(const MsgpackOutputFileStream&) = delete; |
49 | | MsgpackOutputFileStream(MsgpackOutputFileStream&&) = delete; |
50 | | auto operator=(const MsgpackOutputFileStream&) |
51 | | -> MsgpackOutputFileStream& = delete; |
52 | | auto operator=(MsgpackOutputFileStream&&) |
53 | | -> MsgpackOutputFileStream& = delete; |
54 | | |
55 | | /*! |
56 | | * \brief Destructor. |
57 | | */ |
58 | 2 | ~MsgpackOutputFileStream() { (void)std::fclose(file_); } |
59 | | |
60 | | /*! |
61 | | * \brief Write data. |
62 | | * |
63 | | * \param[in] data Pointer to the data. |
64 | | * \param[in] size Size of the data. |
65 | | */ |
66 | 23 | void write(const unsigned char* data, std::size_t size) override { |
67 | 23 | if (std::fwrite(data, 1, size, file_) != size) { |
68 | 0 | throw StatBenchException("Failed to write data."); |
69 | 0 | } |
70 | 23 | } |
71 | | |
72 | | private: |
73 | | //! File descriptor. |
74 | | std::FILE* file_; |
75 | | }; |
76 | | |
77 | | MsgPackReporter::MsgPackReporter(std::string file_path) |
78 | 2 | : DataFileReporterBase(std::move(file_path)) {} |
79 | | |
80 | | void MsgPackReporter::write_data_file( |
81 | 2 | const std::string& file_path, const data_file_spec::RootData& data) { |
82 | 2 | util::prepare_directory_for(file_path); |
83 | | |
84 | 2 | MsgpackOutputFileStream stream{file_path}; |
85 | 2 | msgpack_light::serialize_to(stream, data); |
86 | 2 | } |
87 | | |
88 | | } // namespace reporter |
89 | | } // namespace stat_bench |