2021-10-30 15:01:50 +00:00
|
|
|
#ifndef OPENMW_COMPONENTS_SERIALIZATION_SIZEACCUMULATOR_H
|
|
|
|
#define OPENMW_COMPONENTS_SERIALIZATION_SIZEACCUMULATOR_H
|
2021-10-22 22:06:10 +00:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2021-10-30 15:01:50 +00:00
|
|
|
namespace Serialization
|
2021-10-22 22:06:10 +00:00
|
|
|
{
|
|
|
|
class SizeAccumulator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SizeAccumulator() = default;
|
|
|
|
|
|
|
|
SizeAccumulator(const SizeAccumulator&) = delete;
|
|
|
|
|
|
|
|
std::size_t value() const { return mValue; }
|
|
|
|
|
|
|
|
template <class Format, class T>
|
|
|
|
void operator()(Format&& format, const T& value)
|
|
|
|
{
|
2021-11-08 18:25:42 +00:00
|
|
|
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T>)
|
2021-10-22 22:06:10 +00:00
|
|
|
mValue += sizeof(T);
|
|
|
|
else
|
|
|
|
format(*this, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Format, class T>
|
|
|
|
auto operator()(Format&& format, const T* data, std::size_t count)
|
|
|
|
{
|
2021-11-08 18:25:42 +00:00
|
|
|
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T>)
|
2021-10-22 22:06:10 +00:00
|
|
|
mValue += count * sizeof(T);
|
|
|
|
else
|
|
|
|
format(*this, data, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::size_t mValue = 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|