2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-11-19 19:38:20 +01:00
|
|
|
|
|
|
|
#include "config/activationcontext.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
boost::thread_specific_ptr<std::stack<ActivationContext::Ptr> > ActivationContext::m_ActivationStack;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
std::stack<ActivationContext::Ptr>& ActivationContext::GetActivationStack()
|
2015-11-19 19:38:20 +01:00
|
|
|
{
|
|
|
|
std::stack<ActivationContext::Ptr> *actx = m_ActivationStack.get();
|
|
|
|
|
|
|
|
if (!actx) {
|
|
|
|
actx = new std::stack<ActivationContext::Ptr>();
|
|
|
|
m_ActivationStack.reset(actx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *actx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActivationContext::PushContext(const ActivationContext::Ptr& context)
|
|
|
|
{
|
|
|
|
GetActivationStack().push(context);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ActivationContext::PopContext()
|
2015-11-19 19:38:20 +01:00
|
|
|
{
|
|
|
|
ASSERT(!GetActivationStack().empty());
|
|
|
|
GetActivationStack().pop();
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
ActivationContext::Ptr ActivationContext::GetCurrentContext()
|
2015-11-19 19:38:20 +01:00
|
|
|
{
|
|
|
|
std::stack<ActivationContext::Ptr>& astack = GetActivationStack();
|
|
|
|
|
|
|
|
if (astack.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Objects may not be created outside of an activation context."));
|
|
|
|
|
|
|
|
return astack.top();
|
|
|
|
}
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
ActivationScope::ActivationScope(ActivationContext::Ptr context)
|
|
|
|
: m_Context(std::move(context))
|
2015-11-19 19:38:20 +01:00
|
|
|
{
|
|
|
|
if (!m_Context)
|
|
|
|
m_Context = new ActivationContext();
|
|
|
|
|
|
|
|
ActivationContext::PushContext(m_Context);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
ActivationScope::~ActivationScope()
|
2015-11-19 19:38:20 +01:00
|
|
|
{
|
|
|
|
ActivationContext::PopContext();
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
ActivationContext::Ptr ActivationScope::GetContext() const
|
2015-11-19 19:38:20 +01:00
|
|
|
{
|
|
|
|
return m_Context;
|
|
|
|
}
|
|
|
|
|