<commit_before>/*
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 */

#include <cassert>
#include <cstdlib>
#include <uavcan/node/global_data_type_registry.hpp>
#include <uavcan/debug.hpp>

namespace uavcan
{

const GlobalDataTypeRegistry::List* GlobalDataTypeRegistry::selectList(DataTypeKind kind) const
{
    switch (kind)
    {
    case DataTypeKindMessage: return &msgs_;
    case DataTypeKindService: return &srvs_;
    default:
        assert(0);
        return NULL;
    }
}

GlobalDataTypeRegistry::List* GlobalDataTypeRegistry::selectList(DataTypeKind kind)
{
    switch (kind)
    {
    case DataTypeKindMessage: return &msgs_;
    case DataTypeKindService: return &srvs_;
    default:
        assert(0);
        return NULL;
    }
}

GlobalDataTypeRegistry::RegistResult GlobalDataTypeRegistry::remove(Entry* dtd)
{
    if (!dtd)
    {
        assert(0);
        return RegistResultInvalidParams;
    }
    if (isFrozen())
        return RegistResultFrozen;

    List* list = selectList(dtd->descriptor.getKind());
    if (!list)
        return RegistResultInvalidParams;

    list->remove(dtd);       // If this call came from regist<>(), that would be enough
    Entry* p = list->get();  // But anyway
    while (p)
    {
        Entry* const next = p->getNextListNode();
        if (p->descriptor.match(dtd->descriptor.getKind(), dtd->descriptor.getFullName()))
            list->remove(p);
        p = next;
    }
    return RegistResultOk;
}

GlobalDataTypeRegistry::RegistResult GlobalDataTypeRegistry::registImpl(Entry* dtd)
{
    if (!dtd || (dtd->descriptor.getID() > DataTypeID::Max))
    {
        assert(0);
        return RegistResultInvalidParams;
    }
    if (isFrozen())
        return RegistResultFrozen;

    List* list = selectList(dtd->descriptor.getKind());
    if (!list)
        return RegistResultInvalidParams;

    {   // Collision check
        Entry* p = list->get();
        while (p)
        {
            if (p->descriptor.getID() == dtd->descriptor.getID()) // ID collision
                return RegistResultCollision;
            if (!std::strcmp(p->descriptor.getFullName(), dtd->descriptor.getFullName())) // Name collision
                return RegistResultCollision;
            p = p->getNextListNode();
        }
    }
    list->insertBefore(dtd, EntryInsertionComparator(dtd));

#if UAVCAN_DEBUG
    {   // Order check
        Entry* p = list->get();
        int id = -1;
        while (p)
        {
            if (id >= p->descriptor.getID().get())
            {
                assert(0);
                std::abort();
            }
            id = p->descriptor.getID().get();
            p = p->getNextListNode();
        }
    }
#endif
    return RegistResultOk;
}

GlobalDataTypeRegistry& GlobalDataTypeRegistry::instance()
{
    static GlobalDataTypeRegistry inst;
    return inst;
}

void GlobalDataTypeRegistry::freeze()
{
    if (!frozen_)
    {
        frozen_ = true;
        UAVCAN_TRACE("GlobalDataTypeRegistry", "Frozen; num msgs: %u, num srvs: %u",
                     getNumMessageTypes(), getNumServiceTypes());
    }
}

const DataTypeDescriptor* GlobalDataTypeRegistry::find(DataTypeKind kind, const char* name) const
{
    const List* list = selectList(kind);
    if (!list)<commit_msg>Runtime check for GDTR::find()<commit_after>/*
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 */

#include <cassert>
#include <cstdlib>
#include <uavcan/node/global_data_type_registry.hpp>
#include <uavcan/debug.hpp>

namespace uavcan
{

const GlobalDataTypeRegistry::List* GlobalDataTypeRegistry::selectList(DataTypeKind kind) const
{
    switch (kind)
    {
    case DataTypeKindMessage: return &msgs_;
    case DataTypeKindService: return &srvs_;
    default:
        assert(0);
        return NULL;
    }
}

GlobalDataTypeRegistry::List* GlobalDataTypeRegistry::selectList(DataTypeKind kind)
{
    switch (kind)
    {
    case DataTypeKindMessage: return &msgs_;
    case DataTypeKindService: return &srvs_;
    default:
        assert(0);
        return NULL;
    }
}

GlobalDataTypeRegistry::RegistResult GlobalDataTypeRegistry::remove(Entry* dtd)
{
    if (!dtd)
    {
        assert(0);
        return RegistResultInvalidParams;
    }
    if (isFrozen())
        return RegistResultFrozen;

    List* list = selectList(dtd->descriptor.getKind());
    if (!list)
        return RegistResultInvalidParams;

    list->remove(dtd);       // If this call came from regist<>(), that would be enough
    Entry* p = list->get();  // But anyway
    while (p)
    {
        Entry* const next = p->getNextListNode();
        if (p->descriptor.match(dtd->descriptor.getKind(), dtd->descriptor.getFullName()))
            list->remove(p);
        p = next;
    }
    return RegistResultOk;
}

GlobalDataTypeRegistry::RegistResult GlobalDataTypeRegistry::registImpl(Entry* dtd)
{
    if (!dtd || (dtd->descriptor.getID() > DataTypeID::Max))
    {
        assert(0);
        return RegistResultInvalidParams;
    }
    if (isFrozen())
        return RegistResultFrozen;

    List* list = selectList(dtd->descriptor.getKind());
    if (!list)
        return RegistResultInvalidParams;

    {   // Collision check
        Entry* p = list->get();
        while (p)
        {
            if (p->descriptor.getID() == dtd->descriptor.getID()) // ID collision
                return RegistResultCollision;
            if (!std::strcmp(p->descriptor.getFullName(), dtd->descriptor.getFullName())) // Name collision
                return RegistResultCollision;
            p = p->getNextListNode();
        }
    }
    list->insertBefore(dtd, EntryInsertionComparator(dtd));

#if UAVCAN_DEBUG
    {   // Order check
        Entry* p = list->get();
        int id = -1;
        while (p)
        {
            if (id >= p->descriptor.getID().get())
            {
                assert(0);
                std::abort();
            }
            id = p->descriptor.getID().get();
            p = p->getNextListNode();
        }
    }
#endif
    return RegistResultOk;
}

GlobalDataTypeRegistry& GlobalDataTypeRegistry::instance()
{
    static GlobalDataTypeRegistry inst;
    return inst;
}

void GlobalDataTypeRegistry::freeze()
{
    if (!frozen_)
    {
        frozen_ = true;
        UAVCAN_TRACE("GlobalDataTypeRegistry", "Frozen; num msgs: %u, num srvs: %u",
                     getNumMessageTypes(), getNumServiceTypes());
    }
}

const DataTypeDescriptor* GlobalDataTypeRegistry::find(DataTypeKind kind, const char* name) const
{
    if (!name)
    {
        assert(0);
        return NULL;
    }
    const List* list = selectList(kind);
    if (!list)
{'fraction_non_alphanumeric': 0.12628865979381443, 'fraction_numerical': 0.0031837477258944815, 'mean_word_length': 2.49973474801061, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5519381', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1987, 'n_words': 423}