draconisplusplus/include/rfl/io/save_bytes.hpp

32 lines
670 B
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_IO_SAVE_BYTES_HPP_
#define RFL_IO_SAVE_BYTES_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "../Result.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace io {
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class T, class WriteFunction>
2024-06-08 15:53:06 -04:00
Result<Nothing> save_bytes(
const std::string& _fname,
const T& _obj,
const WriteFunction& _write
) {
2024-06-08 14:10:59 -04:00
try {
std::ofstream output(_fname, std::ios::out | std::ios::binary);
_write(_obj, output);
output.close();
} catch (std::exception& e) { return Error(e.what()); }
return Nothing {};
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace io
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif