mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-23 16:27:35 +08:00
StaticArray specialization for std::bitset<>
This commit is contained in:
@@ -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
|
||||
|
||||
}
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user