mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-23 12:43:34 +08:00
Array<>::appendFormatted() + test
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <bitset>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
@@ -308,6 +309,7 @@ public:
|
||||
typedef typename Base::SizeType SizeType;
|
||||
|
||||
using Base::size;
|
||||
using Base::capacity;
|
||||
|
||||
enum { IsDynamic = ArrayMode == ArrayModeDynamic };
|
||||
enum { MaxSize = MaxSize_ };
|
||||
@@ -416,6 +418,44 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*
|
||||
* Formatting appender.
|
||||
* This method doesn't raise an overflow error; instead it silently truncates the data to fit the array capacity.
|
||||
*/
|
||||
template <typename A>
|
||||
void appendFormatted(const char* const format, const A value)
|
||||
{
|
||||
StaticAssert<Base::IsStringLike>::check();
|
||||
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
|
||||
|
||||
if (!format)
|
||||
{
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
// Add some hardcore runtime checks for the format string correctness?
|
||||
|
||||
ValueType* const ptr = Base::end();
|
||||
assert(capacity() >= size());
|
||||
const SizeType max_size = capacity() - size();
|
||||
|
||||
// We have one extra byte for the null terminator, hence +1
|
||||
const int ret = std::snprintf(reinterpret_cast<char*>(ptr), max_size + 1, format, value);
|
||||
|
||||
for (int i = 0; i < std::min(ret, int(max_size)); i++)
|
||||
{
|
||||
Base::grow();
|
||||
}
|
||||
if (ret < 0)
|
||||
{
|
||||
assert(0); // Likely an invalid format string
|
||||
(*this) += format; // So we print it as is in release builds
|
||||
}
|
||||
}
|
||||
|
||||
typedef ValueType value_type;
|
||||
typedef SizeType size_type;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user