Extract range_t to utility header.

This commit is contained in:
Jared Boone 2016-01-27 12:09:36 -08:00
parent c761d3aaa8
commit 018b54c711
2 changed files with 20 additions and 18 deletions

View File

@ -23,30 +23,14 @@
#include "hal.h" #include "hal.h"
#include "utility.hpp"
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
namespace portapack { namespace portapack {
namespace persistent_memory { namespace persistent_memory {
/* TODO: This is widely applicable. Factor this to somewhere useful. */
template<class T>
struct range_t {
const T minimum;
const T maximum;
const T& clip(const T& value) const {
return std::max(std::min(value, maximum), minimum);
}
void reset_if_outside(T& value, const T& reset_value) const {
if( (value < minimum ) ||
(value > maximum ) ) {
value = reset_value;
}
}
};
using tuned_frequency_range_t = range_t<rf::Frequency>; using tuned_frequency_range_t = range_t<rf::Frequency>;
constexpr tuned_frequency_range_t tuned_frequency_range { rf::tuning_range.min, rf::tuning_range.max }; constexpr tuned_frequency_range_t tuned_frequency_range { rf::tuning_range.min, rf::tuning_range.max };
constexpr rf::Frequency tuned_frequency_reset_value { 858750000 }; constexpr rf::Frequency tuned_frequency_reset_value { 858750000 };

View File

@ -25,6 +25,7 @@
#include <type_traits> #include <type_traits>
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
#include <algorithm>
#include <complex> #include <complex>
#include <memory> #include <memory>
@ -70,6 +71,23 @@ inline float magnitude_squared(const std::complex<float> c) {
return r2 + i2; return r2 + i2;
} }
template<class T>
struct range_t {
const T minimum;
const T maximum;
const T& clip(const T& value) const {
return std::max(std::min(value, maximum), minimum);
}
void reset_if_outside(T& value, const T& reset_value) const {
if( (value < minimum ) ||
(value > maximum ) ) {
value = reset_value;
}
}
};
namespace std { namespace std {
/*! Stephan T Lavavej (STL!) implementation of make_unique, which has been accepted into the C++14 standard. /*! Stephan T Lavavej (STL!) implementation of make_unique, which has been accepted into the C++14 standard.