File indexing completed on 2024-05-12 04:42:18

0001 /*
0002     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #ifndef OSM_O5MWRITER_H
0007 #define OSM_O5MWRITER_H
0008 
0009 #include "abstractwriter.h"
0010 
0011 #include <cstdint>
0012 #include <cstring>
0013 #include <string>
0014 #include <unordered_map>
0015 
0016 #include <QDebug>
0017 
0018 namespace OSM {
0019 
0020 struct O5mStringPair {
0021     std::string s1;
0022     std::string s2;
0023 
0024     [[nodiscard]] inline bool operator==(const O5mStringPair &other) const {
0025         return s1 == other.s1 && s2 == other.s2;
0026     }
0027 };
0028 }
0029 
0030 template<>
0031 struct std::hash<OSM::O5mStringPair>
0032 {
0033 
0034     [[nodiscard]] std::size_t operator()(const OSM::O5mStringPair &p) const noexcept
0035     {
0036         std::size_t h1 = std::hash<std::string>{}(p.s1);
0037         std::size_t h2 = std::hash<std::string>{}(p.s2);
0038         return h1 ^ (h2 << 1);
0039     }
0040 };
0041 
0042 namespace OSM {
0043 
0044 /** Serialize an OSM::DataSet into the o5m file format. */
0045 class O5mWriter : public OSM::AbstractWriter
0046 {
0047 private:
0048     void writeToIODevice(const OSM::DataSet& dataSet, QIODevice* io) override;
0049 
0050     void writeNodes(const OSM::DataSet &dataSet, QIODevice *io);
0051     void writeWays(const OSM::DataSet &dataSet, QIODevice *io);
0052     void writeRelations(const OSM::DataSet &dataSet, QIODevice *io);
0053     template <typename T> void writeTags(const T &elem, QIODevice *io);
0054     void writeStringPair(const char *s1, const char *s2, QIODevice *io);
0055 
0056     std::unordered_map<O5mStringPair, int16_t> m_stringTable;
0057 };
0058 
0059 }
0060 
0061 #endif // OSM_O5MWRITER_H