LCOV - code coverage report
Current view: top level - source/simulation2/system - CmpPtr.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 9 12 75.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_CMPPTR
      19             : #define INCLUDED_CMPPTR
      20             : 
      21             : #include "simulation2/system/Entity.h"
      22             : 
      23             : #include <cstddef>
      24             : 
      25             : class CSimContext;
      26             : class CSimulation2;
      27             : class IComponent;
      28             : 
      29             : // Helper functions for CmpPtr
      30             : IComponent* QueryInterface(const CSimContext& context, entity_id_t ent, int iid);
      31             : IComponent* QueryInterface(const CSimulation2& simulation, entity_id_t ent, int iid);
      32             : 
      33             : /**
      34             :  * A simplified syntax for accessing entity components.
      35             :  * E.g. to get the @c Position component, write:
      36             :  *
      37             :  * @code
      38             :  * CmpPtr<ICmpPosition> cmpPosition(context, ent);
      39             :  * if (!cmpPosition)
      40             :  *     // do something (maybe just silently abort; you should never crash if the
      41             :  *     // component is missing, even if you're sure it should never be missing)
      42             :  * @endcode
      43             :  *
      44             :  * where @c context is (if you're writing component code) a CSimContext object, or
      45             :  * (if you're writing external engine code that makes use of the simulation system)
      46             :  * a CSimulation2 object; and @c ent is the entity ID.
      47             :  *
      48             :  * @c ent can be CComponentManager::SYSTEM_ENTITY (if you're writing a component), or
      49             :  * CSimulation2::SYSTEM_ENTITY (for external code), if you want to access the global
      50             :  * singleton system components.
      51             :  *
      52             :  * You should never hold onto a component pointer outside of the method in which you acquire
      53             :  * it, because it might get deleted and invalidate your pointer. (Components will never be
      54             :  * deleted while inside a simulation method.)
      55             :  */
      56             : template<typename T>
      57             : class CmpPtr
      58             : {
      59             : private:
      60             :     T* m;
      61             : 
      62             : public:
      63      102141 :     CmpPtr(const CSimContext& context, entity_id_t ent)
      64             :     {
      65      101929 :         m = static_cast<T*>(QueryInterface(context, ent, T::GetInterfaceId()));
      66             :     }
      67             : 
      68           4 :     CmpPtr(const CSimulation2& simulation, entity_id_t ent)
      69             :     {
      70           4 :         m = static_cast<T*>(QueryInterface(simulation, ent, T::GetInterfaceId()));
      71             :     }
      72             : 
      73      509619 :     CmpPtr(CEntityHandle ent)
      74             :     {
      75      509619 :         SEntityComponentCache* cache = ent.GetComponentCache();
      76      509619 :         if (cache != NULL && T::GetInterfaceId() < (int)cache->numInterfaces)
      77      502401 :             m = static_cast<T*>(cache->interfaces[T::GetInterfaceId()]);
      78             :         else
      79           0 :             m = NULL;
      80             :     }
      81             : 
      82        7436 :     T* operator->() { return m; }
      83             : 
      84           0 :     explicit operator bool() const
      85             :     {
      86           0 :         return m != NULL;
      87             :     }
      88             : };
      89             : 
      90             : #endif // INCLUDED_CMPPTR

Generated by: LCOV version 1.16