LCOV - code coverage report
Current view: top level - include/openmodelviewer/core/resource - resource_cache.hpp (source / functions) Hit Total Coverage
Test: libopenmodelviewer coverage Lines: 17 17 100.0 %
Date: 2025-07-03 20:26:48 Functions: 9 13 69.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //
       2             : // Copyright 2024 OpenModelViewer Authors
       3             : //
       4             : // Licensed under the Apache License, Version 2.0 (the "License");
       5             : // you may not use this file except in compliance with the License.
       6             : // You may obtain a copy of the License at
       7             : //     http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : //
      15             : 
      16             : #pragma once
      17             : 
      18             : #include "openmodelviewer/core/resource/resource_handle.hpp"
      19             : 
      20             : #include <unordered_map>
      21             : #include <memory>
      22             : #include <utility>
      23             : 
      24             : namespace openmodelviewer::core::resource
      25             : {
      26             :     /**
      27             :      * @brief Abstract base class for all typed resource caches.
      28             :      *
      29             :      * Used internally to store caches for different resource types.
      30             :      */
      31             :     struct IResourceCache
      32             :     {
      33          29 :         virtual ~IResourceCache() = default;
      34             : 
      35             :         /**
      36             :          * @brief remove resource from cache.
      37             :          * 
      38             :          * @param handle The handle identifying the resource.
      39             :          * @return True if resource was successfully removed.
      40             :          */
      41             :         virtual bool remove(const ResourceHandle& handle) = 0;
      42             : 
      43             :         /**
      44             :          * @brief Check if resource is stored in the cache.
      45             :          * 
      46             :          * @param handle The handle identifying the resource.
      47             :          * @return True if resource is stored in the cache.
      48             :          */
      49             :         virtual bool contains(const ResourceHandle& handle) = 0;
      50             : 
      51             :         /**
      52             :          * @brief Clears all cached resources from this cache.
      53             :          *
      54             :          * This removes all entries, but does not force destruction if shared_ptr
      55             :          * references are still held elsewhere.
      56             :          */
      57             :         virtual void clear() = 0;
      58             :     };
      59             : 
      60             :     /**
      61             :      * @brief Typed cache storing loaded resources indexed by their handle.
      62             :      *
      63             :      * This class manages a map from ResourceHandle to shared instances of a given resource type.
      64             :      *
      65             :      * @tparam RType Type of the resource stored in this cache.
      66             :      */
      67             :     template <typename RType>
      68             :     struct ResourceCache final : IResourceCache
      69             :     {
      70             :         /**
      71             :          * @brief Retrieves the resource associated with the given handle.
      72             :          *
      73             :          * @param handle The handle identifying the resource.
      74             :          * @return Shared pointer to the resource, or nullptr if not found.
      75             :          */
      76          22 :         inline std::shared_ptr<RType> get(const ResourceHandle& handle) const
      77             :         {
      78          22 :             auto it = m_map.find(handle);
      79          44 :             return (it != m_map.end()) ? it->second : nullptr;
      80             :         }
      81             : 
      82             :         /**
      83             :          * @brief Inserts or replaces a resource associated with the given handle.
      84             :          *
      85             :          * @param handle Unique handle identifying the resource.
      86             :          * @param resource Shared pointer to the loaded resource.
      87             :          */
      88          33 :         inline void insert(const ResourceHandle& handle, std::shared_ptr<RType> resource)
      89             :         {
      90          33 :             m_map[handle] = std::move(resource);
      91          33 :         }
      92             : 
      93             :         /**
      94             :          * @brief remove resource from cache.
      95             :          * 
      96             :          * @param handle The handle identifying the resource.
      97             :          * @return True if resource was successfully removed.
      98             :          */
      99           7 :         inline bool remove(const ResourceHandle& handle) override
     100             :         {
     101           7 :             if (m_map.contains(handle))
     102             :             {
     103           6 :                 m_map.erase(handle);
     104           6 :                 return true;
     105             :             }
     106             : 
     107           1 :             return false;
     108             :         }
     109             : 
     110             :         /**
     111             :          * @brief Check if resource is stored in the cache.
     112             :          * 
     113             :          * @param handle The handle identifying the resource.
     114             :          * @return True if resource is stored in the cache.
     115             :          */
     116          13 :         inline bool contains(const ResourceHandle& handle)
     117             :         {
     118          13 :             return m_map.contains(handle);
     119             :         }
     120             : 
     121             :         /**
     122             :          * @brief Clears all entries in the cache.
     123             :          *
     124             :          * This operation removes all stored resources of this type.
     125             :          */
     126          18 :         inline void clear()
     127             :         {
     128          18 :             m_map.clear();
     129          18 :         }
     130             : 
     131             :     private:
     132             :         std::unordered_map<ResourceHandle, std::shared_ptr<RType>> m_map;
     133             :     };
     134             : } // namespace openmodelviewer::core::resource

Generated by: LCOV version 1.14