LCOV - code coverage report
Current view: top level - PTN_Engine - ManagedContainer.h (source / functions) Hit Total Coverage
Test: filtered_coverage.info Lines: 28 30 93.3 %
Date: 2024-05-26 15:41:39 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of PTN Engine
       3             :  * Copyright (c) 2023-2024 Eduardo ValgĂ´de
       4             :  *
       5             :  * Licensed under the Apache License, Version 2.0 (the "License");
       6             :  * you may not use this file except in compliance with the License.
       7             :  * You may obtain a copy of the License at
       8             :  *
       9             :  * http://www.apache.org/licenses/LICENSE-2.0
      10             :  *
      11             :  * Unless required by applicable law or agreed to in writing, software
      12             :  * distributed under the License is distributed on an "AS IS" BASIS,
      13             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14             :  * See the License for the specific language governing permissions and
      15             :  * limitations under the License.
      16             :  */
      17             : #pragma once
      18             : 
      19             : #include "PTN_Engine/PTN_Exception.h"
      20             : #include <algorithm>
      21             : #include <mutex>
      22             : #include <shared_mutex>
      23             : #include <unordered_map>
      24             : #include <vector>
      25             : 
      26             : namespace ptne
      27             : {
      28             : 
      29             : //!
      30             : //! \brief The ManagedContainer class
      31             : //!
      32             : template <typename T>
      33             : class ManagedContainer
      34             : {
      35             : public:
      36         142 :         ~ManagedContainer() = default;
      37         142 :         ManagedContainer() = default;
      38             :         ManagedContainer(const ManagedContainer &) = delete;
      39             :         ManagedContainer(ManagedContainer &&) = delete;
      40             :         ManagedContainer &operator=(const ManagedContainer &) = delete;
      41             :         ManagedContainer &operator=(const ManagedContainer &&) = delete;
      42             : 
      43             : 
      44             :         //!
      45             :         //! \brief Add item to container.
      46             :         //! \throws InvalidFunctionNameException, RepeatedFunctionException
      47             :         //! \param name - Name of the item used as key.
      48             :         //! \param item - The item itself.
      49             :         //!
      50          23 :         void addItem(const std::string &name, T item)
      51             :         {
      52          23 :                 std::unique_lock lock(m_mutex);
      53          23 :                 if (name.empty())
      54             :                 {
      55           2 :                         throw InvalidFunctionNameException(name);
      56             :                 }
      57             : 
      58          21 :                 if (m_items.contains(name))
      59             :                 {
      60           4 :                         throw RepeatedFunctionException(name);
      61             :                 }
      62             : 
      63          17 :                 m_items[name] = item;
      64          23 :         }
      65             : 
      66             :         //!
      67             :         //! \brief Retrieve a copy of the item.
      68             :         //! \param name - Identifier of the item.
      69             :         //! \throws InvalidFunctionNameException
      70             :         //! \return Copy of the item identified by name.
      71             :         //!
      72           8 :         T getItem(const std::string &name) const
      73             :         {
      74           8 :                 if (name.empty())
      75             :                 {
      76           0 :                         throw InvalidFunctionNameException(name);
      77             :                 }
      78             : 
      79           8 :                 std::shared_lock lock(m_mutex);
      80           8 :                 if (!m_items.contains(name))
      81             :                 {
      82           1 :                         throw InvalidFunctionNameException(name);
      83             :                 }
      84          14 :                 return m_items.at(name);
      85           8 :         }
      86             : 
      87             :         //!
      88             :         //! \brief Gets copies of the items based on their identifying names.
      89             :         //!        Invalid names result in a InvalidFunctionNameException.
      90             :         //! \throws InvalidFunctionNameException
      91             :         //! \param names of the items
      92             :         //! \return vector with the names and copies of the items found in the container.
      93             :         //!
      94           8 :         std::vector<std::pair<std::string, T>> getItems(const std::vector<std::string> &names) const
      95             :         {
      96           8 :                 std::shared_lock lock(m_mutex);
      97           8 :                 std::vector<std::pair<std::string, T>> items;
      98           8 :                 std::ranges::for_each(names,
      99          10 :                                                           [&](const std::string &name)
     100             :                                                           {
     101          11 :                                                                   if (name.empty())
     102             :                                                                   {
     103           0 :                                                                           throw InvalidFunctionNameException(name);
     104             :                                                                   }
     105             : 
     106          11 :                                                                   if (!m_items.contains(name))
     107             :                                                                   {
     108           1 :                                                                           throw InvalidFunctionNameException(name);
     109             :                                                                   }
     110          10 :                                                                   items.emplace_back(name, m_items.at(name));
     111             :                                                           });
     112          14 :                 return items;
     113           9 :         }
     114             : 
     115             : private:
     116             :         //! An unordered map of items identified by a string.
     117             :         std::unordered_map<std::string, T> m_items;
     118             : 
     119             :         //! Protects concurrent access to m_items.
     120             :         mutable std::shared_mutex m_mutex;
     121             : };
     122             : 
     123             : } // namespace ptne

Generated by: LCOV version 1.14