From 15429e84420037534bbc6383c55ab4777e718127 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 26 Oct 2025 19:09:23 -0700 Subject: [PATCH] tags_int: reduce templates use more if constexpr. Signed-off-by: Rosen Penev --- src/tags_int.hpp | 76 +++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/src/tags_int.hpp b/src/tags_int.hpp index 36ac87b59d..81bef9ada4 100644 --- a/src/tags_int.hpp +++ b/src/tags_int.hpp @@ -92,25 +92,18 @@ struct TagVocabulary { @brief Generic pretty-print function to translate a full string value to a description by looking up a reference table. */ -template -std::ostream& printTagString(std::ostream& os, std::string_view value, const ExifData*) { +template +std::ostream& printTagString(std::ostream& os, const T& value, const ExifData*) { static_assert(N > 0, "Passed zero length printTagString"); - if (auto td = Exiv2::find(array, value)) { - os << _(td->label_); + if constexpr (std::is_same_v) { + if (auto td = Exiv2::find(array, value.toString(0))) + return os << _(td->label_); + return os << "(" << value << ")"; } else { - os << "(" << value << ")"; + if (auto td = Exiv2::find(array, value)) + return os << _(td->label_); + return os << "(" << value << ")"; } - return os; -} - -/*! - @brief Generic pretty-print function to translate the full string value in Value, to a description - by looking up a reference table. - */ -template -std::ostream& printTagString(std::ostream& os, const Value& value, const ExifData* data) { - static_assert(N > 0, "Passed zero length printTagString"); - return printTagString(os, value.toString(0), data); } //! Shortcut for the printStringTag template which requires typing the array name only once. @@ -152,25 +145,18 @@ std::ostream& printTagString4(std::ostream& os, const Value& value, const ExifDa @brief Generic pretty-print function to translate a long value to a description by looking up a reference table. Unknown values are passed through without error. */ -template -std::ostream& printTagNoError(std::ostream& os, const int64_t value, const ExifData*) { +template +std::ostream& printTagNoError(std::ostream& os, const T& value, const ExifData*) { static_assert(N > 0, "Passed zero length printTagNoError"); - if (auto td = Exiv2::find(array, value)) { - os << _(td->label_); + if constexpr (std::is_same_v) { + if (auto td = Exiv2::find(array, value.toInt64())) + return os << _(td->label_); + return os << value; } else { - os << value; + if (auto td = Exiv2::find(array, value)) + return os << _(td->label_); + return os << value; } - return os; -} - -/*! - @brief Generic pretty-print function to translate the full string value in Value, to a description - by looking up a reference table. - */ -template -std::ostream& printTagNoError(std::ostream& os, const Value& value, const ExifData* data) { - static_assert(N > 0, "Passed zero length printTagNoError"); - return printTagNoError(os, value.toInt64(), data); } //! Shortcut for the printStringTag template which requires typing the array name only once. @@ -181,14 +167,11 @@ std::ostream& printTagNoError(std::ostream& os, const Value& value, const ExifDa by looking up a reference table. */ template -std::ostream& printTag(std::ostream& os, const int64_t value, const ExifData*) { +std::ostream& printTag(std::ostream& os, int64_t value, const ExifData*) { static_assert(N > 0, "Passed zero length printTag"); - if (auto td = Exiv2::find(array, value)) { - os << _(td->label_); - } else { - os << "(" << value << ")"; - } - return os; + if (auto td = Exiv2::find(array, value)) + return os << _(td->label_); + return os << "(" << value << ")"; } /*! @@ -301,12 +284,9 @@ std::ostream& printTagBitlistAllLE(std::ostream& os, const Value& value, const E template std::ostream& printTagVocabulary(std::ostream& os, const Value& value, const ExifData*) { static_assert(N > 0, "Passed zero length printTagVocabulary"); - if (auto td = Exiv2::find(array, value.toString())) { - os << _(td->label_); - } else { - os << "(" << value << ")"; - } - return os; + if (auto td = Exiv2::find(array, value.toString())) + return os << _(td->label_); + return os << "(" << value << ")"; } //! Shortcut for the printTagVocabulary template which requires typing the array name only once. @@ -315,10 +295,8 @@ std::ostream& printTagVocabulary(std::ostream& os, const Value& value, const Exi template std::ostream& printTagVocabularyMulti(std::ostream& os, const Value& value, const ExifData*) { static_assert(N > 0, "Passed zero length printTagVocabularyMulti"); - if (value.count() == 0) { - os << "(" << value << ")"; - return os; - } + if (value.count() == 0) + return os << "(" << value << ")"; for (size_t i = 0; i < value.count(); i++) { if (i != 0)