From 56ae875861ab260b80a030f50c4aff9f9dc8fff0 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:32:39 +0200 Subject: Adding upstream version 2.14.2. Signed-off-by: Daniel Baumann --- lib/config/activationcontext.cpp | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/config/activationcontext.cpp (limited to 'lib/config/activationcontext.cpp') diff --git a/lib/config/activationcontext.cpp b/lib/config/activationcontext.cpp new file mode 100644 index 0000000..d050875 --- /dev/null +++ b/lib/config/activationcontext.cpp @@ -0,0 +1,61 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#include "config/activationcontext.hpp" +#include "base/exception.hpp" + +using namespace icinga; + +boost::thread_specific_ptr > ActivationContext::m_ActivationStack; + +std::stack& ActivationContext::GetActivationStack() +{ + std::stack *actx = m_ActivationStack.get(); + + if (!actx) { + actx = new std::stack(); + m_ActivationStack.reset(actx); + } + + return *actx; +} + +void ActivationContext::PushContext(const ActivationContext::Ptr& context) +{ + GetActivationStack().push(context); +} + +void ActivationContext::PopContext() +{ + ASSERT(!GetActivationStack().empty()); + GetActivationStack().pop(); +} + +ActivationContext::Ptr ActivationContext::GetCurrentContext() +{ + std::stack& astack = GetActivationStack(); + + if (astack.empty()) + BOOST_THROW_EXCEPTION(std::runtime_error("Objects may not be created outside of an activation context.")); + + return astack.top(); +} + +ActivationScope::ActivationScope(ActivationContext::Ptr context) + : m_Context(std::move(context)) +{ + if (!m_Context) + m_Context = new ActivationContext(); + + ActivationContext::PushContext(m_Context); +} + +ActivationScope::~ActivationScope() +{ + ActivationContext::PopContext(); +} + +ActivationContext::Ptr ActivationScope::GetContext() const +{ + return m_Context; +} + -- cgit v1.2.3