LCOV - code coverage report
Current view: top level - src/engraving/infrastructure - eidregister.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 68.8 % 48 33
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "logger.h"
      24              : #include "eidregister.h"
      25              : #include "log.h"
      26              : 
      27              : #include "dom/mscore.h"
      28              : 
      29              : using namespace mu::engraving;
      30              : 
      31        70301 : EID EIDRegister::newEIDForItem(const EngravingObject* item)
      32              : {
      33        70301 :     EID eid = MScore::testMode ? EID::newUniqueTestMode(m_maxValTestMode) : EID::newUnique();
      34        70301 :     registerItemEID(eid, const_cast<EngravingObject*>(item));
      35        70301 :     return eid;
      36              : }
      37              : 
      38       110132 : void EIDRegister::registerItemEID(const EID& eid, const EngravingObject* item)
      39              : {
      40       110132 :     std::lock_guard lock(m_mutex);
      41       110132 :     IF_ASSERT_FAILED(eid.isValid() && item) {
      42            0 :         return;
      43              :     }
      44              : 
      45       110132 :     bool inserted = m_eidToItem.emplace(eid, const_cast<EngravingObject*>(item)).second;
      46       110132 :     assert(inserted);
      47              : 
      48       110132 :     inserted = m_itemToEid.emplace(const_cast<EngravingObject*>(item), eid).second;
      49       110132 :     assert(inserted);
      50              : 
      51       110132 :     if (MScore::testMode) {
      52       110132 :         EID::updateMaxValTestMode(eid, m_maxValTestMode);
      53              :     }
      54              : 
      55              : #ifdef NDEBUG
      56              :     UNUSED(inserted);
      57              : #endif
      58       110132 : }
      59              : 
      60            0 : void EIDRegister::removeItem(const EngravingObject* item)
      61              : {
      62            0 :     std::lock_guard lock(m_mutex);
      63              :     // NOTE: needed only when elements are removed during read (e.g. broken spanners)
      64              : 
      65            0 :     auto itemIter = m_itemToEid.find(const_cast<EngravingObject*>(item));
      66            0 :     IF_ASSERT_FAILED(itemIter != m_itemToEid.end()) {
      67            0 :         return;
      68              :     }
      69              : 
      70            0 :     EID eid = (*itemIter).second;
      71            0 :     DO_ASSERT(eid.isValid());
      72              : 
      73            0 :     m_itemToEid.erase(itemIter);
      74              : 
      75            0 :     auto eidIter = m_eidToItem.find(eid);
      76            0 :     IF_ASSERT_FAILED(eidIter != m_eidToItem.end()) {
      77            0 :         return;
      78              :     }
      79              : 
      80            0 :     m_eidToItem.erase(eidIter);
      81            0 : }
      82              : 
      83       272910 : void EIDRegister::onItemDestroyed(const EngravingObject* item)
      84              : {
      85       272910 :     std::lock_guard lock(m_mutex);
      86              :     // NOTE: most items never get an EID assigned, so a missing entry is the normal case.
      87              :     // Items that do have one must be unregistered here: a later allocation can reuse
      88              :     // the freed address, and a stale entry would corrupt the register.
      89              : 
      90       272910 :     auto itemIter = m_itemToEid.find(const_cast<EngravingObject*>(item));
      91       272910 :     if (itemIter == m_itemToEid.end()) {
      92       259563 :         return;
      93              :     }
      94              : 
      95        13347 :     m_eidToItem.erase(itemIter->second);
      96        13347 :     m_itemToEid.erase(itemIter);
      97       272910 : }
      98              : 
      99         5632 : EngravingObject* EIDRegister::itemFromEID(const EID& eid) const
     100              : {
     101         5632 :     std::shared_lock lock(m_mutex);
     102         5632 :     auto iter = m_eidToItem.find(eid);
     103         5632 :     if (iter == m_eidToItem.end()) {
     104            0 :         return nullptr;
     105              :     }
     106         5632 :     return iter->second;
     107         5632 : }
     108              : 
     109       108866 : EID EIDRegister::EIDFromItem(const EngravingObject* item) const
     110              : {
     111       108866 :     std::shared_lock lock(m_mutex);
     112       108866 :     auto iter = m_itemToEid.find(const_cast<EngravingObject*>(item));
     113       217732 :     return iter == m_itemToEid.end() ? EID::invalid() : iter->second;
     114       108866 : }
        

Generated by: LCOV version 2.0-1