]> granicus.if.org Git - icinga2/commitdiff
Implement INITIALIZE_ONCE().
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 1 Aug 2013 09:07:26 +0000 (11:07 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 1 Aug 2013 09:08:06 +0000 (11:08 +0200)
lib/base/Makefile.am
lib/base/initialize.h [new file with mode: 0644]
lib/ido/dbconnection.cpp
lib/ido/dbobject.cpp
lib/ido/dbtype.cpp
lib/ido/dbtype.h

index c14525a83385a7508112fc69c984bc8a17ed9e48..0e5de80f55999e155fbeaf39c3684f7c354012e7 100644 (file)
@@ -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 (file)
index 0000000..580ec8d
--- /dev/null
@@ -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 */
index 2d39f88e5d3939018efabd8c76c090f8a67e2ee8..5fd6ec2332c6d917574864e88edb4ef9ed5d55e3 100644 (file)
 #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)
 {
index c5f01ac6211a8d1c86e0c9517f9e7e68655dfd84..7d6327f967a69599a936824c26d933e1d9257f06 100644 (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)
 { }
index 7c7c4c21a963870c8822209afd25889281e1579f..112fa26a83759f0a8fe96ea823d5a5d1bdd94615 100644 (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
 {
index 0c3797c596196d84187c9c384fd31b800f9d7792..005d06f083d565b3abbffdb8c822d856ec18a8f7 100644 (file)
@@ -64,8 +64,6 @@ private:
        static TypeMap& GetTypes(void);
 
        ObjectMap& GetObjects(void);
-
-       static void StaticInitialize(void);
 };
 
 /**