diff --git a/libraries/mpio b/libraries/mpio index f1d8d7b..39e5075 160000 --- a/libraries/mpio +++ b/libraries/mpio @@ -1 +1 @@ -Subproject commit f1d8d7b0985e1033a121b831e1b3db423965ed63 +Subproject commit 39e50757646e343855186d846f1522361bf4be1b diff --git a/wrappers/cpp/pack/common.hpp b/wrappers/cpp/pack/common.hpp index 96b46b7..ca2a46d 100644 --- a/wrappers/cpp/pack/common.hpp +++ b/wrappers/cpp/pack/common.hpp @@ -19,8 +19,7 @@ **********************************************************************************************************************/ #pragma once -#include -#include +#include "pack/error.hpp" #include extern "C" @@ -31,8 +30,6 @@ extern "C" namespace pack { -using namespace std; - /** * @brief Common Pack functions. * @details See the @ref common.h @@ -61,14 +58,14 @@ class Common final * @param[in] filePath target file path string * @param[out] header reference to the @ref PackHeader structure * - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error with a @ref PackResult string on failure. */ static void readHeader(const filesystem::path& filePath, PackHeader& header) { auto path = filePath.generic_string(); auto result = readPackHeader(path.c_str(), &header); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string())); + throw Error(packResultToString(result) + (", path: " + filePath.generic_string())); } /** diff --git a/wrappers/cpp/pack/error.hpp b/wrappers/cpp/pack/error.hpp new file mode 100644 index 0000000..648a7e0 --- /dev/null +++ b/wrappers/cpp/pack/error.hpp @@ -0,0 +1,48 @@ +// Copyright 2021-2024 Nikita Fediuchin. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/*********************************************************************************************************************** + * @file + * @brief Common Pack error (exception) functions. + **********************************************************************************************************************/ + +#pragma once +#include +#include + +namespace pack +{ + +using namespace std; + +/** + * @brief Pack error (exception) class. + */ +class Error : public exception +{ + string message; +public: + /** + * @brief Creates a new Pack error (exception) instance. + * @param message target error message + */ + Error(const string& message) : message(message) { } + + /** + * @brief Returns Pack error message C-string. + */ + const char* what() const noexcept override { return message.c_str(); } +}; + +} // mpio \ No newline at end of file diff --git a/wrappers/cpp/pack/reader.hpp b/wrappers/cpp/pack/reader.hpp index 3295d1e..968090c 100644 --- a/wrappers/cpp/pack/reader.hpp +++ b/wrappers/cpp/pack/reader.hpp @@ -19,10 +19,10 @@ **********************************************************************************************************************/ #pragma once -#include +#include "pack/error.hpp" + #include #include -#include #include #include @@ -34,8 +34,6 @@ extern "C" namespace pack { -using namespace std; - /** * @brief Pack reader instance handle. * @details See the @ref reader.h @@ -68,7 +66,7 @@ class Reader final * @param isResourcesDirectory read from the resources directory (Android/iOS/macOS only) * @param threadCount max concurrent read thread count * - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error with a @ref PackResult string on failure. */ Reader(const filesystem::path& filePath, bool isResourcesDirectory = true, uint32_t threadCount = thread::hardware_concurrency()) @@ -76,7 +74,7 @@ class Reader final auto path = filePath.generic_string(); auto result = createFilePackReader(path.c_str(), isResourcesDirectory, threadCount, &instance); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string())); + throw Error(packResultToString(result)); } /** @@ -93,7 +91,7 @@ class Reader final * @param isResourcesDirectory read from the resources directory (Android/iOS/macOS only) * @param threadCount max concurrent read thread count * - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error with a @ref PackResult string on failure. */ void open(const filesystem::path& filePath, bool isResourcesDirectory = true, uint32_t threadCount = thread::hardware_concurrency()) @@ -102,7 +100,7 @@ class Reader final auto path = filePath.generic_string(); auto result = createFilePackReader(path.c_str(), isResourcesDirectory, threadCount, &instance); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string())); + throw Error(packResultToString(result)); } /** @@ -149,7 +147,7 @@ class Reader final * * @param[in] path item path string used to pack the file * @return The item index in the Pack. - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error if item does not exist. */ uint64_t getItemIndex(const filesystem::path& path) const { @@ -157,7 +155,7 @@ class Reader final auto _path = path.generic_string(); auto result = getPackItemIndex(instance, _path.c_str(), &index); if (!result) - throw runtime_error("Item is not exist"); + throw Error("Item does not exist"); return index; } @@ -193,13 +191,13 @@ class Reader final * @param[out] buffer pointer to the buffer where to read item data * @param threadIndex current thread index or 0 * - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error with a @ref PackResult string on failure. */ void readItemData(uint64_t itemIndex, uint8_t* buffer, uint32_t threadIndex = 0) const { auto result = readPackItemData(instance, itemIndex, buffer, threadIndex); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result) + (", index: " + to_string(itemIndex))); + throw Error(packResultToString(result)); } /** @@ -210,14 +208,14 @@ class Reader final * @param[out] buffer reference to the buffer where to read item data * @param threadIndex current thread index or 0 * - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error with a @ref PackResult string on failure. */ void readItemData(uint64_t itemIndex, vector& buffer, uint32_t threadIndex = 0) const { buffer.resize(getPackItemDataSize(instance, itemIndex)); auto result = readPackItemData(instance, itemIndex, buffer.data(), threadIndex); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result) + (", index: " + to_string(itemIndex))); + throw Error(packResultToString(result)); } /** @@ -228,7 +226,7 @@ class Reader final * @param[out] buffer reference to the buffer where to read item data * @param threadIndex current thread index or 0 * - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error with a @ref PackResult string on failure. */ void readItemData(const filesystem::path& path, vector& buffer, uint32_t threadIndex = 0) const { @@ -236,7 +234,7 @@ class Reader final buffer.resize(getPackItemDataSize(instance, itemIndex)); auto result = readPackItemData(instance, itemIndex, buffer.data(), threadIndex); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result) + (", path: " + path.generic_string())); + throw Error(packResultToString(result)); } /******************************************************************************************************************* @@ -289,14 +287,14 @@ class Reader final * @param[in] filePath target Pack file path string * @param printProgress output unpacking progress to the stdout * - * @throw runtime_error with a @ref PackResult string on failure. + * @throw Error with a @ref PackResult string on failure. */ static void unpack(const filesystem::path& filePath, bool printProgress = false) { auto path = filePath.generic_string(); auto result = unpackFiles(path.c_str(), printProgress); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string())); + throw Error(packResultToString(result)); } }; diff --git a/wrappers/cpp/pack/writer.hpp b/wrappers/cpp/pack/writer.hpp index 97c091d..b7eea45 100644 --- a/wrappers/cpp/pack/writer.hpp +++ b/wrappers/cpp/pack/writer.hpp @@ -19,8 +19,7 @@ **********************************************************************************************************************/ #pragma once -#include -#include +#include "pack/error.hpp" #include extern "C" @@ -31,8 +30,6 @@ extern "C" namespace pack { -using namespace std; - /** * @brief Pack writer functions. * @details See the @ref writer.h @@ -51,6 +48,8 @@ class Writer final * @param printProgress output packing progress to the stdout * @param[in] onPackFile file packing callback, or NULL * @param[in] argument file packing callback argument, or NULL + * + * @throw Error with a @ref PackResult string on failure. */ static void pack(const filesystem::path& packPath, uint64_t fileCount, const char** fileItemPaths, @@ -61,7 +60,7 @@ class Writer final auto result = packFiles(path.c_str(), fileCount, fileItemPaths, zipThreshold, printProgress, onPackFile, argument); if (result != SUCCESS_PACK_RESULT) - throw runtime_error(packResultToString(result)); + throw Error(packResultToString(result)); } };