Skip to content

iceoryx_hoofs/cxx/serialization.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::cxx

Classes🔗

Name
class iox::cxx::Serialization
Simple serializer which serials every given type into the following format: (The type needs to be convertable into a string via cxx::convert::toString) LENGTH:DATALENGTH:DATA... Example: Serializes "hello", 123, 123.01 into 5:hello3:1236:123.01.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CXX_SERIALIZATION_HPP
#define IOX_HOOFS_CXX_SERIALIZATION_HPP

#include "iceoryx_hoofs/cxx/convert.hpp"

#include <cstdlib>
#include <iostream>
#include <sstream>

namespace iox
{
namespace cxx
{
class Serialization
{
  public:
    explicit Serialization(const std::string& value) noexcept;

    std::string toString() const noexcept;

    operator std::string() const noexcept;

    template <typename... Targs>
    static Serialization create(const Targs&... args) noexcept;

    template <typename T, typename... Targs>
    bool extract(T& t, Targs&... args) const noexcept;

    template <typename T>
    bool getNth(const unsigned int index, T& t) const noexcept;

    enum class Error
    {
        DESERIALIZATION_FAILED, 
    };

  private:
    std::string m_value;
    static constexpr char separator = ':';

  private:
    static std::string serializer() noexcept;

    static bool removeFirstEntry(std::string& firstEntry, std::string& remainder) noexcept;

    template <typename T>
    static typename std::enable_if<std::is_convertible<T, Serialization>::value, std::string>::type
    getString(const T& t) noexcept;
    template <typename T>
    static typename std::enable_if<!std::is_convertible<T, Serialization>::value, std::string>::type
    getString(const T& t) noexcept;
    template <typename T, typename... Targs>
    static std::string serializer(const T& t, const Targs&... args) noexcept;

    static bool deserialize(const std::string& serializedString) noexcept;

    template <typename T, typename... Targs>
    static bool deserialize(const std::string& serializedString, T& t, Targs&... args) noexcept;
};

} // namespace cxx
} // namespace iox

#include "iceoryx_hoofs/internal/cxx/serialization.inl"

#endif // IOX_HOOFS_CXX_SERIALIZATION_HPP

Updated on 18 December 2023 at 13:11:42 CET