Skip to content

Commit

Permalink
guard all usages of throw
Browse files Browse the repository at this point in the history
add `fastgltf::raise` to guard all usages of raw throw
  • Loading branch information
apache-hb committed Mar 5, 2024
1 parent 84aa81c commit ac47794
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
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

0 comments on commit ac47794

Please sign in to comment.