LUMIERA.clone/src/lib/dependency-factory.cpp
Ichthyostega 52c83b860b DependencyFactory: remove the ability to restart a service explicitly
We don't need this ability and it pushes us into using a
central registry. This solution turned out to be problematic
when loading dynamic libraries (plug-ins).
2013-10-21 02:06:01 +02:00

98 lines
2.9 KiB
C++

/*
DependencyFactory - managing the lifecycle of singletons and dependencies
Copyright (C) Lumiera.org
2013, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
/** @file dependency-factory.cpp
** This compilation unit holds the common backend of all singleton and dependency factories.
** Especially the code for tracking and clean-up of all service instance is located here.
** This clean-up is triggered by the invocation of \c ~AutoDestructor() -- at this point
** a special \c shutdownLock is set, which prevents any further singleton service creation.
**
*/
#include "lib/dependency-factory.hpp"
#include "lib/del-stash.hpp"
namespace lib {
namespace error = lumiera::error;
namespace { // private implementation details...
class AutoDestructor
{
DelStash destructionExecutor_;
static bool shutdownLock;
static AutoDestructor&
instance()
{
static AutoDestructor _instance_;
return _instance_;
}
~AutoDestructor()
{
shutdownLock = true;
}
static void
__lifecycleCheck()
{
if (shutdownLock)
throw error::Fatal("Attempt to re-access a service, "
"while Application is already in shutdown"
,error::LUMIERA_ERROR_LIFECYCLE);
}
public:
static void
schedule (void* object, DependencyFactory::KillFun customDeleter)
{
__lifecycleCheck();
instance().destructionExecutor_.manage (object, customDeleter);
}
};
bool AutoDestructor::shutdownLock = false;
}
/** hook to install a deleter function to clean up a service object.
* The standard constructor function uses this hook to schedule the
* destructor invocation on application shutdown; custom constructors
* are free to use this mechanism (or care for clean-up otherwise)
* @see lib::DelStash
*/
void
DependencyFactory::scheduleDestruction (void* object, KillFun customDeleter)
{
AutoDestructor::schedule (object, customDeleter);
}
}// lib