From bec8a6d54f65dac0ccd4152430bf0a3e41b88744 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Tue, 6 Jun 2023 15:36:07 +0200 Subject: [PATCH] rewrite ForEachInArray as a simple C++11 function no need for std::function here. A free function with an unspecified callback will do nicely. --- tiny_gltf.h | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index f497ad9..d3a1748 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -5775,6 +5775,24 @@ static bool ParseLight(Light *light, std::string *err, const detail::json &o, return true; } +namespace detail { + +template +bool ForEachInArray(const detail::json &_v, const char *member, Callback && cb) { + detail::json_const_iterator itm; + if (detail::FindMember(_v, member, itm) && detail::IsArray(detail::GetValue(itm))) { + const detail::json &root = detail::GetValue(itm); + auto it = detail::ArrayBegin(root); + auto end = detail::ArrayEnd(root); + for (; it != end; ++it) { + if (!cb(*it)) return false; + } + } + return true; +}; + +} // end of namespace detail + bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn, const char *json_str, unsigned int json_str_length, @@ -5924,28 +5942,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn, } } -#ifdef TINYGLTF_USE_CPP14 - auto ForEachInArray = [](const detail::json &_v, const char *member, - const auto &cb) -> bool -#else - // The std::function<> implementation can be less efficient because it will - // allocate heap when the size of the captured lambda is above 16 bytes with - // clang and gcc, but it does not require C++14. - auto ForEachInArray = [](const detail::json &_v, const char *member, - const std::function &cb) -> bool -#endif - { - detail::json_const_iterator itm; - if (detail::FindMember(_v, member, itm) && detail::IsArray(detail::GetValue(itm))) { - const detail::json &root = detail::GetValue(itm); - auto it = detail::ArrayBegin(root); - auto end = detail::ArrayEnd(root); - for (; it != end; ++it) { - if (!cb(*it)) return false; - } - } - return true; - }; + using detail::ForEachInArray; // 2. Parse extensionUsed {