StaticArray specialization for std::bitset<>

This commit is contained in:
Pavel Kirienko
2014-02-22 21:37:31 +04:00
parent ac6456695e
commit 056791619e
2 changed files with 41 additions and 2 deletions
@@ -4,6 +4,7 @@
#pragma once
#include <bitset>
#include <algorithm>
#include <stdexcept>
#include <uavcan/internal/impl_constants.hpp>
@@ -115,6 +116,44 @@ public:
typedef const ValueType* const_iterator;
};
/// Special case - bit array
template <unsigned int Size_, CastMode CastMode>
class StaticArray<IntegerSpec<1, SignednessUnsigned, CastMode>, Size_ > : public std::bitset<Size_>
{
public:
enum { IsDynamic = 0 };
enum { Size = Size_ };
typedef IntegerSpec<1, SignednessUnsigned, CastMode> RawValueType;
typedef typename StorageType<RawValueType>::Type ValueType;
static int encode(const StaticArray<RawValueType, Size>& array, ScalarCodec& codec)
{
for (std::size_t i = 0; i < Size; i++)
{
const int res = RawValueType::encode(bool(array[i]), codec);
if (res <= 0)
return res;
}
return 1;
}
static int decode(StaticArray<RawValueType, Size>& array, ScalarCodec& codec)
{
for (std::size_t i = 0; i < Size; i++)
{
ValueType value = 0;
const int res = RawValueType::decode(value, codec);
array[i] = value;
if (res <= 0)
return res;
}
return 1;
}
};
template <typename T> class StaticArray<T, 0>; // Invalid instantiation
}
+2 -2
View File
@@ -96,8 +96,8 @@ TEST(StaticArray, Basic)
{
ASSERT_EQ(0, it->a);
ASSERT_EQ(0, it->b);
for (A3::ValueType::C::const_iterator it2 = it->c.begin(); it2 != it->c.end(); ++it2)
ASSERT_EQ(0, *it2);
for (int i = 0; i < 8; i++)
ASSERT_EQ(0, it->c[i]);
}
/*