Automated style fixes in order to bring the code a little bit closer to compliance with MISRA C++ rules. No changes in logic.

This commit is contained in:
Pavel Kirienko
2014-03-25 03:04:36 +04:00
parent cce657e1fe
commit 0cc627016a
64 changed files with 777 additions and 283 deletions
+74 -5
View File
@@ -42,7 +42,9 @@ protected:
SizeType validateRange(SizeType pos) const
{
if (pos < Size)
{
return pos;
}
#if UAVCAN_EXCEPTIONS
throw std::out_of_range("uavcan::Array");
#else
@@ -71,7 +73,9 @@ protected:
SizeType validateRange(SizeType pos) const
{
if (pos < size_)
{
return pos;
}
#if UAVCAN_EXCEPTIONS
throw std::out_of_range("uavcan::Array");
#else
@@ -83,15 +87,21 @@ protected:
void grow()
{
if (size_ >= MaxSize)
{
validateRange(MaxSize); // Will throw, assert() or do nothing
}
else
{
size_++;
}
}
void shrink()
{
if (size_ > 0)
{
size_--;
}
}
public:
@@ -122,8 +132,8 @@ class ArrayImpl : public Select<ArrayMode == ArrayModeDynamic,
public:
enum
{
IsStringLike = IsIntegerSpec<T>::Result && (T::MaxBitLen == 8 || T::MaxBitLen == 7)
&& (ArrayMode == ArrayModeDynamic)
IsStringLike = IsIntegerSpec<T>::Result && (T::MaxBitLen == 8 || T::MaxBitLen == 7) &&
(ArrayMode == ArrayModeDynamic)
};
private:
@@ -134,7 +144,9 @@ private:
typename EnableIf<sizeof(U(0) == U())>::Type initialize(int)
{
if (ArrayMode != ArrayModeDynamic)
{
std::fill(data_, data_ + MaxSize, U());
}
}
template <typename> void initialize(...) { }
@@ -233,7 +245,9 @@ class Array : public ArrayImpl<T, ArrayMode, MaxSize_>
const bool last_item = i == (size() - 1);
const int res = RawValueType::encode(Base::at(i), codec, last_item ? tao_mode : TailArrayOptDisabled);
if (res <= 0)
{
return res;
}
}
return 1;
}
@@ -246,10 +260,14 @@ class Array : public ArrayImpl<T, ArrayMode, MaxSize_>
{
const int res_sz = Base::RawSizeType::encode(size(), codec, TailArrayOptDisabled);
if (res_sz <= 0)
{
return res_sz;
}
}
if (size() == 0)
{
return 1;
}
return encodeImpl(codec, self_tao_enabled ? TailArrayOptDisabled : tao_mode, FalseType());
}
@@ -262,7 +280,9 @@ class Array : public ArrayImpl<T, ArrayMode, MaxSize_>
ValueType value; // TODO: avoid extra copy
const int res = RawValueType::decode(value, codec, last_item ? tao_mode : TailArrayOptDisabled);
if (res <= 0)
{
return res;
}
Base::at(i) = value;
}
return 1;
@@ -279,11 +299,17 @@ class Array : public ArrayImpl<T, ArrayMode, MaxSize_>
ValueType value;
const int res = RawValueType::decode(value, codec, TailArrayOptDisabled);
if (res < 0)
{
return res;
}
if (res == 0) // Success: End of stream reached (even if zero items were read)
{
return 1;
}
if (size() == MaxSize_) // Error: Max array length reached, but the end of stream is not
{
return -1;
}
push_back(value);
}
}
@@ -292,12 +318,18 @@ class Array : public ArrayImpl<T, ArrayMode, MaxSize_>
typename StorageType<typename Base::RawSizeType>::Type sz = 0;
const int res_sz = Base::RawSizeType::decode(sz, codec, TailArrayOptDisabled);
if (res_sz <= 0)
{
return res_sz;
}
if ((sz > 0) && ((sz - 1u) > (MaxSize_ - 1u))) // -Werror=type-limits
{
return -1;
}
resize(sz);
if (sz == 0)
{
return 1;
}
return decodeImpl(codec, tao_mode, FalseType());
}
assert(0); // Unreachable
@@ -347,13 +379,17 @@ public:
{
unsigned int cnt = new_size - size();
while (cnt--)
{
push_back(filler);
}
}
else if (new_size < size())
{
unsigned int cnt = size() - new_size;
while (cnt--)
{
pop_back();
}
}
}
@@ -370,17 +406,25 @@ public:
operator==(const R& rhs) const
{
if (size() != rhs.size())
{
return false;
}
for (SizeType i = 0; i < size(); i++) // Bitset does not have iterators
{
if (!(Base::at(i) == rhs[i]))
{
return false;
}
}
return true;
}
bool operator==(const char* ch) const
{
if (ch == NULL)
{
return false;
}
return std::strcmp(Base::c_str(), ch) == 0;
}
@@ -395,9 +439,13 @@ public:
StaticAssert<IsDynamic>::check();
Base::clear();
if (ch == NULL)
{
handleFatalError("Null pointer in Array<>::operator=(const char*)");
}
while (*ch)
{
push_back(*ch++);
}
return *this;
}
@@ -406,9 +454,13 @@ public:
StaticAssert<Base::IsStringLike>::check();
StaticAssert<IsDynamic>::check();
if (ch == NULL)
{
handleFatalError("Null pointer in Array<>::operator+=(const char*)");
}
while (*ch)
{
push_back(*ch++);
}
return *this;
}
@@ -420,7 +472,9 @@ public:
SizeType, typename Rhs::SizeType>::Result CommonSizeType;
StaticAssert<IsDynamic>::check();
for (CommonSizeType i = 0; i < rhs.size(); i++)
{
push_back(rhs[i]);
}
return *this;
}
@@ -435,7 +489,7 @@ public:
StaticAssert<IsDynamic>::check();
StaticAssert<sizeof(A() == A(0))>::check(); // This check allows to weed out most non-trivial types
StaticAssert<sizeof(A) <= sizeof(long double)>::check();// Another stupid check to catch non-trivial types
StaticAssert<sizeof(A) <= sizeof(long double)>::check(); // Another stupid check to catch non-trivial types
if (!format)
{
@@ -487,11 +541,17 @@ class YamlStreamer<Array<T, ArrayMode, MaxSize> >
static bool isNiceCharacter(int c)
{
if (c >= 32 && c <= 126)
{
return true;
}
static const char Good[] = {'\n', '\r', '\t'};
for (unsigned int i = 0; i < sizeof(Good) / sizeof(Good[0]); i++)
{
if (Good[i] == c)
{
return true;
}
}
return false;
}
@@ -503,7 +563,9 @@ class YamlStreamer<Array<T, ArrayMode, MaxSize> >
{
YamlStreamer<T>::stream(s, array.at(i), 0);
if ((i + 1) < array.size())
{
s << ", ";
}
}
s << ']';
}
@@ -522,14 +584,18 @@ class YamlStreamer<Array<T, ArrayMode, MaxSize> >
{
nibbles[i] += '0';
if (nibbles[i] > '9')
{
nibbles[i] += 'A' - '9' - 1;
}
}
s << "\\x" << nibbles[0] << nibbles[1];
}
else
{
if (c == '"' || c == '\\') // YAML requires to escape these two
{
s << '\\';
}
s << char(c);
}
}
@@ -582,7 +648,9 @@ class YamlStreamer<Array<T, ArrayMode, MaxSize> >
{
s << '\n';
for (int pos = 0; pos < level; pos++)
{
s << " ";
}
s << "- ";
YamlStreamer<T>::stream(s, array.at(i), level + 1);
}
@@ -593,8 +661,9 @@ public:
static void stream(Stream& s, const ArrayType& array, int level)
{
typedef typename Select<ArrayType::IsStringLike, SelectorStringLike,
typename Select<IsPrimitiveType<typename ArrayType::RawValueType>::Result, SelectorPrimitives,
SelectorObjects>::Result >::Result Type;
typename Select<IsPrimitiveType<typename ArrayType::RawValueType>::Result,
SelectorPrimitives,
SelectorObjects>::Result >::Result Type;
genericStreamImpl(s, array, level, Type());
}
};
@@ -43,9 +43,9 @@ public:
};
BitStream(ITransferBuffer& buf)
: buf_(buf)
, bit_offset_(0)
, byte_cache_(0)
: buf_(buf)
, bit_offset_(0)
, byte_cache_(0)
{
StaticAssert<sizeof(uint8_t) == 1>::check();
}
@@ -39,7 +39,9 @@ class CharArrayFormatter
if (std::is_same<T, char>())
{
if (array_.size() != array_.capacity())
{
array_.push_back(value);
}
}
else if (std::is_signed<T>())
{
@@ -67,7 +69,7 @@ public:
typedef ArrayType_ ArrayType;
CharArrayFormatter(ArrayType& array)
: array_(array)
: array_(array)
{ }
ArrayType& getArray() { return array_; }
@@ -78,7 +80,7 @@ public:
writeValue(text);
}
template<typename T, typename... Args>
template <typename T, typename... Args>
void write(const char* s, T value, Args... args)
{
while (s && *s)
@@ -105,7 +107,7 @@ public:
typedef ArrayType_ ArrayType;
CharArrayFormatter(ArrayType& array)
: array_(array)
: array_(array)
{ }
ArrayType& getArray() { return array_; }
@@ -124,9 +124,13 @@ public:
{
// cppcheck-suppress duplicateExpression
if (CastMode == CastModeSaturate)
{
saturate(value);
}
else
{
truncate(value);
}
return codec.encode<BitLen>(IEEE754Converter::toIeee<BitLen>(value));
}
@@ -135,7 +139,9 @@ public:
typename IntegerSpec<BitLen, SignednessUnsigned, CastModeTruncate>::StorageType ieee = 0;
const int res = codec.decode<BitLen>(ieee);
if (res <= 0)
{
return res;
}
out_value = IEEE754Converter::toNative<BitLen>(ieee);
return res;
}
@@ -149,9 +155,13 @@ private:
if (!IsExactRepresentation && isfinite(value))
{
if (value > max())
{
value = max();
}
else if (value < -max())
{
value = -max();
}
}
}
@@ -161,9 +171,13 @@ private:
if (!IsExactRepresentation && isfinite(value))
{
if (value > max())
{
value = std::numeric_limits<StorageType>::infinity();
}
else if (value < -max())
{
value = -std::numeric_limits<StorageType>::infinity();
}
}
}
};
@@ -64,9 +64,13 @@ private:
static void saturate(StorageType& value)
{
if (value > max())
{
value = max();
}
else if (value <= min()) // 'Less or Equal' allows to suppress compiler warning on unsigned types
{
value = min();
}
}
static void truncate(StorageType& value) { value &= mask(); }
@@ -88,9 +92,13 @@ public:
validate();
// cppcheck-suppress duplicateExpression
if (CastMode == CastModeSaturate)
{
saturate(value);
}
else
{
truncate(value);
}
return codec.encode<BitLen>(value);
}
@@ -111,10 +119,16 @@ class IntegerSpec<0, Signedness, CastMode>; // Invalid instantiation
template <typename T>
struct IsIntegerSpec { enum { Result = 0 }; };
struct IsIntegerSpec
{
enum { Result = 0 };
};
template <unsigned int BitLen, Signedness Signedness, CastMode CastMode>
struct IsIntegerSpec<IntegerSpec<BitLen, Signedness, CastMode> > { enum { Result = 1 }; };
struct IsIntegerSpec<IntegerSpec<BitLen, Signedness, CastMode> >
{
enum { Result = 1 };
};
template <unsigned int BitLen, Signedness Signedness, CastMode CastMode>
@@ -128,7 +142,7 @@ struct YamlStreamer<IntegerSpec<BitLen, Signedness, CastMode> >
{
// Get rid of character types - we want its integer representation, not ASCII code
typedef typename Select<(sizeof(StorageType) >= sizeof(int)), StorageType,
typename Select<RawType::IsSigned, int, unsigned int>::Result >::Result TempType;
typename Select<RawType::IsSigned, int, unsigned int>::Result >::Result TempType;
s << TempType(value);
}
};
@@ -45,9 +45,10 @@ class ScalarCodec
* It is likely to be OK anyway, so feel free to remove this assert() as needed.
*/
assert(big_endian == false);
if (big_endian)
{
swapByteOrder(bytes);
}
}
template <int BitLen, int Size>
@@ -62,9 +63,10 @@ class ScalarCodec
fixTwosComplement(T& value)
{
StaticAssert<std::numeric_limits<T>::is_integer>::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);
}
}
template <int BitLen, typename T>
@@ -98,7 +100,7 @@ class ScalarCodec
public:
ScalarCodec(BitStream& stream)
: stream_(stream)
: stream_(stream)
{ }
template <int BitLen, typename T>
@@ -111,14 +113,13 @@ public:
uint8_t bytes[sizeof(T)];
} byte_union;
byte_union.value = value;
clearExtraBits<BitLen>(byte_union.value);
convertByteOrder<BitLen>(byte_union.bytes);
// Underlying stream class assumes that more significant bits have lower index, so we need to shift some.
if (BitLen % 8)
{
byte_union.bytes[BitLen / 8] <<= (8 - (BitLen % 8)) & 7;
}
return stream_.write(byte_union.bytes, BitLen);
}
@@ -135,14 +136,15 @@ public:
const int read_res = stream_.read(byte_union.bytes, BitLen);
if (read_res <= 0)
{
return read_res;
}
if (BitLen % 8)
{
byte_union.bytes[BitLen / 8] >>= (8 - (BitLen % 8)) & 7; // As in encode(), vice versa
}
convertByteOrder<BitLen>(byte_union.bytes);
fixTwosComplement<BitLen>(byte_union.value);
value = byte_union.value;
return read_res;
}
+16 -4
View File
@@ -15,17 +15,29 @@ enum TailArrayOptimizationMode { TailArrayOptDisabled, TailArrayOptEnabled };
template <unsigned long Num>
struct IntegerBitLen { enum { Result = 1 + IntegerBitLen<(Num >> 1)>::Result }; };
struct IntegerBitLen
{
enum { Result = 1 + IntegerBitLen<(Num >> 1)>::Result };
};
template <>
struct IntegerBitLen<0> { enum { Result = 0 }; };
struct IntegerBitLen<0>
{
enum { Result = 0 };
};
template <unsigned long BitLen>
struct BitLenToByteLen { enum { Result = (BitLen + 7) / 8 }; };
struct BitLenToByteLen
{
enum { Result = (BitLen + 7) / 8 };
};
template <typename T, typename Enable = void>
struct StorageType { typedef T Type; };
struct StorageType
{
typedef T Type;
};
template <typename T>
struct StorageType<T, typename EnableIfType<typename T::StorageType>::Type>