File type_information_registry.hpp

File List > astutedds > rtps > type_information_registry.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 type_information_registry.hpp
// @brief Process-local registry mapping type names to XTypes equivalence hashes
//

#ifndef ASTUTEDDS_RTPS_TYPE_INFORMATION_REGISTRY_HPP
#define ASTUTEDDS_RTPS_TYPE_INFORMATION_REGISTRY_HPP

#include <astutedds/xtypes/type_object.hpp>

#include <map>
#include <mutex>
#include <string>
#include <vector>

namespace astutedds::rtps
{

inline std::mutex& type_info_registry_mutex()
{
    static std::mutex mutex;
    return mutex;
}

inline std::map<std::string, xtypes::EquivalenceHash>& type_info_registry_map()
{
    static std::map<std::string, xtypes::EquivalenceHash> byType;
    return byType;
}

inline std::map<xtypes::EquivalenceHash, std::string>& type_info_hash_alias_map()
{
    static std::map<xtypes::EquivalenceHash, std::string> byHash;
    return byHash;
}

inline std::map<std::string, std::vector<std::string>>& type_schema_registry_map()
{
    static std::map<std::string, std::vector<std::string>> byType;
    return byType;
}

inline std::map<std::string, std::vector<uint8_t>>& type_schema_kinds_registry_map()
{
    static std::map<std::string, std::vector<uint8_t>> byType;
    return byType;
}

inline std::map<std::string, std::pair<std::vector<std::string>, std::vector<uint8_t>>>&
type_schema_nested_registry_map()
{
    static std::map<std::string, std::pair<std::vector<std::string>, std::vector<uint8_t>>> byKey;
    return byKey;
}

inline void register_type_information_hash(const std::string& typeName,
                                           const xtypes::EquivalenceHash& hash)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    type_info_registry_map()[typeName] = hash;
    type_info_hash_alias_map()[hash] = typeName;
}

inline bool lookup_type_information_hash(const std::string& typeName,
                                         xtypes::EquivalenceHash& outHash)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    const auto& byType = type_info_registry_map();
    const auto it = byType.find(typeName);
    if (it == byType.end())
    {
        return false;
    }
    outHash = it->second;
    return true;
}

inline bool lookup_type_name_by_hash(const xtypes::EquivalenceHash& hash,
                                     std::string& outTypeName)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    const auto& byHash = type_info_hash_alias_map();
    const auto hit = byHash.find(hash);
    if (hit != byHash.end())
    {
        outTypeName = hit->second;
        return true;
    }

    const auto& byType = type_info_registry_map();
    for (const auto& [typeName, h] : byType)
    {
        if (h == hash)
        {
            outTypeName = typeName;
            return true;
        }
    }
    return false;
}

inline void register_type_schema_fields(const std::string& typeName,
                                        const std::vector<std::string>& fields)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    type_schema_registry_map()[typeName] = fields;
}

inline void register_type_schema_field_kinds(const std::string& typeName,
                                              const std::vector<uint8_t>& kinds)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    type_schema_kinds_registry_map()[typeName] = kinds;
}

inline bool lookup_type_schema_fields(const std::string& typeName,
                                      std::vector<std::string>& outFields)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    const auto& byType = type_schema_registry_map();
    const auto it = byType.find(typeName);
    if (it == byType.end())
    {
        return false;
    }
    outFields = it->second;
    return true;
}

inline bool lookup_type_schema_field_kinds(const std::string& typeName,
                                           std::vector<uint8_t>& outKinds)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    const auto& byType = type_schema_kinds_registry_map();
    const auto it = byType.find(typeName);
    if (it == byType.end())
    {
        return false;
    }
    outKinds = it->second;
    return true;
}

inline void register_type_schema_nested(const std::string& typeName,
                                        const std::string& fieldName,
                                        const std::vector<std::string>& subNames,
                                        const std::vector<uint8_t>& subKinds)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    const std::string key = typeName + "." + fieldName;
    type_schema_nested_registry_map()[key] = {subNames, subKinds};
}

inline bool lookup_type_schema_nested(const std::string& typeName,
                                      const std::string& fieldName,
                                      std::vector<std::string>& outSubNames,
                                      std::vector<uint8_t>& outSubKinds)
{
    std::lock_guard<std::mutex> lock(type_info_registry_mutex());
    const std::string key = typeName + "." + fieldName;
    const auto& m = type_schema_nested_registry_map();
    const auto it = m.find(key);
    if (it == m.end())
        return false;
    outSubNames = it->second.first;
    outSubKinds = it->second.second;
    return true;
}

}  // namespace astutedds::rtps

#endif  // ASTUTEDDS_RTPS_TYPE_INFORMATION_REGISTRY_HPP