Skip to content

Commit

Permalink
Implement Custom read methods
Browse files Browse the repository at this point in the history
  • Loading branch information
golxzn committed Jul 19, 2023
1 parent e3b2c33 commit 17ec237
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.23)
set(GRM_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

project(resman
VERSION 1.3.1
VERSION 1.4.0
DESCRIPTION "golxzn resource manager"
LANGUAGES CXX
)
Expand Down
161 changes: 139 additions & 22 deletions code/resman/headers/golxzn/resman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,76 @@ class resman final {
*/
[[nodiscard]] static std::string read_text(const std::wstring_view path);

// template<std::constructible_from<std::vector<uint8_t> &&> Custom>
// [[nodiscard]] static Custom read_binary(const std::wstring_view path) { return Custom{ read_binary(path) }; }

// template<std::constructible_from<std::string &&> Custom>
// [[nodiscard]] static Custom read_text(const std::wstring_view path) { return Custom{ read_text(path) }; }

// template<std::constructible_from<std::string &&> Custom>
// [[nodiscard]] static std::shared_ptr<Custom> read_shared_text(const std::wstring_view path) {
// return std::make_shared<Custom>(read_text(path));
// }
/**
* @brief Construct @p Custom class by binary data from file
*
* @tparam Custom Class to construct. Has to have a constructor with std::vector<uint8_t> argument
* @param path Path to the file
* @return Custom Constructed class
*/
template<std::constructible_from<std::vector<uint8_t>> Custom>
[[nodiscard]] static Custom read_binary(const std::wstring_view path) {
return Custom{ read_binary(path) };
}
/**
* @brief Construct @p Custom class by text data from file
*
* @tparam Custom Class to construct. Has to have a constructor with std::string argument
* @param path Path to the file
* @return Custom Constructed class
*/
template<std::constructible_from<std::string> Custom>
[[nodiscard]] static Custom read_text(const std::wstring_view path) {
return Custom{ read_text(path) };
}

// template<std::constructible_from<std::vector<uint8_t> &&> Custom>
// [[nodiscard]] static std::shared_ptr<Custom> read_shared_binary(const std::wstring_view path) {
// return std::make_shared<Custom>(read_binary(path));
// }
/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::vector<uint8_t> argument
* @param path Path to the file
* @return std::shared_ptr<Custom> Constructed shared class
*/
template<std::constructible_from<std::vector<uint8_t>> Custom>
[[nodiscard]] static std::shared_ptr<Custom> read_shared_binary(const std::wstring_view path) {
return std::make_shared<Custom>(read_binary(path));
}

// template<std::constructible_from<std::string &&> Custom>
// [[nodiscard]] static std::shared_ptr<Custom> read_unique_text(const std::wstring_view path) {
// return std::make_unique<Custom>(read_text(path));
// }
/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::string argument
* @param path Path to the file
* @return std::shared_ptr<Custom> Constructed shared class
*/
template<std::constructible_from<std::string> Custom>
[[nodiscard]] static std::shared_ptr<Custom> read_shared_text(const std::wstring_view path) {
return std::make_shared<Custom>(read_text(path));
}

// template<std::constructible_from<std::vector<uint8_t> &&> Custom>
// [[nodiscard]] static std::shared_ptr<Custom> read_unique_binary(const std::wstring_view path) {
// return std::make_unique<Custom>(read_binary(path));
// }
/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::vector<uint8_t> argument
* @param path Path to the file
* @return std::unique_ptr<Custom> Constructed unique class
*/
template<std::constructible_from<std::vector<uint8_t>> Custom>
[[nodiscard]] static std::unique_ptr<Custom> read_unique_binary(const std::wstring_view path) {
return std::make_unique<Custom>(read_binary(path));
}

/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::string argument
* @param path Path to the file
* @return std::unique_ptr<Custom> Constructed unique class
*/
template<std::constructible_from<std::string> Custom>
[[nodiscard]] static std::unique_ptr<Custom> read_unique_text(const std::wstring_view path) {
return std::make_unique<Custom>(read_text(path));
}

/** @} */

Expand Down Expand Up @@ -500,6 +544,79 @@ class resman final {
*/
[[nodiscard]] static std::string read_text(const std::string_view path);

/**
* @brief Construct @p Custom class by binary data from file
*
* @tparam Custom Class to construct. Has to have a constructor with std::vector<uint8_t> argument
* @param path Path to the file
* @return Custom Constructed class
*/
template<std::constructible_from<std::vector<uint8_t>> Custom>
[[nodiscard]] static Custom read_binary(const std::string_view path) {
return Custom{ read_binary(path) };
}
/**
* @brief Construct @p Custom class by text data from file
*
* @tparam Custom Class to construct. Has to have a constructor with std::string argument
* @param path Path to the file
* @return Custom Constructed class
*/
template<std::constructible_from<std::string> Custom>
[[nodiscard]] static Custom read_text(const std::string_view path) {
return Custom{ read_text(path) };
}

/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::vector<uint8_t> argument
* @param path Path to the file
* @return std::shared_ptr<Custom> Constructed shared class
*/
template<std::constructible_from<std::vector<uint8_t>> Custom>
[[nodiscard]] static std::shared_ptr<Custom> read_shared_binary(const std::string_view path) {
return std::make_shared<Custom>(read_binary(path));
}

/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::string argument
* @param path Path to the file
* @return std::shared_ptr<Custom> Constructed shared class
*/
template<std::constructible_from<std::string> Custom>
[[nodiscard]] static std::shared_ptr<Custom> read_shared_text(const std::string_view path) {
return std::make_shared<Custom>(read_text(path));
}

/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::vector<uint8_t> argument
* @param path Path to the file
* @return std::unique_ptr<Custom> Constructed unique class
*/
template<std::constructible_from<std::vector<uint8_t>> Custom>
[[nodiscard]] static std::unique_ptr<Custom> read_unique_binary(const std::string_view path) {
return std::make_unique<Custom>(read_binary(path));
}

/**
* @brief Construct @p std::shared_ptr<Custom> by binary data from file
*
* @tparam Custom Class to construct. Has to have a public constructor with std::string argument
* @param path Path to the file
* @return std::unique_ptr<Custom> Constructed unique class
*/
template<std::constructible_from<std::string> Custom>
[[nodiscard]] static std::unique_ptr<Custom> read_unique_text(const std::string_view path) {
return std::make_unique<Custom>(read_text(path));
}



/**
* @brief Write binary data to a file
*
Expand Down
60 changes: 60 additions & 0 deletions code/tests/src/read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,63 @@ TEST_CASE("resman", "[resman][read_write]") {
// BENCHMARK("Write user://write.bin") { return gxzn::resman::write_binary(path, expected_content); };
}
}

struct CustomResource {
explicit CustomResource(std::string &&content) noexcept : text{ std::move(content) } {}
explicit CustomResource(std::vector<uint8_t> &&content) noexcept : data{ std::move(content) } {}

std::string text;
std::vector<uint8_t> data;
};

TEST_CASE("resman", "[resman][read_write custom]") {
static constexpr std::string_view expected_text{ "custom" };
static constexpr std::initializer_list<uint8_t> expected_data{ 'c', 'u', 's', 't', 'o', 'm' };

SECTION("Construct CustomResource by res://custom.bin") {
auto res{ golxzn::resman::read_binary<CustomResource>("res://custom.bin") };
INFO("Read content: " << dump(res.data));
INFO("Expected content: " << dump(expected_data));
REQUIRE(res.data.size() == expected_data.size());
REQUIRE(std::equal(std::begin(res.data), std::end(res.data), std::begin(expected_data)));
}
SECTION("Construct CustomResource by res://custom.txt") {
auto res{ golxzn::resman::read_text<CustomResource>("res://custom.txt") };
INFO("Read content: " << res.text);
INFO("Expected content: " << expected_text);
REQUIRE(res.text.size() == expected_text.size());
REQUIRE(res.text == expected_text);
}
SECTION("Construct std::shared_ptr<CustomResource> by res://custom.bin") {
auto res{ golxzn::resman::read_shared_binary<CustomResource>("res://custom.bin") };
REQUIRE(res != nullptr);
INFO("Read content: " << dump(res->data));
INFO("Expected content: " << dump(expected_data));
REQUIRE(res->data.size() == expected_data.size());
REQUIRE(std::equal(std::begin(res->data), std::end(res->data), std::begin(expected_data)));
}
SECTION("Construct std::shared_ptr<CustomResource> by res://custom.txt") {
auto res{ golxzn::resman::read_shared_text<CustomResource>("res://custom.txt") };
REQUIRE(res != nullptr);
INFO("Read content: " << res->text);
INFO("Expected content: " << expected_text);
REQUIRE(res->text.size() == expected_text.size());
REQUIRE(res->text == expected_text);
}
SECTION("Construct std::unique_ptr<CustomResource> by res://custom.bin") {
auto res{ golxzn::resman::read_unique_binary<CustomResource>("res://custom.bin") };
REQUIRE(res != nullptr);
INFO("Read content: " << dump(res->data));
INFO("Expected content: " << dump(expected_data));
REQUIRE(res->data.size() == expected_data.size());
REQUIRE(std::equal(std::begin(res->data), std::end(res->data), std::begin(expected_data)));
}
SECTION("Construct std::unique_ptr<CustomResource> by res://custom.txt") {
auto res{ golxzn::resman::read_unique_text<CustomResource>("res://custom.txt") };
REQUIRE(res != nullptr);
INFO("Read content: " << res->text);
INFO("Expected content: " << expected_text);
REQUIRE(res->text.size() == expected_text.size());
REQUIRE(res->text == expected_text);
}
}

0 comments on commit 17ec237

Please sign in to comment.