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 : // 8 : // http://www.apache.org/licenses/LICENSE-2.0 9 : // 10 : // Unless required by applicable law or agreed to in writing, software 11 : // distributed under the License is distributed on an "AS IS" BASIS, 12 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 : // See the License for the specific language governing permissions and 14 : // limitations under the License. 15 : // 16 : 17 : #include "openmodelviewer/core/context.hpp" 18 : 19 : #include <stdexcept> 20 : #include <utility> 21 : 22 : namespace openmodelviewer::core 23 : { 24 23 : Context::Context(std::shared_ptr<Logger> logger_ptr, std::shared_ptr<TaskScheduler> scheduler_ptr, std::shared_ptr<ResourceManager> resource_ptr) 25 : { 26 23 : m_running.store(false); 27 : 28 23 : if(!logger_ptr || !scheduler_ptr || !resource_ptr) 29 : { 30 2 : throw std::invalid_argument("Context error: cannot be initialized with logger_ptr, scheduler_ptr or resource_ptr as nullptr."); 31 : } 32 : 33 21 : m_logger = std::move(logger_ptr); 34 21 : m_scheduler = std::move(scheduler_ptr); 35 21 : m_resource = std::move(resource_ptr); 36 29 : } 37 : 38 13 : bool Context::start() 39 : { 40 13 : bool expected = false; 41 13 : if (!m_running.compare_exchange_strong(expected, true)) 42 : { 43 2 : return false; 44 : } 45 : 46 11 : if (!m_logger->start() || !m_scheduler->start()) 47 : { 48 2 : m_running.store(false); 49 2 : return false; 50 : } 51 : 52 9 : return true; 53 : } 54 : 55 10 : void Context::stop() 56 : { 57 10 : bool expected = true; 58 10 : if (!m_running.compare_exchange_strong(expected, false)) 59 : { 60 3 : return; 61 : } 62 : 63 7 : m_logger->stop(); 64 7 : m_scheduler->stop(); 65 : } 66 : 67 4 : bool Context::isRunning() const noexcept 68 : { 69 4 : return m_running.load(); 70 : } 71 : 72 7 : Logger& Context::logger() const noexcept 73 : { 74 7 : return *m_logger; 75 : } 76 : 77 10 : TaskScheduler& Context::scheduler() const noexcept 78 : { 79 10 : return *m_scheduler; 80 : } 81 : 82 6 : ResourceManager& Context::resource() const noexcept 83 : { 84 6 : return *m_resource; 85 : } 86 : }