Skip to content

Commit 15429e8

Browse files
committed
tags_int: reduce templates
use more if constexpr. Signed-off-by: Rosen Penev <[email protected]>
1 parent 51dfe90 commit 15429e8

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

src/tags_int.hpp

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,18 @@ struct TagVocabulary {
9292
@brief Generic pretty-print function to translate a full string value to a description
9393
by looking up a reference table.
9494
*/
95-
template <size_t N, const StringTagDetails (&array)[N]>
96-
std::ostream& printTagString(std::ostream& os, std::string_view value, const ExifData*) {
95+
template <size_t N, const StringTagDetails (&array)[N], typename T>
96+
std::ostream& printTagString(std::ostream& os, const T& value, const ExifData*) {
9797
static_assert(N > 0, "Passed zero length printTagString");
98-
if (auto td = Exiv2::find(array, value)) {
99-
os << _(td->label_);
98+
if constexpr (std::is_same_v<T, Value>) {
99+
if (auto td = Exiv2::find(array, value.toString(0)))
100+
return os << _(td->label_);
101+
return os << "(" << value << ")";
100102
} else {
101-
os << "(" << value << ")";
103+
if (auto td = Exiv2::find(array, value))
104+
return os << _(td->label_);
105+
return os << "(" << value << ")";
102106
}
103-
return os;
104-
}
105-
106-
/*!
107-
@brief Generic pretty-print function to translate the full string value in Value, to a description
108-
by looking up a reference table.
109-
*/
110-
template <size_t N, const StringTagDetails (&array)[N]>
111-
std::ostream& printTagString(std::ostream& os, const Value& value, const ExifData* data) {
112-
static_assert(N > 0, "Passed zero length printTagString");
113-
return printTagString<N, array>(os, value.toString(0), data);
114107
}
115108

116109
//! 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
152145
@brief Generic pretty-print function to translate a long value to a description
153146
by looking up a reference table. Unknown values are passed through without error.
154147
*/
155-
template <size_t N, const TagDetails (&array)[N]>
156-
std::ostream& printTagNoError(std::ostream& os, const int64_t value, const ExifData*) {
148+
template <size_t N, const TagDetails (&array)[N], typename T>
149+
std::ostream& printTagNoError(std::ostream& os, const T& value, const ExifData*) {
157150
static_assert(N > 0, "Passed zero length printTagNoError");
158-
if (auto td = Exiv2::find(array, value)) {
159-
os << _(td->label_);
151+
if constexpr (std::is_same_v<T, Value>) {
152+
if (auto td = Exiv2::find(array, value.toInt64()))
153+
return os << _(td->label_);
154+
return os << value;
160155
} else {
161-
os << value;
156+
if (auto td = Exiv2::find(array, value))
157+
return os << _(td->label_);
158+
return os << value;
162159
}
163-
return os;
164-
}
165-
166-
/*!
167-
@brief Generic pretty-print function to translate the full string value in Value, to a description
168-
by looking up a reference table.
169-
*/
170-
template <size_t N, const TagDetails (&array)[N]>
171-
std::ostream& printTagNoError(std::ostream& os, const Value& value, const ExifData* data) {
172-
static_assert(N > 0, "Passed zero length printTagNoError");
173-
return printTagNoError<N, array>(os, value.toInt64(), data);
174160
}
175161

176162
//! 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
181167
by looking up a reference table.
182168
*/
183169
template <size_t N, const TagDetails (&array)[N]>
184-
std::ostream& printTag(std::ostream& os, const int64_t value, const ExifData*) {
170+
std::ostream& printTag(std::ostream& os, int64_t value, const ExifData*) {
185171
static_assert(N > 0, "Passed zero length printTag");
186-
if (auto td = Exiv2::find(array, value)) {
187-
os << _(td->label_);
188-
} else {
189-
os << "(" << value << ")";
190-
}
191-
return os;
172+
if (auto td = Exiv2::find(array, value))
173+
return os << _(td->label_);
174+
return os << "(" << value << ")";
192175
}
193176

194177
/*!
@@ -301,12 +284,9 @@ std::ostream& printTagBitlistAllLE(std::ostream& os, const Value& value, const E
301284
template <size_t N, const TagVocabulary (&array)[N]>
302285
std::ostream& printTagVocabulary(std::ostream& os, const Value& value, const ExifData*) {
303286
static_assert(N > 0, "Passed zero length printTagVocabulary");
304-
if (auto td = Exiv2::find(array, value.toString())) {
305-
os << _(td->label_);
306-
} else {
307-
os << "(" << value << ")";
308-
}
309-
return os;
287+
if (auto td = Exiv2::find(array, value.toString()))
288+
return os << _(td->label_);
289+
return os << "(" << value << ")";
310290
}
311291

312292
//! 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
315295
template <size_t N, const TagVocabulary (&array)[N]>
316296
std::ostream& printTagVocabularyMulti(std::ostream& os, const Value& value, const ExifData*) {
317297
static_assert(N > 0, "Passed zero length printTagVocabularyMulti");
318-
if (value.count() == 0) {
319-
os << "(" << value << ")";
320-
return os;
321-
}
298+
if (value.count() == 0)
299+
return os << "(" << value << ")";
322300

323301
for (size_t i = 0; i < value.count(); i++) {
324302
if (i != 0)

0 commit comments

Comments
 (0)