From 28d21a4e1b5f3724f76b15aa93253aca84429686 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Thu, 28 Aug 2014 23:27:05 +0400 Subject: [PATCH] Smaller SizeType for static Array<>s --- libuavcan/include/uavcan/marshal/array.hpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/libuavcan/include/uavcan/marshal/array.hpp b/libuavcan/include/uavcan/marshal/array.hpp index 267925eb0d..003ee540db 100644 --- a/libuavcan/include/uavcan/marshal/array.hpp +++ b/libuavcan/include/uavcan/marshal/array.hpp @@ -36,11 +36,16 @@ enum ArrayMode { ArrayModeStatic, ArrayModeDynamic }; template class UAVCAN_EXPORT StaticArrayBase { +protected: + typedef IntegerSpec::Result, SignednessUnsigned, CastModeSaturate> RawSizeType; + public: enum { SizeBitLen = 0 }; - typedef unsigned SizeType; - SizeType size() const { return Size; } - SizeType capacity() const { return Size; } + + typedef typename StorageType::Type SizeType; + + SizeType size() const { return SizeType(Size); } + SizeType capacity() const { return SizeType(Size); } protected: StaticArrayBase() { } @@ -48,7 +53,7 @@ protected: SizeType validateRange(SizeType pos) const { - if (pos < Size) + if (pos < SizeType(Size)) { return pos; } @@ -209,10 +214,10 @@ public: const ValueType* begin() const { return data_; } ValueType* end() { return data_ + Base::size(); } const ValueType* end() const { return data_ + Base::size(); } - ValueType& front() { return at(0); } - const ValueType& front() const { return at(0); } - ValueType& back() { return at(Base::size() - 1); } - const ValueType& back() const { return at(Base::size() - 1); } + ValueType& front() { return at(0U); } + const ValueType& front() const { return at(0U); } + ValueType& back() { return at(SizeType(Base::size() - 1U)); } + const ValueType& back() const { return at(SizeType(Base::size() - 1U)); } /** * Performs standard lexicographical compare of the elements.