Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a bunch of static_asserts to templates #3212

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/exiv2/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,9 @@ EXIV2API Rational floatToRationalCast(float f);
}
@endcode
*/
template <typename T, typename K, int N>
template <typename T, typename K, size_t N>
const T* find(T (&src)[N], const K& key) {
static_assert(N > 0, "Passed zero length find");
auto rc = std::find(src, src + N, key);
return rc == src + N ? nullptr : rc;
}
Expand Down
1 change: 1 addition & 0 deletions src/easyaccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using namespace Exiv2;

template <size_t N, const char* const (&keys)[N]>
ExifData::const_iterator findMetadatum(const ExifData& ed) {
static_assert(N > 0, "Passed zero length findMetadatum");
for (const auto& k : keys) {
auto pos = ed.findKey(ExifKey(k));
if (pos != ed.end())
Expand Down
3 changes: 2 additions & 1 deletion src/pentaxmn_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class PentaxMakerNote {
@brief Print function to translate Pentax "combi-values" to a description
by looking up a reference table.
*/
template <int N, const TagDetails (&array)[N], int count, int ignoredcount, int ignoredcountmax>
template <size_t N, const TagDetails (&array)[N], int count, int ignoredcount, int ignoredcountmax>
std::ostream& printCombiTag(std::ostream& os, const Value& value, const ExifData* metadata) {
static_assert(N > 0, "Passed zero length printCombiTag");
std::ios::fmtflags f(os.flags());
if ((value.count() != count &&
(value.count() < (count + ignoredcount) || value.count() > (count + ignoredcountmax))) ||
Expand Down
15 changes: 13 additions & 2 deletions src/tags_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct TagDetails {

//! Comparison operator for use with the find template
bool operator==(int64_t key) const {
return val_ == key;
return key == val_;
}
}; // struct TagDetails

Expand Down Expand Up @@ -89,6 +89,7 @@ struct TagVocabulary {
*/
template <size_t N, const StringTagDetails (&array)[N]>
std::ostream& printTagString(std::ostream& os, const std::string& value, const ExifData*) {
static_assert(N > 0, "Passed zero length printTagString");
if (auto td = Exiv2::find(array, value)) {
os << exvGettext(td->label_);
} else {
Expand All @@ -103,6 +104,7 @@ std::ostream& printTagString(std::ostream& os, const std::string& value, const E
*/
template <size_t N, const StringTagDetails (&array)[N]>
std::ostream& printTagString(std::ostream& os, const Value& value, const ExifData* data) {
static_assert(N > 0, "Passed zero length printTagString");
return printTagString<N, array>(os, value.toString(0), data);
}

Expand All @@ -115,6 +117,7 @@ std::ostream& printTagString(std::ostream& os, const Value& value, const ExifDat
*/
template <size_t N, const StringTagDetails (&array)[N]>
std::ostream& printTagString2(std::ostream& os, const Value& value, const ExifData* data) {
static_assert(N > 0, "Passed zero length printTagString2");
if (value.count() < 2)
return os << "(" << value << ")";
std::string temp = value.toString(0) + " " + value.toString(1);
Expand All @@ -130,6 +133,7 @@ std::ostream& printTagString2(std::ostream& os, const Value& value, const ExifDa
*/
template <size_t N, const StringTagDetails (&array)[N]>
std::ostream& printTagString4(std::ostream& os, const Value& value, const ExifData* data) {
static_assert(N > 0, "Passed zero length printTagString4");
if (value.count() < 4)
return os << "(" << value << ")";
std::string temp = value.toString(0) + " " + value.toString(1) + " " + value.toString(2) + " " + value.toString(3);
Expand All @@ -145,6 +149,7 @@ std::ostream& printTagString4(std::ostream& os, const Value& value, const ExifDa
*/
template <size_t N, const TagDetails (&array)[N]>
std::ostream& printTagNoError(std::ostream& os, const int64_t value, const ExifData*) {
static_assert(N > 0, "Passed zero length printTagNoError");
if (auto td = Exiv2::find(array, value)) {
os << exvGettext(td->label_);
} else {
Expand All @@ -159,6 +164,7 @@ std::ostream& printTagNoError(std::ostream& os, const int64_t value, const ExifD
*/
template <size_t N, const TagDetails (&array)[N]>
std::ostream& printTagNoError(std::ostream& os, const Value& value, const ExifData* data) {
static_assert(N > 0, "Passed zero length printTagNoError");
return printTagNoError<N, array>(os, value.toInt64(), data);
}

Expand All @@ -171,6 +177,7 @@ std::ostream& printTagNoError(std::ostream& os, const Value& value, const ExifDa
*/
template <size_t N, const TagDetails (&array)[N]>
std::ostream& printTag(std::ostream& os, const int64_t value, const ExifData*) {
static_assert(N > 0, "Passed zero length printTag");
if (auto td = Exiv2::find(array, value)) {
os << exvGettext(td->label_);
} else {
Expand All @@ -185,6 +192,7 @@ std::ostream& printTag(std::ostream& os, const int64_t value, const ExifData*) {
*/
template <size_t N, const TagDetails (&array)[N]>
std::ostream& printTag(std::ostream& os, const Value& value, const ExifData* data) {
static_assert(N > 0, "Passed zero length printTag");
return printTag<N, array>(os, value.toInt64(), data);
}

Expand All @@ -197,8 +205,9 @@ std::ostream& printTag(std::ostream& os, const Value& value, const ExifData* dat
*/
template <size_t N, const TagDetailsBitmask (&array)[N]>
std::ostream& printTagBitmask(std::ostream& os, const Value& value, const ExifData*) {
static_assert(N > 0, "Passed zero length printTag");
const auto val = value.toUint32();
if (val == 0 && N > 0) {
if (val == 0) {
auto [mask, label] = *array;
if (mask == 0)
return os << exvGettext(label);
Expand Down Expand Up @@ -288,6 +297,7 @@ std::ostream& printTagBitlistAllLE(std::ostream& os, const Value& value, const E
*/
template <size_t N, const TagVocabulary (&array)[N]>
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 << exvGettext(td->label_);
} else {
Expand All @@ -301,6 +311,7 @@ std::ostream& printTagVocabulary(std::ostream& os, const Value& value, const Exi

template <size_t N, const TagVocabulary (&array)[N]>
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;
Expand Down
2 changes: 2 additions & 0 deletions src/tiffcomposite_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,7 @@ TiffComponent::UniquePtr newTiffSubIfd(uint16_t tag, IfdId group) {
//! Function to create and initialize a new binary array entry
template <const ArrayCfg& arrayCfg, size_t N, const ArrayDef (&arrayDef)[N]>
TiffComponent::UniquePtr newTiffBinaryArray0(uint16_t tag, IfdId group) {
static_assert(N > 0, "Passed zero length newTiffBinaryArray0");
return std::make_unique<TiffBinaryArray>(tag, group, arrayCfg, arrayDef, N);
}

Expand All @@ -1514,6 +1515,7 @@ TiffComponent::UniquePtr newTiffBinaryArray1(uint16_t tag, IfdId group) {
//! Function to create and initialize a new complex binary array entry
template <size_t N, const ArraySet (&arraySet)[N], CfgSelFct cfgSelFct>
TiffComponent::UniquePtr newTiffBinaryArray2(uint16_t tag, IfdId group) {
static_assert(N > 0, "Passed zero length newTiffBinaryArray2");
return std::make_unique<TiffBinaryArray>(tag, group, arraySet, N, cfgSelFct);
}

Expand Down
Loading