Implement INITIALIZE_ONCE().

This commit is contained in:
Gunnar Beutner 2013-08-01 11:07:26 +02:00
parent cd4ebf1bf2
commit bdd2adc85e
6 changed files with 50 additions and 12 deletions

View File

@ -30,6 +30,7 @@ libbase_la_SOURCES = \
filelogger.cpp \
filelogger.h \
i2-base.h \
initialize.h \
logger.cpp \
logger.h \
logger_fwd.h \

42
lib/base/initialize.h Normal file
View File

@ -0,0 +1,42 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef UTILITY_H
#define UTILITY_H
#include "base/i2-base.h"
namespace icinga
{
typedef void (*InitializeFunc)(void);
inline bool InitializeOnceHelper(InitializeFunc func)
{
func();
return true;
}
#define INITIALIZE_ONCE(func) \
static bool l_InitializeOnce ## __LINE__(InitializeOnceHelper(func));
}
#endif /* UTILITY_H */

View File

@ -22,12 +22,15 @@
#include "icinga/icingaapplication.h"
#include "base/dynamictype.h"
#include "base/utility.h"
#include "base/initialize.h"
#include <boost/foreach.hpp>
using namespace icinga;
Timer::Ptr DbConnection::m_ProgramStatusTimer;
INITIALIZE_ONCE(&DbConnection::StaticInitialize);
DbConnection::DbConnection(const Dictionary::Ptr& serializedUpdate)
: DynamicObject(serializedUpdate)
{

View File

@ -24,6 +24,7 @@
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/utility.h"
#include "base/initialize.h"
#include "base/logger_fwd.h"
#include <boost/foreach.hpp>
@ -33,6 +34,8 @@ boost::signals2::signal<void (const DbObject::Ptr&)> DbObject::OnRegistered;
boost::signals2::signal<void (const DbObject::Ptr&)> DbObject::OnUnregistered;
boost::signals2::signal<void (const DbQuery&)> DbObject::OnQuery;
INITIALIZE_ONCE(&DbObject::StaticInitialize);
DbObject::DbObject(const shared_ptr<DbType>& type, const String& name1, const String& name2)
: m_Name1(name1), m_Name2(name2), m_Type(type), m_LastConfigUpdate(0), m_LastStatusUpdate(0)
{ }

View File

@ -31,16 +31,7 @@ boost::mutex DbType::m_StaticMutex;
DbType::DbType(const String& name, const String& table, long tid, const DbType::ObjectFactory& factory)
: m_Name(name), m_Table(table), m_TypeID(tid), m_ObjectFactory(factory)
{
static boost::once_flag initializeOnce = BOOST_ONCE_INIT;
boost::call_once(initializeOnce, &DbType::StaticInitialize);
}
void DbType::StaticInitialize(void)
{
DbConnection::StaticInitialize();
DbObject::StaticInitialize();
}
{ }
String DbType::GetName(void) const
{

View File

@ -64,8 +64,6 @@ private:
static TypeMap& GetTypes(void);
ObjectMap& GetObjects(void);
static void StaticInitialize(void);
};
/**