File dynamic_data.hpp

File List > astutedds > xtypes > dynamic_data.hpp

Go to the documentation of this file

//
// Copyright (c) 2026, Astute Systems PTY LTD
//
// This file is part of the Astute DDS developed by Astute Systems.
//
// See the commercial LICENSE file in the project root for full license details.
//
// @file dynamic_data.hpp
// @brief Dynamic data API for runtime type manipulation
//
// Provides runtime access to typed data without compile-time knowledge of the type.
// Reference: DDS-XTypes 1.3 Section 7.5
//

#ifndef ASTUTEDDS_XTYPES_DYNAMIC_DATA_HPP
#define ASTUTEDDS_XTYPES_DYNAMIC_DATA_HPP

#include "type_object.hpp"

#include <memory>
#include <unordered_map>
#include <variant>

namespace astutedds::xtypes
{

class DynamicType
{
public:
    explicit DynamicType(const TypeObject& type_obj) : type_object_(type_obj) {}

    const TypeObject& get_type_object() const { return type_object_; }

    TypeKind get_kind() const;
    std::string get_name() const;
    uint32_t get_member_count() const;

private:
    TypeObject type_object_;
};

class DynamicData
{
public:
    explicit DynamicData(std::shared_ptr<DynamicType> type);
    ~DynamicData() = default;

    // Type information
    std::shared_ptr<DynamicType> get_type() const { return type_; }

    // Primitive type setters
    void set_int8_value(uint32_t member_id, int8_t value);
    void set_uint8_value(uint32_t member_id, uint8_t value);
    void set_int16_value(uint32_t member_id, int16_t value);
    void set_uint16_value(uint32_t member_id, uint16_t value);
    void set_int32_value(uint32_t member_id, int32_t value);
    void set_uint32_value(uint32_t member_id, uint32_t value);
    void set_int64_value(uint32_t member_id, int64_t value);
    void set_uint64_value(uint32_t member_id, uint64_t value);
    void set_float32_value(uint32_t member_id, float value);
    void set_float64_value(uint32_t member_id, double value);
    void set_bool_value(uint32_t member_id, bool value);
    void set_char8_value(uint32_t member_id, char value);
    void set_string_value(uint32_t member_id, const std::string& value);

    // Primitive type getters
    int8_t get_int8_value(uint32_t member_id) const;
    uint8_t get_uint8_value(uint32_t member_id) const;
    int16_t get_int16_value(uint32_t member_id) const;
    uint16_t get_uint16_value(uint32_t member_id) const;
    int32_t get_int32_value(uint32_t member_id) const;
    uint32_t get_uint32_value(uint32_t member_id) const;
    int64_t get_int64_value(uint32_t member_id) const;
    uint64_t get_uint64_value(uint32_t member_id) const;
    float get_float32_value(uint32_t member_id) const;
    double get_float64_value(uint32_t member_id) const;
    bool get_bool_value(uint32_t member_id) const;
    char get_char8_value(uint32_t member_id) const;
    std::string get_string_value(uint32_t member_id) const;

    // Complex type access
    std::shared_ptr<DynamicData> get_complex_value(uint32_t member_id) const;
    void set_complex_value(uint32_t member_id, std::shared_ptr<DynamicData> value);

    // Validation
    bool is_member_present(uint32_t member_id) const;
    void clear_member(uint32_t member_id);
    void clear_all();

private:
    using ValueType = std::variant<std::monostate, int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
                                   uint64_t, float, double, bool, char, std::string, std::shared_ptr<DynamicData>>;

    std::shared_ptr<DynamicType> type_;
    std::unordered_map<uint32_t, ValueType> values_;

    void validate_member_id(uint32_t member_id) const;
    void validate_member_type(uint32_t member_id, TypeKind expected) const;
};

}  // namespace astutedds::xtypes

#endif  // ASTUTEDDS_XTYPES_DYNAMIC_DATA_HPP