Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
create check_max_boundary and inline boundary checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Carneiro committed Jul 10, 2014
1 parent 423ad04 commit b00746c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gamgee/base_quals.cpp
Expand Up @@ -77,7 +77,7 @@ BaseQuals& BaseQuals::operator=(BaseQuals&& other) noexcept {
* @return base quality at the specified index as an unsigned byte
*/
uint8_t BaseQuals::operator[](const uint32_t index) const {
utils::check_boundaries(index, m_num_quals);
utils::check_max_boundary(index, m_num_quals-1);
return m_quals[index];
}

Expand All @@ -87,7 +87,7 @@ uint8_t BaseQuals::operator[](const uint32_t index) const {
* @return base quality at the specified index as an unsigned byte
*/
uint8_t& BaseQuals::operator[](const uint32_t index) {
utils::check_boundaries(index, m_num_quals);
utils::check_max_boundary(index, m_num_quals-1);
return m_quals[index];
}

Expand Down
9 changes: 0 additions & 9 deletions gamgee/utils/utils.cpp
Expand Up @@ -63,14 +63,5 @@ std::vector<std::string> hts_string_array_to_vector(const char * const * const s
return result;
}

void check_boundaries(const int index, const int max_index, const int min_index) {
if (index < min_index || index > max_index) {
std::stringstream error_message {}; ///< @todo update this to auto when gcc-4.9 is available on travis-ci
error_message << "The index requested is out of range: " << index << " the maximum index is " << max_index << " and the minimum is " << min_index << std::endl;
throw std::out_of_range(error_message.str());
}
}


}
}
23 changes: 22 additions & 1 deletion gamgee/utils/utils.h
Expand Up @@ -66,7 +66,28 @@ std::vector<std::string> hts_string_array_to_vector(const char * const * const s
* @param min_index the minimum valid index (typically 0 for array boundaries, but can be arbitrary for pointer checks)
* @exception throws an out_of_bounds exception if index is out of limits
*/
void check_boundaries(const int index, const int max_index, const int min_index = 0);
inline void check_boundaries(const int index, const int max_index, const int min_index = 0) {

This comment has been minimized.

Copy link
@droazen

droazen Jul 10, 2014

Contributor

I wonder if the compiler will actually inline these. I know it's free to ignore inline requests if the function is too long...

if (index < min_index || index > max_index) {
std::stringstream error_message {}; ///< @todo update this to auto when gcc-4.9 is available on travis-ci
error_message << "The index requested is out of range: " << index << " the maximum index is " << max_index << " and the minimum is " << min_index << std::endl;
throw std::out_of_range(error_message.str());
}
}


/**
* @brief checks that an index is greater than max_index
* @param index the index to check
* @param max_index the maximum valid index
* @exception throws an out_of_bounds exception if index is out of limits
*/
inline void check_max_boundary(const uint32_t index, const uint32_t max_index) {
if (index > max_index) {
std::stringstream error_message {}; ///< @todo update this to auto when gcc-4.9 is available on travis-ci
error_message << "The index requested is out of range: " << index << " the maximum index is " << max_index << std::endl;
throw std::out_of_range(error_message.str());
}
}


} // end utils namespace
Expand Down

0 comments on commit b00746c

Please sign in to comment.