File indexing completed on 2024-06-16 04:23:17

0001 /*
0002     SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "typeregister.h"
0008 
0009 #include <debug.h>
0010 
0011 namespace KDevelop {
0012 AbstractType* TypeSystem::create(AbstractTypeData* data) const
0013 {
0014     Q_ASSERT(data);
0015     if (!ensureFactoryLoaded(*data)) {
0016         return nullptr;
0017     }
0018     return m_factories.value(data->typeClassId)->create(data);
0019 }
0020 
0021 void TypeSystem::callDestructor(AbstractTypeData* data) const
0022 {
0023     Q_ASSERT(data);
0024     if (!ensureFactoryLoaded(*data)) {
0025         return;
0026     }
0027     m_factories.value(data->typeClassId)->callDestructor(data);
0028 }
0029 
0030 uint TypeSystem::dynamicSize(const AbstractTypeData& data) const
0031 {
0032     if (!ensureFactoryLoaded(data)) {
0033         return 0;
0034     }
0035     return m_factories.value(data.typeClassId)->dynamicSize(data);
0036 }
0037 
0038 uint TypeSystem::dataClassSize(const AbstractTypeData& data) const
0039 {
0040     Q_ASSERT(m_dataClassSizes.contains(data.typeClassId));
0041     return m_dataClassSizes.value(data.typeClassId);
0042 }
0043 
0044 bool TypeSystem::isFactoryLoaded(const AbstractTypeData& data) const
0045 {
0046     return m_factories.contains(data.typeClassId);
0047 }
0048 
0049 bool TypeSystem::ensureFactoryLoaded(const AbstractTypeData& data) const
0050 {
0051     if (!m_factories.contains(data.typeClassId)) {
0052         qCWarning(LANGUAGE) << "Factory for this type not loaded:" << data.typeClassId;
0053         Q_ASSERT(false);
0054         return false;
0055     }
0056     return true;
0057 }
0058 
0059 void TypeSystem::copy(const AbstractTypeData& from, AbstractTypeData& to, bool constant) const
0060 {
0061     //Shouldn't try to copy an unknown type
0062     ensureFactoryLoaded(from);
0063     m_factories.value(from.typeClassId)->copy(from, to, constant);
0064 }
0065 
0066 TypeSystem& TypeSystem::self()
0067 {
0068     static TypeSystem system;
0069     return system;
0070 }
0071 
0072 void TypeSystem::registerTypeClassInternal(AbstractTypeFactory* repo, uint dataClassSize, uint identity)
0073 {
0074     qCDebug(LANGUAGE) << "Registering type class" << identity;
0075     Q_ASSERT(repo);
0076     Q_ASSERT(!m_factories.contains(identity));
0077     m_factories.insert(identity, repo);
0078     Q_ASSERT(!m_dataClassSizes.contains(identity));
0079     m_dataClassSizes.insert(identity, dataClassSize);
0080 }
0081 
0082 void TypeSystem::unregisterTypeClassInternal(uint identity)
0083 {
0084     qCDebug(LANGUAGE) << "Unregistering type class" << identity;
0085     AbstractTypeFactory* repo = m_factories.take(identity);
0086     Q_ASSERT(repo);
0087     delete repo;
0088     int removed = m_dataClassSizes.remove(identity);
0089     Q_ASSERT(removed);
0090     Q_UNUSED(removed);
0091 }
0092 }