<limits> from STL is not required

This commit is contained in:
Pavel Kirienko 2014-05-05 15:51:49 +04:00
parent 1a9e1d8202
commit 08cea4aacd
11 changed files with 162 additions and 22 deletions

View File

@ -7,7 +7,6 @@
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <uavcan/stdint.hpp>
#include <uavcan/util/templates.hpp>
#include <uavcan/impl_constants.hpp>
@ -41,8 +40,8 @@ class UAVCAN_EXPORT PoolManager : public IPoolAllocator, Noncopyable
{
const IPoolAllocator* const a = *static_cast<const IPoolAllocator* const*>(raw_a);
const IPoolAllocator* const b = *static_cast<const IPoolAllocator* const*>(raw_b);
const std::size_t a_size = a ? a->getBlockSize() : std::numeric_limits<std::size_t>::max();
const std::size_t b_size = b ? b->getBlockSize() : std::numeric_limits<std::size_t>::max();
const std::size_t a_size = a ? a->getBlockSize() : NumericTraits<std::size_t>::max();
const std::size_t b_size = b ? b->getBlockSize() : NumericTraits<std::size_t>::max();
if (a_size != b_size)
{
return (a_size > b_size) ? 1 : -1;

View File

@ -4,7 +4,6 @@
#pragma once
#include <limits>
#include <cmath>
#include <uavcan/stdint.hpp>
#include <uavcan/data_type.hpp>
@ -18,6 +17,8 @@
#endif
#if UAVCAN_CPP_VERSION < UAVCAN_CPP11
# include <math.h> // Needed for isfinite()
#else
# include <limits> // Assuming that in C++11 mode all standard headers are available
#endif
namespace uavcan
@ -43,8 +44,10 @@ class UAVCAN_EXPORT IEEE754Converter
IEEE754Converter();
public:
#if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
/// UAVCAN requires rounding to nearest for all float conversions
static std::float_round_style roundstyle() { return std::round_to_nearest; }
#endif
template <unsigned BitLen>
static typename IntegerSpec<BitLen, SignednessUnsigned, CastModeTruncate>::StorageType
@ -118,11 +121,17 @@ public:
typedef typename NativeFloatSelector<BitLen>::Type StorageType;
#if UAVCAN_CPP_VERSION < UAVCAN_CPP11
enum { IsExactRepresentation = (sizeof(StorageType) * 8 == BitLen) };
#else
enum { IsExactRepresentation = (sizeof(StorageType) * 8 == BitLen) && std::numeric_limits<StorageType>::is_iec559 };
#endif
using IEEE754Limits<BitLen>::max;
using IEEE754Limits<BitLen>::epsilon;
#if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
static std::float_round_style roundstyle() { return IEEE754Converter::roundstyle(); }
#endif
static int encode(StorageType value, ScalarCodec& codec, TailArrayOptimizationMode)
{
@ -180,11 +189,11 @@ private:
{
if (value > max())
{
value = std::numeric_limits<StorageType>::infinity();
value = NumericTraits<StorageType>::infinity();
}
else if (value < -max())
{
value = -std::numeric_limits<StorageType>::infinity();
value = -NumericTraits<StorageType>::infinity();
}
else
{

View File

@ -4,7 +4,6 @@
#pragma once
#include <limits>
#include <uavcan/stdint.hpp>
#include <uavcan/data_type.hpp>
#include <uavcan/util/templates.hpp>
@ -93,9 +92,9 @@ private:
{
StaticAssert<(BitLen <= (sizeof(StorageType) * 8))>::check();
// coverity[result_independent_of_operands : FALSE]
assert(max() <= std::numeric_limits<StorageType>::max());
assert(max() <= NumericTraits<StorageType>::max());
// coverity[result_independent_of_operands : FALSE]
assert(min() >= std::numeric_limits<StorageType>::min());
assert(min() >= NumericTraits<StorageType>::min());
}
public:

View File

@ -5,7 +5,6 @@
#pragma once
#include <cassert>
#include <limits>
#include <uavcan/stdint.hpp>
#include <uavcan/impl_constants.hpp>
#include <uavcan/util/templates.hpp>
@ -47,10 +46,10 @@ class UAVCAN_EXPORT ScalarCodec
convertByteOrder(uint8_t (&)[Size]) { }
template <unsigned BitLen, typename T>
static typename EnableIf<std::numeric_limits<T>::is_signed && ((sizeof(T) * 8) > BitLen)>::Type
static typename EnableIf<static_cast<bool>(NumericTraits<T>::IsSigned) && ((sizeof(T) * 8) > BitLen)>::Type
fixTwosComplement(T& value)
{
StaticAssert<std::numeric_limits<T>::is_integer>::check(); // Not applicable to floating point types
StaticAssert<NumericTraits<T>::IsInteger>::check(); // Not applicable to floating point types
if (value & (T(1) << (BitLen - 1))) // The most significant bit is set --> negative
{
value |= 0xFFFFFFFFFFFFFFFF & ~((T(1) << BitLen) - 1);
@ -58,7 +57,7 @@ class UAVCAN_EXPORT ScalarCodec
}
template <unsigned BitLen, typename T>
static typename EnableIf<!std::numeric_limits<T>::is_signed || ((sizeof(T) * 8) == BitLen)>::Type
static typename EnableIf<!static_cast<bool>(NumericTraits<T>::IsSigned) || ((sizeof(T) * 8) == BitLen)>::Type
fixTwosComplement(T&) { }
template <unsigned BitLen, typename T>
@ -77,7 +76,7 @@ class UAVCAN_EXPORT ScalarCodec
{
StaticAssert<((sizeof(T) * 8) >= BitLen)>::check();
StaticAssert<(BitLen <= BitStream::MaxBitsPerRW)>::check();
StaticAssert<std::numeric_limits<T>::is_signed ? (BitLen > 1) : 1>::check();
StaticAssert<static_cast<bool>(NumericTraits<T>::IsSigned) ? (BitLen > 1) : true>::check();
}
int encodeBytesImpl(uint8_t* bytes, unsigned bitlen);

View File

@ -4,7 +4,6 @@
#pragma once
#include <limits>
#include <uavcan/stdint.hpp>
#include <uavcan/impl_constants.hpp>
#include <uavcan/util/templates.hpp>
@ -29,7 +28,7 @@ protected:
}
public:
static D getInfinite() { return fromUSec(std::numeric_limits<int64_t>::max()); }
static D getInfinite() { return fromUSec(NumericTraits<int64_t>::max()); }
static D fromUSec(int64_t us)
{
@ -106,7 +105,7 @@ protected:
}
public:
static T getMax() { return fromUSec(std::numeric_limits<uint64_t>::max()); }
static T getMax() { return fromUSec(NumericTraits<uint64_t>::max()); }
static T fromUSec(uint64_t us)
{
@ -142,7 +141,7 @@ public:
{
if (uint64_t(usec_ + r.toUSec()) < usec_)
{
return fromUSec(std::numeric_limits<uint64_t>::max());
return fromUSec(NumericTraits<uint64_t>::max());
}
}
return fromUSec(usec_ + r.toUSec());

View File

@ -4,7 +4,6 @@
#pragma once
#include <limits>
#include <uavcan/stdint.hpp>
#include <uavcan/error.hpp>
#include <uavcan/transport/frame.hpp>

View File

@ -4,7 +4,19 @@
#pragma once
#include <climits>
#include <cmath>
#include <uavcan/impl_constants.hpp>
#include <uavcan/stdint.hpp>
#ifndef UAVCAN_CPP_VERSION
# error UAVCAN_CPP_VERSION
#endif
#if UAVCAN_CPP_VERSION < UAVCAN_CPP11
# include <float.h> // cfloat may not be available
#else
# include <cfloat> // C++11 mode assumes that all standard headers are available
#endif
namespace uavcan
{
@ -241,4 +253,115 @@ bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
return true;
}
/**
* Numeric traits, like std::numeric_limits<>
*/
template <typename T>
struct UAVCAN_EXPORT NumericTraits;
/// 8 bit
template <>
struct UAVCAN_EXPORT NumericTraits<int8_t>
{
enum { IsSigned = 1 };
enum { IsInteger = 1 };
static int8_t max() { return 127; }
static int8_t min() { return -max() - 1; }
};
template <>
struct UAVCAN_EXPORT NumericTraits<uint8_t>
{
enum { IsSigned = 0 };
enum { IsInteger = 1 };
static uint8_t max() { return 0xFFU; }
static uint8_t min() { return 0; }
};
/// 16 bit
template <>
struct UAVCAN_EXPORT NumericTraits<int16_t>
{
enum { IsSigned = 1 };
enum { IsInteger = 1 };
static int16_t max() { return 32767; }
static int16_t min() { return -max() - 1; }
};
template <>
struct UAVCAN_EXPORT NumericTraits<uint16_t>
{
enum { IsSigned = 0 };
enum { IsInteger = 1 };
static uint16_t max() { return 0xFFFFU; }
static uint16_t min() { return 0; }
};
/// 32 bit
template <>
struct UAVCAN_EXPORT NumericTraits<int32_t>
{
enum { IsSigned = 1 };
enum { IsInteger = 1 };
static int32_t max() { return 2147483647L; }
static int32_t min() { return -max() - 1; }
};
template <>
struct UAVCAN_EXPORT NumericTraits<uint32_t>
{
enum { IsSigned = 0 };
enum { IsInteger = 1 };
static uint32_t max() { return 0xFFFFFFFFUL; }
static uint32_t min() { return 0UL; }
};
/// 64 bit
template <>
struct UAVCAN_EXPORT NumericTraits<int64_t>
{
enum { IsSigned = 1 };
enum { IsInteger = 1 };
static int64_t max() { return 9223372036854775807LL; }
static int64_t min() { return -max() - 1; }
};
template <>
struct UAVCAN_EXPORT NumericTraits<uint64_t>
{
enum { IsSigned = 0 };
enum { IsInteger = 1 };
static uint64_t max() { return 0xFFFFFFFFFFFFFFFFULL; }
static uint64_t min() { return 0; }
};
/// float
template <>
struct UAVCAN_EXPORT NumericTraits<float>
{
enum { IsSigned = 1 };
enum { IsInteger = 0 };
static float max() { return FLT_MAX; }
static float min() { return FLT_MIN; }
static float infinity() { return INFINITY; }
};
/// double
template <>
struct UAVCAN_EXPORT NumericTraits<double>
{
enum { IsSigned = 1 };
enum { IsInteger = 0 };
static double max() { return DBL_MAX; }
static double min() { return DBL_MIN; }
static double infinity() { return static_cast<double>(INFINITY) * static_cast<double>(INFINITY); }
};
/// long double
template <>
struct UAVCAN_EXPORT NumericTraits<long double>
{
enum { IsSigned = 1 };
enum { IsInteger = 0 };
static long double max() { return LDBL_MAX; }
static long double min() { return LDBL_MIN; }
static long double infinity() { return static_cast<long double>(INFINITY) * static_cast<long double>(INFINITY); }
};
}

View File

@ -10,6 +10,10 @@
# error UAVCAN_CPP_VERSION
#endif
#if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
# include <limits>
#endif
#undef signbit
#undef isnan
#undef isinf
@ -47,7 +51,7 @@ static inline bool isinf(T arg)
#if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
return std::isinf(arg);
#else
return arg == std::numeric_limits<T>::infinity() || arg == -std::numeric_limits<T>::infinity();
return arg == NumericTraits<T>::infinity() || arg == -NumericTraits<T>::infinity();
#endif
}
@ -94,12 +98,20 @@ float IEEE754Converter::halfToNativeNonIeee(uint16_t value)
unsigned abs = value & 0x7FFFU;
if (abs > 0x7C00U)
{
#if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
out = std::numeric_limits<float>::has_quiet_NaN ? std::numeric_limits<float>::quiet_NaN() : 0.0F;
#else
out = nanf("");
#endif
}
else if (abs == 0x7C00U)
{
#if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
out = std::numeric_limits<float>::has_infinity ?
std::numeric_limits<float>::infinity() : std::numeric_limits<float>::max();
#else
out = NumericTraits<float>::infinity();
#endif
}
else if (abs > 0x3FFU)
{

View File

@ -6,7 +6,6 @@
#include <uavcan/transport/can_io.hpp>
#include <uavcan/debug.hpp>
#include <cassert>
#include <limits>
namespace uavcan
{
@ -100,7 +99,7 @@ CanTxQueue::~CanTxQueue()
void CanTxQueue::registerRejectedFrame()
{
if (rejected_frames_cnt_ < std::numeric_limits<uint32_t>::max())
if (rejected_frames_cnt_ < NumericTraits<uint32_t>::max())
{
rejected_frames_cnt_++;
}

View File

@ -3,6 +3,7 @@
*/
#include <gtest/gtest.h>
#include <limits>
#include <uavcan/marshal/types.hpp>
#include <uavcan/transport/transfer_buffer.hpp>

View File

@ -3,6 +3,7 @@
*/
#include <gtest/gtest.h>
#include <limits>
#include <uavcan/marshal/scalar_codec.hpp>
#include <uavcan/transport/transfer_buffer.hpp>