Skip to content

Commit cf1015c

Browse files
committed
reintroduce if constexpr
We can use SFINAE for pre C++ and if constexpr for post instead of handling this in cpp files. A bit cleaner. Signed-off-by: Rosen Penev <[email protected]>
1 parent 4644a09 commit cf1015c

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

include/exiv2/xmp_exiv2.hpp

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,12 @@ class EXIV2API Xmpdatum : public Metadatum {
5656
@brief Assign std::string \em value to the %Xmpdatum.
5757
Calls setValue(const std::string&).
5858
*/
59-
Xmpdatum& operator=(const std::string& value);
60-
/*!
61-
@brief Assign a boolean \em value to the %Xmpdatum.
62-
Translates the value to a string "true" or "false".
63-
*/
64-
Xmpdatum& operator=(bool value);
65-
/*!
66-
@brief Assign a \em value of any type with an output operator
67-
to the %Xmpdatum. Calls operator=(const std::string&).
68-
*/
6959
template <typename T>
7060
Xmpdatum& operator=(const T& value);
7161
/*!
7262
@brief Assign Value \em value to the %Xmpdatum.
7363
Calls setValue(const Value*).
7464
*/
75-
Xmpdatum& operator=(const Value& value);
7665
void setValue(const Value* pValue) override;
7766
/*!
7867
@brief Set the value to the string \em value. Uses Value::read(const
@@ -398,12 +387,49 @@ class EXIV2API XmpParser {
398387

399388
// *****************************************************************************
400389
// free functions, template and inline definitions
390+
391+
#if __cpp_if_constexpr
401392
template <typename T>
402393
Xmpdatum& Xmpdatum::operator=(const T& value) {
403-
setValue(Exiv2::toString(value));
394+
if constexpr (std::is_same_v<T, bool>)
395+
setValue(value ? "True" : "False");
396+
else if constexpr (std::is_convertible_v<T, std::string>)
397+
setValue(value);
398+
else if constexpr (std::is_base_of_v<Value, T>)
399+
setValue(&value);
400+
else
401+
setValue(Exiv2::toString(value));
404402
return *this;
405403
}
404+
#else
405+
template <typename T>
406+
std::enable_if_t<std::is_convertible<T, std::string>::value> operatorHelper(Xmpdatum* xmp, const T& value) {
407+
xmp->setValue(value);
408+
}
409+
410+
template <typename T>
411+
std::enable_if_t<std::is_base_of<Value, T>::value> operatorHelper(Xmpdatum* xmp, const T& value) {
412+
xmp->setValue(&value);
413+
}
406414

415+
template <typename T>
416+
std::enable_if_t<std::is_same<T, bool>::value> operatorHelper(Xmpdatum* xmp, const T& value) {
417+
xmp->setValue(value ? "True" : "False");
418+
}
419+
420+
template <typename T>
421+
std::enable_if_t<!(std::is_convertible<T, std::string>::value || std::is_base_of<Value, T>::value ||
422+
std::is_same<T, bool>::value)>
423+
operatorHelper(Xmpdatum* xmp, const T& value) {
424+
xmp->setValue(Exiv2::toString(value));
425+
}
426+
427+
template <typename T>
428+
Xmpdatum& Xmpdatum::operator=(const T& value) {
429+
operatorHelper(this, value);
430+
return *this;
431+
}
432+
#endif
407433
} // namespace Exiv2
408434

409435
#endif // EXIV2_XMP_EXIV2_HPP

src/xmp.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,6 @@ Xmpdatum::Xmpdatum(const XmpKey& key, const Value* pValue) : p_(std::make_unique
297297
Xmpdatum::Xmpdatum(const Xmpdatum& rhs) : p_(std::make_unique<Impl>(*rhs.p_)) {
298298
}
299299

300-
Xmpdatum& Xmpdatum::operator=(bool value) {
301-
return operator=(value ? "True" : "False");
302-
}
303-
304300
Xmpdatum& Xmpdatum::operator=(const Xmpdatum& rhs) {
305301
if (this == &rhs)
306302
return *this;
@@ -396,16 +392,6 @@ std::ostream& Xmpdatum::write(std::ostream& os, const ExifData*) const {
396392
return XmpProperties::printProperty(os, key(), value());
397393
}
398394

399-
Xmpdatum& Xmpdatum::operator=(const std::string& value) {
400-
setValue(value);
401-
return *this;
402-
}
403-
404-
Xmpdatum& Xmpdatum::operator=(const Value& value) {
405-
setValue(&value);
406-
return *this;
407-
}
408-
409395
void Xmpdatum::setValue(const Value* pValue) {
410396
p_->value_.reset();
411397
if (pValue)

0 commit comments

Comments
 (0)