mirror of
https://codeberg.org/comaps/comaps
synced 2026-01-03 03:13:48 +00:00
[coding] Rename non-empty string serialization.
Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
committed by
Konstantin Pastbin
parent
7b4862b442
commit
1d29e7816a
@@ -1,46 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "coding/reader.hpp"
|
||||
#include "coding/read_write_utils.hpp"
|
||||
#include "coding/varint.hpp"
|
||||
#include "coding/writer.hpp"
|
||||
|
||||
#include "base/assert.hpp"
|
||||
#include "base/buffer_vector.hpp"
|
||||
#include "base/control_flow.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace utils
|
||||
namespace rw
|
||||
{
|
||||
template <class TSink, bool EnableExceptions = false>
|
||||
void WriteString(TSink & sink, std::string const & s)
|
||||
template <class TSink>
|
||||
void WriteNonEmpty(TSink & sink, std::string const & s)
|
||||
{
|
||||
if (EnableExceptions && s.empty())
|
||||
MYTHROW(Writer::WriteException, ("String is empty"));
|
||||
else
|
||||
CHECK(!s.empty(), ());
|
||||
|
||||
CHECK(!s.empty(), ());
|
||||
size_t const sz = s.size();
|
||||
WriteVarUint(sink, static_cast<uint32_t>(sz - 1));
|
||||
sink.Write(s.c_str(), sz);
|
||||
}
|
||||
|
||||
template <class TSource, bool EnableExceptions = false>
|
||||
void ReadString(TSource & src, std::string & s)
|
||||
template <class TSource>
|
||||
void ReadNonEmpty(TSource & src, std::string & s)
|
||||
{
|
||||
uint32_t const sz = ReadVarUint<uint32_t>(src) + 1;
|
||||
s.resize(sz);
|
||||
src.Read(&s[0], sz);
|
||||
|
||||
if (EnableExceptions && s.empty())
|
||||
MYTHROW(Reader::ReadException, ("String is empty"));
|
||||
else
|
||||
CHECK(!s.empty(), ());
|
||||
}
|
||||
} // namespace utils
|
||||
} // namespace rw
|
||||
|
||||
// A class to store strings in multiple languages.
|
||||
// May be used e.g. to store several translations of a feature's name.
|
||||
@@ -201,15 +190,14 @@ public:
|
||||
/// @name Used for serdes.
|
||||
/// @{
|
||||
template <class TSink>
|
||||
void Write(TSink & sink) const
|
||||
void WriteNonEmpty(TSink & sink) const
|
||||
{
|
||||
utils::WriteString(sink, m_s);
|
||||
rw::WriteNonEmpty(sink, m_s);
|
||||
}
|
||||
|
||||
template <class TSource>
|
||||
void Read(TSource & src)
|
||||
void ReadNonEmpty(TSource & src)
|
||||
{
|
||||
utils::ReadString(src, m_s);
|
||||
rw::ReadNonEmpty(src, m_s);
|
||||
}
|
||||
|
||||
std::string const & GetBuffer() const { return m_s; }
|
||||
|
||||
@@ -176,7 +176,7 @@ struct FeatureParamsBase
|
||||
using namespace feature;
|
||||
|
||||
if (header & HEADER_MASK_HAS_NAME)
|
||||
name.Write(sink);
|
||||
name.WriteNonEmpty(sink);
|
||||
|
||||
if (header & HEADER_MASK_HAS_LAYER)
|
||||
WriteToSink(sink, layer);
|
||||
@@ -187,7 +187,7 @@ struct FeatureParamsBase
|
||||
switch (headerGeomType)
|
||||
{
|
||||
case HeaderGeomType::Point: WriteToSink(sink, rank); break;
|
||||
case HeaderGeomType::Line: utils::WriteString(sink, ref); break;
|
||||
case HeaderGeomType::Line: rw::WriteNonEmpty(sink, ref); break;
|
||||
case HeaderGeomType::Area:
|
||||
case HeaderGeomType::PointEx: house.Write(sink); break;
|
||||
}
|
||||
@@ -200,7 +200,7 @@ struct FeatureParamsBase
|
||||
using namespace feature;
|
||||
|
||||
if (header & HEADER_MASK_HAS_NAME)
|
||||
name.Read(src);
|
||||
name.ReadNonEmpty(src);
|
||||
|
||||
if (header & HEADER_MASK_HAS_LAYER)
|
||||
layer = ReadPrimitiveFromSource<int8_t>(src);
|
||||
@@ -211,7 +211,7 @@ struct FeatureParamsBase
|
||||
switch (headerGeomType)
|
||||
{
|
||||
case HeaderGeomType::Point: rank = ReadPrimitiveFromSource<uint8_t>(src); break;
|
||||
case HeaderGeomType::Line: utils::ReadString(src, ref); break;
|
||||
case HeaderGeomType::Line: rw::ReadNonEmpty(src, ref); break;
|
||||
case HeaderGeomType::Area:
|
||||
case HeaderGeomType::PointEx: house.Read(src); break;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
for (auto const & it : m_metadata)
|
||||
{
|
||||
WriteVarUint(sink, static_cast<uint32_t>(it.first));
|
||||
utils::WriteString(sink, it.second);
|
||||
rw::WriteNonEmpty(sink, it.second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public:
|
||||
for (size_t i = 0; i < sz; ++i)
|
||||
{
|
||||
auto const key = ReadVarUint<uint32_t>(src);
|
||||
utils::ReadString(src, m_metadata[key]);
|
||||
rw::ReadNonEmpty(src, m_metadata[key]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "coding/string_utf8_multilang.hpp"
|
||||
#include "coding/writer.hpp"
|
||||
|
||||
#include "geometry/point2d.hpp"
|
||||
#include "geometry/point_with_altitude.hpp"
|
||||
|
||||
Reference in New Issue
Block a user