Skip to content

Commit

Permalink
replace clamping of scaled values by std::clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrauch committed Feb 26, 2024
1 parent 751c1e9 commit b1c68e5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand Down
12 changes: 6 additions & 6 deletions inc/msp/ByteVector.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef BYTE_VECTOR_HPP
#define BYTE_VECTOR_HPP

#include <algorithm>
#include <cstdint>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -91,12 +92,11 @@ class ByteVector : public std::vector<uint8_t> {
typename std::enable_if<std::is_arithmetic<T2>::value,
T2>::type* = nullptr>
bool pack(const T1 val, const T2 scale, const T2 offset = 0) {
const T1 tmp = (val + offset) * scale;
if(tmp <= std::numeric_limits<encoding_T>::min())
return pack(std::numeric_limits<encoding_T>::min());
else if(tmp >= std::numeric_limits<encoding_T>::max())
return pack(std::numeric_limits<encoding_T>::max());
return pack(static_cast<encoding_T>(tmp));
return pack(std::clamp(
static_cast<encoding_T>((val + offset) * scale),
std::numeric_limits<encoding_T>::min(),
std::numeric_limits<encoding_T>::max()
));
}

/**
Expand Down

0 comments on commit b1c68e5

Please sign in to comment.