Skip to content

Commit

Permalink
Use a switch statement in common::sample_type_to_string().
Browse files Browse the repository at this point in the history
  • Loading branch information
aliddell committed Jul 9, 2024
1 parent a4c1442 commit eef0db7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
32 changes: 13 additions & 19 deletions src/common/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,22 @@ common::bytes_per_chunk(const std::vector<Dimension>& dimensions,
return n_bytes;
}

const char*
common::sample_type_to_dtype(SampleType t)

{
static const char* table[] = { "u1", "u2", "i1", "i2",
"f4", "u2", "u2", "u2" };
if (t < countof(table)) {
return table[t];
} else {
throw std::runtime_error("Invalid sample type.");
}
}

const char*
common::sample_type_to_string(SampleType t) noexcept
{
static const char* table[] = { "u8", "u16", "i8", "i16",
"f32", "u16", "u16", "u16" };
if (t < countof(table)) {
return table[t];
} else {
return "unrecognized pixel type";
switch (t) {
case SampleType_u8:
return "u8";
case SampleType_u16:
return "u16";
case SampleType_i8:
return "i8";
case SampleType_i16:
return "i16";
case SampleType_f32:
return "f32";
default:
return "unrecognized pixel type";
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/common/utilities.hh
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ size_t
bytes_per_chunk(const std::vector<Dimension>& dimensions,
const SampleType& dtype);

/// @brief Get the Zarr dtype for a given SampleType.
/// @param t An enumerated sample type.
/// @throw std::runtime_error if @par t is not a valid SampleType.
/// @return A representation of the SampleType @par t expected by a Zarr reader.
const char*
sample_type_to_dtype(SampleType t);

/// @brief Get a string representation of the SampleType enum.
/// @param t An enumerated sample type.
/// @return A human-readable representation of the SampleType @par t.
Expand Down

0 comments on commit eef0db7

Please sign in to comment.