From: Gunnar Beutner Date: Thu, 1 Aug 2013 09:07:26 +0000 (+0200) Subject: Implement INITIALIZE_ONCE(). X-Git-Tag: v0.0.3~765 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bdd2adc85e7a90b8da32accb73957a30c3c18639;p=icinga2 Implement INITIALIZE_ONCE(). --- diff --git a/lib/base/Makefile.am b/lib/base/Makefile.am index c14525a83..0e5de80f5 100644 --- a/lib/base/Makefile.am +++ b/lib/base/Makefile.am @@ -30,6 +30,7 @@ libbase_la_SOURCES = \ filelogger.cpp \ filelogger.h \ i2-base.h \ + initialize.h \ logger.cpp \ logger.h \ logger_fwd.h \ diff --git a/lib/base/initialize.h b/lib/base/initialize.h new file mode 100644 index 000000000..580ec8d34 --- /dev/null +++ b/lib/base/initialize.h @@ -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 */ diff --git a/lib/ido/dbconnection.cpp b/lib/ido/dbconnection.cpp index 2d39f88e5..5fd6ec233 100644 --- a/lib/ido/dbconnection.cpp +++ b/lib/ido/dbconnection.cpp @@ -22,12 +22,15 @@ #include "icinga/icingaapplication.h" #include "base/dynamictype.h" #include "base/utility.h" +#include "base/initialize.h" #include using namespace icinga; Timer::Ptr DbConnection::m_ProgramStatusTimer; +INITIALIZE_ONCE(&DbConnection::StaticInitialize); + DbConnection::DbConnection(const Dictionary::Ptr& serializedUpdate) : DynamicObject(serializedUpdate) { diff --git a/lib/ido/dbobject.cpp b/lib/ido/dbobject.cpp index c5f01ac62..7d6327f96 100644 --- a/lib/ido/dbobject.cpp +++ b/lib/ido/dbobject.cpp @@ -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 @@ -33,6 +34,8 @@ boost::signals2::signal DbObject::OnRegistered; boost::signals2::signal DbObject::OnUnregistered; boost::signals2::signal DbObject::OnQuery; +INITIALIZE_ONCE(&DbObject::StaticInitialize); + DbObject::DbObject(const shared_ptr& type, const String& name1, const String& name2) : m_Name1(name1), m_Name2(name2), m_Type(type), m_LastConfigUpdate(0), m_LastStatusUpdate(0) { } diff --git a/lib/ido/dbtype.cpp b/lib/ido/dbtype.cpp index 7c7c4c21a..112fa26a8 100644 --- a/lib/ido/dbtype.cpp +++ b/lib/ido/dbtype.cpp @@ -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 { diff --git a/lib/ido/dbtype.h b/lib/ido/dbtype.h index 0c3797c59..005d06f08 100644 --- a/lib/ido/dbtype.h +++ b/lib/ido/dbtype.h @@ -64,8 +64,6 @@ private: static TypeMap& GetTypes(void); ObjectMap& GetObjects(void); - - static void StaticInitialize(void); }; /**