Skip to content

Commit

Permalink
Added read and write binary utility functions for primitive types.
Browse files Browse the repository at this point in the history
  • Loading branch information
MStachowicz committed Mar 30, 2024
1 parent 8c079db commit dd4d654
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions source/Utility/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <filesystem>
#include <functional>
#include <string>
#include <type_traits>
#include <fstream>

namespace Utility
{
Expand Down Expand Up @@ -38,6 +40,21 @@ namespace Utility
class File
{
public:
// Write a value to a file in binary. Only works for arithmatic types.
template <typename T>
static void write_binary(std::ofstream& p_stream, const T& value)
{
static_assert(std::is_arithmetic_v<T>, "Type T is not a primitive type, cannot be written to file in binary safely.");
p_stream.write(reinterpret_cast<const char*>(&value), sizeof(T));
}
// Read a value from a file in binary. Only works for arithmatic types.
template <typename T>
static void read_binary(std::ifstream& p_stream, T& value)
{
static_assert(std::is_arithmetic_v<T>, "Type T is not a primitive type, cannot be read from file in binary safely.");
p_stream.read(reinterpret_cast<char*>(&value), sizeof(T));
}

static bool exists(const std::filesystem::path& p_path) { return std::filesystem::exists(p_path); }
static void foreach_file(const std::filesystem::path& p_directory, const std::function<void(const std::filesystem::directory_entry& p_entry)>& p_function);
static void foreach_file_recursive(const std::filesystem::path& p_directory, const std::function<void(const std::filesystem::directory_entry& p_entry)>& p_function);
Expand Down

0 comments on commit dd4d654

Please sign in to comment.