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: 11 15 73.3 %
Date: 2023-01-19 00:18:29 Functions: 26 145 17.9 %

          Line data    Source code
       1             : /* Copyright (C) 2015 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             : class CSimContext;
      24             : class CSimulation2;
      25             : class IComponent;
      26             : 
      27             : // Helper functions for CmpPtr
      28             : IComponent* QueryInterface(const CSimContext& context, entity_id_t ent, int iid);
      29             : IComponent* QueryInterface(const CSimulation2& simulation, entity_id_t ent, int iid);
      30             : 
      31             : /**
      32             :  * A simplified syntax for accessing entity components.
      33             :  * E.g. to get the @c Position component, write:
      34             :  *
      35             :  * @code
      36             :  * CmpPtr<ICmpPosition> cmpPosition(context, ent);
      37             :  * if (!cmpPosition)
      38             :  *     // do something (maybe just silently abort; you should never crash if the
      39             :  *     // component is missing, even if you're sure it should never be missing)
      40             :  * @endcode
      41             :  *
      42             :  * where @c context is (if you're writing component code) a CSimContext object, or
      43             :  * (if you're writing external engine code that makes use of the simulation system)
      44             :  * a CSimulation2 object; and @c ent is the entity ID.
      45             :  *
      46             :  * @c ent can be CComponentManager::SYSTEM_ENTITY (if you're writing a component), or
      47             :  * CSimulation2::SYSTEM_ENTITY (for external code), if you want to access the global
      48             :  * singleton system components.
      49             :  *
      50             :  * You should never hold onto a component pointer outside of the method in which you acquire
      51             :  * it, because it might get deleted and invalidate your pointer. (Components will never be
      52             :  * deleted while inside a simulation method.)
      53             :  */
      54             : template<typename T>
      55             : class CmpPtr
      56             : {
      57             : private:
      58             :     T* m;
      59             : 
      60             : public:
      61         112 :     CmpPtr(const CSimContext& context, entity_id_t ent)
      62             :     {
      63         112 :         m = static_cast<T*>(QueryInterface(context, ent, T::GetInterfaceId()));
      64         112 :     }
      65             : 
      66           0 :     CmpPtr(const CSimulation2& simulation, entity_id_t ent)
      67             :     {
      68           0 :         m = static_cast<T*>(QueryInterface(simulation, ent, T::GetInterfaceId()));
      69           0 :     }
      70             : 
      71          75 :     CmpPtr(CEntityHandle ent)
      72             :     {
      73          75 :         SEntityComponentCache* cache = ent.GetComponentCache();
      74          75 :         if (cache != NULL && T::GetInterfaceId() < (int)cache->numInterfaces)
      75          75 :             m = static_cast<T*>(cache->interfaces[T::GetInterfaceId()]);
      76             :         else
      77           0 :             m = NULL;
      78          75 :     }
      79             : 
      80         203 :     T* operator->() { return m; }
      81             : 
      82         187 :     explicit operator bool() const
      83             :     {
      84         187 :         return m != NULL;
      85             :     }
      86             : };
      87             : 
      88             : #endif // INCLUDED_CMPPTR

Generated by: LCOV version 1.13