Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard all usages of throw #50

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions include/fastgltf/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,21 +860,13 @@ namespace fastgltf {

[[nodiscard]] T& at(std::size_t idx) {
if (idx >= size()) {
#ifdef __cpp_exceptions
throw std::out_of_range("Index is out of range for SmallVector");
#else
std::abort();
#endif
fastgltf::raise<std::out_of_range>("Index is out of range for SmallVector");
}
return begin()[idx];
}
[[nodiscard]] const T& at(std::size_t idx) const {
if (idx >= size()) {
#ifdef __cpp_exceptions
throw std::out_of_range("Index is out of range for SmallVector");
#else
std::abort();
#endif
fastgltf::raise<std::out_of_range>("Index is out of range for SmallVector");
}
return begin()[idx];
}
Expand Down Expand Up @@ -1083,28 +1075,28 @@ namespace fastgltf {

[[nodiscard]] T& value()& {
if (!has_value()) {
throw std::bad_optional_access();
fastgltf::raise<std::bad_optional_access>();
}
return _value;
}

[[nodiscard]] const T& value() const& {
if (!has_value()) {
throw std::bad_optional_access();
fastgltf::raise<std::bad_optional_access>();
}
return _value;
}

[[nodiscard]] T&& value()&& {
if (!has_value()) {
throw std::bad_optional_access();
fastgltf::raise<std::bad_optional_access>();
}
return std::move(_value);
}

[[nodiscard]] const T&& value() const&& {
if (!has_value()) {
throw std::bad_optional_access();
fastgltf::raise<std::bad_optional_access>();
}
return std::move(_value);
}
Expand Down Expand Up @@ -1560,7 +1552,7 @@ namespace fastgltf {
FASTGLTF_STD_PMR_NS::vector<std::pair<FASTGLTF_STD_PMR_NS::string, std::size_t>> instancingAttributes;

FASTGLTF_STD_PMR_NS::string name;

[[nodiscard]] auto findInstancingAttribute(std::string_view attributeName) noexcept {
for (auto it = instancingAttributes.begin(); it != instancingAttributes.end(); ++it) {
if (it->first == attributeName)
Expand Down Expand Up @@ -1931,7 +1923,7 @@ namespace fastgltf {
AccessorType type;
ComponentType componentType;
bool normalized = false;

std::variant<std::monostate, FASTGLTF_STD_PMR_NS::vector<double>, FASTGLTF_STD_PMR_NS::vector<std::int64_t>> max;
std::variant<std::monostate, FASTGLTF_STD_PMR_NS::vector<double>, FASTGLTF_STD_PMR_NS::vector<std::int64_t>> min;

Expand Down
11 changes: 10 additions & 1 deletion include/fastgltf/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ namespace fastgltf {
return (a > b) ? a : b;
}

template<typename T, typename... A>
[[noreturn]] constexpr void raise(A&&... args) {
#ifdef __cpp_exceptions
throw T(std::forward<A>(args)...);
#else
std::abort();
#endif
}

/**
* Decomposes a transform matrix into the translation, rotation, and scale components. This
* function does not support skew, shear, or perspective. This currently uses a quick algorithm
Expand Down Expand Up @@ -328,7 +337,7 @@ namespace fastgltf {
* Helper type in order to allow building a visitor out of multiple lambdas within a call to
* std::visit
*/
template<class... Ts>
template<class... Ts>
struct visitor : Ts... {
using Ts::operator()...;
};
Expand Down
Loading