Skip to main content

AVxcelerate Simulation Framework 2026 R1 SP01

File autonomy_msg.h

Last update: 22.04.2026

Location: autonomy/communication/messages/autonomy_msg.h

Classes

Namespaces

Includes

  • <type_traits>

Included by

Source


#ifndef SIMFWK_AUTONOMY_AUTONOMY_COMMUNICATION_MESSAGES_AUTONOMYMSG_H
#define SIMFWK_AUTONOMY_AUTONOMY_COMMUNICATION_MESSAGES_AUTONOMYMSG_H

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <type_traits>

namespace simulation_framework
{
namespace autonomy
{
namespace communication
{
namespace messages
{

struct TimestampView
{
    uint64_t seconds() const noexcept { return secs; }

    uint64_t nanos() const noexcept { return ns; }

    uint64_t secs{0};
    uint64_t ns{0};
};

class AutonomyMsg
{
  public:
    AutonomyMsg() {}

    uint64_t sequence_id{0ULL};

    size_t ByteSizeLong() const noexcept { return BaseSize() + PayloadSize(); }

    TimestampView timestamp() const noexcept { return timestamp_view_; }

    void SetTimestamp(uint64_t seconds, uint64_t nanos) noexcept
    {
        timestamp_view_.secs = seconds;
        timestamp_view_.ns = nanos;
    }

    void SerializeToArray(void* data, size_t size) const
    {
        if (!data)
        {
            throw std::runtime_error("Serialize buffer pointer is invalid");
        }
        const size_t needed = ByteSizeLong();
        if (size < needed)
        {
            throw std::runtime_error("Serialize buffer overflow: not enough space to write value");
        }
        unsigned char* ptr = static_cast<unsigned char*>(data);
        size_t remaining = size;

        writeLE(ptr, remaining, sequence_id);
        writeLE(ptr, remaining, timestamp_view_.secs);
        writeLE(ptr, remaining, timestamp_view_.ns);

        SerializePayload(ptr, remaining);
    }
    void ParseFromArray(const void* data, size_t size)
    {
        if (!data)
            throw std::runtime_error("Parse buffer pointer is invalid");
        const unsigned char* ptr = static_cast<const unsigned char*>(data);
        size_t remaining = size;

        readLE(ptr, remaining, sequence_id);
        readLE(ptr, remaining, timestamp_view_.secs);
        readLE(ptr, remaining, timestamp_view_.ns);

        ParsePayload(ptr, remaining);
    }

  protected:
    static constexpr size_t BaseSize() noexcept { return sizeof(uint64_t) + sizeof(uint64_t) + sizeof(uint64_t); }

    virtual size_t PayloadSize() const noexcept = 0;

    virtual void SerializePayload(unsigned char*& ptr, size_t& remaining) const = 0;

    template <typename T>
    static typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value, void>::type
    readLE(const unsigned char*& ptr, size_t& remaining, T& out)
    {
        const size_t n = sizeof(T);
        if (remaining < n)
        {
            throw std::runtime_error("Parse buffer underflow: not enough data to read value");
        }
        unsigned char tmp[sizeof(T)];
        std::memcpy(tmp, ptr, n);
        const uint16_t one = 1;
        const bool host_is_little = (*reinterpret_cast<const unsigned char*>(&one) == 1);
        if (!host_is_little)
        {
            std::reverse(tmp, tmp + n);
        }
        std::memcpy(&out, tmp, n);
        ptr += n;
        remaining -= n;
    }

    virtual void ParsePayload(const unsigned char*& ptr, size_t& remaining) = 0;

    template <typename T>
    static typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value, void>::type
    writeLE(unsigned char*& ptr, size_t& remaining, T value)
    {
        const size_t n = sizeof(T);
        if (remaining < n)
        {
            throw std::runtime_error("Serialize buffer overflow: not enough space to write value");
        }
        unsigned char tmp[sizeof(T)];
        std::memcpy(tmp, &value, n);
        const uint16_t one = 1;
        const bool host_is_little = (*reinterpret_cast<const unsigned char*>(&one) == 1);
        if (!host_is_little)
        {
            std::reverse(tmp, tmp + n);
        }
        std::memcpy(ptr, tmp, n);
        ptr += n;
        remaining -= n;
    }

    TimestampView timestamp_view_;
};

}  // namespace messages
}  // namespace communication
}  // namespace autonomy
}  // namespace simulation_framework

#endif  // SIMFWK_AUTONOMY_AUTONOMY_COMMUNICATION_MESSAGES_AUTONOMYMSG_H

Connect with Ansys