Line data Source code
1 : /* 2 : * This file is part of PTN Engine 3 : * 4 : * Copyright (c) 2024 Eduardo ValgĂ´de 5 : * 6 : * Licensed under the Apache License, Version 2.0 (the "License"); 7 : * you may not use this file except in compliance with the License. 8 : * You may obtain a copy of the License at 9 : * 10 : * http://www.apache.org/licenses/LICENSE-2.0 11 : * 12 : * Unless required by applicable law or agreed to in writing, software 13 : * distributed under the License is distributed on an "AS IS" BASIS, 14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 : * See the License for the specific language governing permissions and 16 : * limitations under the License. 17 : */ 18 : 19 : #pragma once 20 : 21 : #include "PTN_Engine/PTN_Exception.h" 22 : #include <memory> 23 : #include <unordered_map> 24 : 25 : namespace ptne 26 : { 27 : 28 : template <typename T> 29 : class ManagerBase 30 : { 31 : protected: 32 159 : ~ManagerBase() = default; 33 159 : ManagerBase() = default; 34 : ManagerBase(const ManagerBase &) = delete; 35 : ManagerBase(ManagerBase &&) = delete; 36 : ManagerBase &operator=(const ManagerBase &) = delete; 37 : ManagerBase &operator=(const ManagerBase &&) = delete; 38 : 39 : //! 40 : //! \brief Removes all places from the container. 41 : //! 42 6 : void clear() 43 : { 44 6 : m_items.clear(); 45 6 : } 46 : 47 189 : bool contains(const std::string &itemName) const 48 : { 49 189 : return m_items.contains(itemName); 50 : } 51 : 52 354 : void insert(const std::shared_ptr<T> &item) 53 : { 54 354 : if (item == nullptr) 55 : { 56 1 : throw PTN_Exception("Tried to insert nullptr item"); 57 : } 58 353 : const auto &itemName = item->getName(); 59 353 : if (itemName.empty()) 60 : { 61 2 : throw PTN_Exception("Empty item names are not supported."); 62 : } 63 351 : else if (m_items.contains(itemName)) 64 : { 65 3 : throw RepeatedPlaceException(itemName); 66 : } 67 348 : m_items[itemName] = item; 68 353 : } 69 : 70 422 : std::shared_ptr<T> getItem(const std::string &itemName) const 71 : { 72 422 : if (!m_items.contains(itemName)) 73 : { 74 2 : throw PTN_Exception("Cannot get:" + itemName); 75 : } 76 420 : return m_items.at(itemName); 77 : } 78 : 79 : std::unordered_map<std::string, std::shared_ptr<T>> m_items; 80 : }; 81 : 82 : } // namespace ptne