From d6042065eca4f90b4846f1788ffa07716b465618 Mon Sep 17 00:00:00 2001 From: aleks-f Date: Sat, 9 Feb 2013 22:39:07 -0600 Subject: [PATCH] Auto init for Net and SQLite (win) Auto init for Net and SQLite (only tested on Windows) --- CppUnit/WinTestRunner/src/ActiveTest.h | 16 +- Data/SQLite/SQLite_vs100.vcxproj | 156 +++++++++--------- .../include/Poco/Data/SQLite/Connector.h | 59 ++++++- Data/SQLite/include/Poco/Data/SQLite/SQLite.h | 2 +- .../include/Poco/Data/SQLite/SessionImpl.h | 2 +- Data/SQLite/src/Connector.cpp | 7 +- Data/SQLite/src/SessionImpl.cpp | 1 - Data/SQLite/testsuite/src/SQLiteTest.cpp | 2 - Data/samples/Binding/src/Binding.cpp | 3 - .../samples/RecordSet/RecordSet_vs100.vcxproj | 134 +++++++-------- Data/samples/RecordSet/src/RecordSet.cpp | 3 - .../samples/RowFormatter/src/RowFormatter.cpp | 3 - Data/samples/Tuple/src/Tuple.cpp | 3 - Data/samples/TypeHandler/src/TypeHandler.cpp | 3 - Foundation/include/Poco/Config.h | 8 + Net/include/Poco/Net/Net.h | 36 ++-- Net/src/Net.cpp | 71 +++----- 17 files changed, 273 insertions(+), 236 deletions(-) diff --git a/CppUnit/WinTestRunner/src/ActiveTest.h b/CppUnit/WinTestRunner/src/ActiveTest.h index 232747a9c..bf9df5e7d 100644 --- a/CppUnit/WinTestRunner/src/ActiveTest.h +++ b/CppUnit/WinTestRunner/src/ActiveTest.h @@ -38,16 +38,16 @@ public: ActiveTest(Test* test); ~ActiveTest(); - void run(TestResult* result); + void run(TestResult* result); protected: - HANDLE _threadHandle; - CEvent _runCompleted; - TestResult* _currentTestResult; - - void run (); - void setTestResult(TestResult* result); - static UINT threadFunction(LPVOID thisInstance); + HANDLE _threadHandle; + CEvent _runCompleted; + TestResult* _currentTestResult; + + void run (); + void setTestResult(TestResult* result); + static UINT threadFunction(LPVOID thisInstance); }; diff --git a/Data/SQLite/SQLite_vs100.vcxproj b/Data/SQLite/SQLite_vs100.vcxproj index 64a095484..c7d8b1cb8 100644 --- a/Data/SQLite/SQLite_vs100.vcxproj +++ b/Data/SQLite/SQLite_vs100.vcxproj @@ -1,4 +1,4 @@ - + @@ -32,76 +32,76 @@ SQLite Win32Proj - - + + StaticLibrary MultiByte - + StaticLibrary MultiByte - + StaticLibrary MultiByte - + StaticLibrary MultiByte - + DynamicLibrary MultiByte - + DynamicLibrary MultiByte - - - - + + + + - - + + - - + + - - + + - - + + - - + + - + <_ProjectFileVersion>10.0.40219.1 - ..\..\bin\ - obj\$(Configuration)\ - true - ..\..\bin\ - obj\$(Configuration)\ - false - ..\..\lib\ - obj\$(Configuration)\ - ..\..\lib\ - obj\$(Configuration)\ - ..\..\lib\ - obj\$(Configuration)\ - ..\..\lib\ - obj\$(Configuration)\ - PocoDataSQLited - PocoDataSQLitemdd - PocoDataSQLitemtd - PocoDataSQLite - PocoDataSQLitemd - PocoDataSQLitemt + ..\..\bin\ + obj\$(Configuration)\ + true + ..\..\bin\ + obj\$(Configuration)\ + false + ..\..\lib\ + obj\$(Configuration)\ + ..\..\lib\ + obj\$(Configuration)\ + ..\..\lib\ + obj\$(Configuration)\ + ..\..\lib\ + obj\$(Configuration)\ + PocoDataSQLited + PocoDataSQLitemdd + PocoDataSQLitemtd + PocoDataSQLite + PocoDataSQLitemd + PocoDataSQLitemt - + Disabled .\include;..\..\Foundation\include;..\..\Data\include;%(AdditionalIncludeDirectories) @@ -114,7 +114,7 @@ true true true - + Level3 EditAndContinue Default @@ -132,7 +132,7 @@ MachineX86 - + Disabled OnlyExplicitInline @@ -147,9 +147,9 @@ true true true - + Level3 - + Default 4996;4244;4018;%(DisableSpecificWarnings) @@ -166,7 +166,7 @@ MachineX86 - + Disabled .\include;..\..\Foundation\include;..\..\Data\include;%(AdditionalIncludeDirectories) @@ -179,7 +179,7 @@ true true true - + ..\..\lib\PocoDataSQLitemtd.pdb Level3 EditAndContinue @@ -190,7 +190,7 @@ ..\..\lib\PocoDataSQLitemtd.lib - + Disabled OnlyExplicitInline @@ -205,9 +205,9 @@ true true true - + Level3 - + Default 4996;4244;4018;%(DisableSpecificWarnings) @@ -215,7 +215,7 @@ ..\..\lib\PocoDataSQLitemt.lib - + Disabled .\include;..\..\Foundation\include;..\..\Data\include;%(AdditionalIncludeDirectories) @@ -228,7 +228,7 @@ true true true - + ..\..\lib\PocoDataSQLitemdd.pdb Level3 EditAndContinue @@ -239,7 +239,7 @@ ..\..\lib\PocoDataSQLitemdd.lib - + Disabled OnlyExplicitInline @@ -254,10 +254,10 @@ true true true - + ..\..\lib\PocoDataSQLitemd.pdb Level3 - + Default 4996;4244;4018;%(DisableSpecificWarnings) @@ -267,26 +267,26 @@ - - - - - - - - - + + + + + + + + + - - - - - - - - + + + + + + + + - - - + + + \ No newline at end of file diff --git a/Data/SQLite/include/Poco/Data/SQLite/Connector.h b/Data/SQLite/include/Poco/Data/SQLite/Connector.h index eefe6961f..2ee8afcc6 100644 --- a/Data/SQLite/include/Poco/Data/SQLite/Connector.h +++ b/Data/SQLite/include/Poco/Data/SQLite/Connector.h @@ -44,6 +44,12 @@ #include "Poco/Data/Connector.h" +// Note: to avoid static (de)initialization problems, +// during connector automatic (un)registration, it is +// best to have this as a macro. +#define POCO_DATA_SQLITE_CONNECTOR_NAME "sqlite" + + namespace Poco { namespace Data { namespace SQLite { @@ -91,11 +97,62 @@ public: /// inline const std::string& Connector::name() const { - return KEY; + static const std::string name(POCO_DATA_SQLITE_CONNECTOR_NAME); + return name; } } } } // namespace Poco::Data::SQLite +// +// Automatic Connector registration +// + +struct SQLite_API SQLiteConnectorRegistrator + /// Connector registering class. + /// A global instance of this class is instantiated + /// with sole purpose to automatically register the + /// SQLite connector with central Poco Data registry. +{ + SQLiteConnectorRegistrator() + /// Calls Poco::Data::SQLite::registerConnector(); + { + Poco::Data::SQLite::Connector::registerConnector(); + } + + ~SQLiteConnectorRegistrator() + /// Calls Poco::Data::SQLite::unregisterConnector(); + { + Poco::Data::SQLite::Connector::unregisterConnector(); + } +}; + + +#if !defined(POCO_NO_AUTOMATIC_LIB_INIT) + #if defined(POCO_OS_FAMILY_WINDOWS) + extern "C" const struct SQLite_API SQLiteConnectorRegistrator pocoSQLiteConnectorRegistrator; + #if defined(SQLite_EXPORTS) + #if defined(_WIN64) + #define POCO_DATA_SQLITE_FORCE_SYMBOL(s) __pragma(comment (linker, "/export:"#s)) + #elif defined(_WIN32) + #define POCO_DATA_SQLITE_FORCE_SYMBOL(s) __pragma(comment (linker, "/export:_"#s)) + #endif + #else // SQLite_EXPORTS + #if defined(_WIN64) + #define POCO_DATA_SQLITE_FORCE_SYMBOL(s) __pragma(comment (linker, "/include:"#s)) + #elif defined(_WIN32) + #define POCO_DATA_SQLITE_FORCE_SYMBOL(s) __pragma(comment (linker, "/include:_"#s)) + #endif + #endif // SQLite_EXPORTS + #else // !POCO_OS_FAMILY_WINDOWS + #define POCO_DATA_SQLITE_FORCE_SYMBOL(s) extern "C" const struct SQLiteConnectorRegistrator s; + #endif // POCO_OS_FAMILY_WINDOWS + POCO_DATA_SQLITE_FORCE_SYMBOL(pocoSQLiteConnectorRegistrator) +#endif // POCO_NO_AUTOMATIC_LIB_INIT + +// +// End automatic Connector registration +// + #endif // Data_SQLite_Connector_INCLUDED diff --git a/Data/SQLite/include/Poco/Data/SQLite/SQLite.h b/Data/SQLite/include/Poco/Data/SQLite/SQLite.h index ea013103a..15ea80b9b 100644 --- a/Data/SQLite/include/Poco/Data/SQLite/SQLite.h +++ b/Data/SQLite/include/Poco/Data/SQLite/SQLite.h @@ -84,7 +84,7 @@ // // Thread safety mode defaults to "serialized". // See http://www.sqlite.org/threadsafe.html for details. -// Threading mode significantly affects performance +// Threading mode may significantly affect performance // (see TestSuite::benchmarkThreadModesTiming) // #ifndef SQLITE_THREADSAFE diff --git a/Data/SQLite/include/Poco/Data/SQLite/SessionImpl.h b/Data/SQLite/include/Poco/Data/SQLite/SessionImpl.h index 912c74c57..a9118087b 100644 --- a/Data/SQLite/include/Poco/Data/SQLite/SessionImpl.h +++ b/Data/SQLite/include/Poco/Data/SQLite/SessionImpl.h @@ -41,6 +41,7 @@ #include "Poco/Data/SQLite/SQLite.h" +#include "Poco/Data/SQLite/Connector.h" #include "Poco/Data/SQLite/Binder.h" #include "Poco/Data/AbstractSessionImpl.h" #include "Poco/SharedPtr.h" @@ -128,7 +129,6 @@ public: /// Returns the name of the connector. private: - std::string _connector; sqlite3* _pDB; bool _connected; diff --git a/Data/SQLite/src/Connector.cpp b/Data/SQLite/src/Connector.cpp index c4c6fa6db..70de69a80 100644 --- a/Data/SQLite/src/Connector.cpp +++ b/Data/SQLite/src/Connector.cpp @@ -44,12 +44,15 @@ #endif +const SQLiteConnectorRegistrator pocoSQLiteConnectorRegistrator; + + namespace Poco { namespace Data { namespace SQLite { -const std::string Connector::KEY("sqlite"); +const std::string Connector::KEY(POCO_DATA_SQLITE_CONNECTOR_NAME); Connector::Connector() @@ -77,7 +80,7 @@ void Connector::registerConnector() void Connector::unregisterConnector() { - Poco::Data::SessionFactory::instance().remove(KEY); + Poco::Data::SessionFactory::instance().remove(POCO_DATA_SQLITE_CONNECTOR_NAME); } diff --git a/Data/SQLite/src/SessionImpl.cpp b/Data/SQLite/src/SessionImpl.cpp index 91bbb0029..9dde4c34d 100644 --- a/Data/SQLite/src/SessionImpl.cpp +++ b/Data/SQLite/src/SessionImpl.cpp @@ -37,7 +37,6 @@ #include "Poco/Data/SQLite/SessionImpl.h" #include "Poco/Data/SQLite/Utility.h" #include "Poco/Data/SQLite/SQLiteStatementImpl.h" -#include "Poco/Data/SQLite/Connector.h" #include "Poco/Data/SQLite/SQLiteException.h" #include "Poco/Data/Session.h" #include "Poco/ActiveMethod.h" diff --git a/Data/SQLite/testsuite/src/SQLiteTest.cpp b/Data/SQLite/testsuite/src/SQLiteTest.cpp index 443ed141b..ee3a9a9e4 100644 --- a/Data/SQLite/testsuite/src/SQLiteTest.cpp +++ b/Data/SQLite/testsuite/src/SQLiteTest.cpp @@ -251,13 +251,11 @@ private: SQLiteTest::SQLiteTest(const std::string& name): CppUnit::TestCase(name) { - Poco::Data::SQLite::Connector::registerConnector(); } SQLiteTest::~SQLiteTest() { - Poco::Data::SQLite::Connector::unregisterConnector(); } diff --git a/Data/samples/Binding/src/Binding.cpp b/Data/samples/Binding/src/Binding.cpp index 214b1fedd..07964acd8 100644 --- a/Data/samples/Binding/src/Binding.cpp +++ b/Data/samples/Binding/src/Binding.cpp @@ -52,9 +52,6 @@ struct Person int main(int argc, char** argv) { - // register SQLite connector - Poco::Data::SQLite::Connector::registerConnector(); - // create a session Session session("SQLite", "sample.db"); diff --git a/Data/samples/RecordSet/RecordSet_vs100.vcxproj b/Data/samples/RecordSet/RecordSet_vs100.vcxproj index 47b6e5310..234912723 100644 --- a/Data/samples/RecordSet/RecordSet_vs100.vcxproj +++ b/Data/samples/RecordSet/RecordSet_vs100.vcxproj @@ -1,4 +1,4 @@ - + @@ -32,84 +32,84 @@ RecordSet Win32Proj - - + + Application MultiByte - + Application MultiByte - + Application MultiByte - + Application MultiByte - + Application MultiByte - + Application MultiByte - - - - + + + + - - + + - - + + - - + + - - + + - - + + - + <_ProjectFileVersion>10.0.40219.1 - bin\ - obj\$(Configuration)\ - true - bin\ - obj\$(Configuration)\ - false - bin\static_mt\ - obj\$(Configuration)\ - true - bin\static_mt\ - obj\$(Configuration)\ - false - bin\static_md\ - obj\$(Configuration)\ - true - bin\static_md\ - obj\$(Configuration)\ - false - RecordSetd - RecordSetd - RecordSetd - RecordSet - RecordSet - RecordSet + bin\ + obj\$(Configuration)\ + true + bin\ + obj\$(Configuration)\ + false + bin\static_mt\ + obj\$(Configuration)\ + true + bin\static_mt\ + obj\$(Configuration)\ + false + bin\static_md\ + obj\$(Configuration)\ + true + bin\static_md\ + obj\$(Configuration)\ + false + RecordSetd + RecordSetd + RecordSetd + RecordSet + RecordSet + RecordSet - + Disabled .\include;..\..\..\Foundation\include;..\..\..\XML\include;..\..\..\Util\include;..\..\..\Data\include;..\..\..\Data\SQLite\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;_WINDOWS;WINVER=0x0500;%(PreprocessorDefinitions) + WIN32;_DEBUG;_WINDOWS;WINVER=0x0500;POCO_DLL;%(PreprocessorDefinitions) true true EnableFastChecks @@ -118,7 +118,7 @@ true true true - + Level3 EditAndContinue Default @@ -135,7 +135,7 @@ MachineX86 - + Disabled OnlyExplicitInline @@ -150,9 +150,9 @@ true true true - + Level3 - + Default %(DisableSpecificWarnings) @@ -167,7 +167,7 @@ MachineX86 - + Disabled .\include;..\..\..\Foundation\include;..\..\..\XML\include;..\..\..\Util\include;..\..\..\Data\include;..\..\..\Data\SQLite\include;%(AdditionalIncludeDirectories) @@ -180,7 +180,7 @@ true true true - + Level3 EditAndContinue Default @@ -197,7 +197,7 @@ MachineX86 - + Disabled OnlyExplicitInline @@ -212,9 +212,9 @@ true true true - + Level3 - + Default %(DisableSpecificWarnings) @@ -229,7 +229,7 @@ MachineX86 - + Disabled .\include;..\..\..\Foundation\include;..\..\..\XML\include;..\..\..\Util\include;..\..\..\Data\include;..\..\..\Data\SQLite\include;%(AdditionalIncludeDirectories) @@ -242,7 +242,7 @@ true true true - + Level3 EditAndContinue Default @@ -259,7 +259,7 @@ MachineX86 - + Disabled OnlyExplicitInline @@ -274,9 +274,9 @@ true true true - + Level3 - + Default %(DisableSpecificWarnings) @@ -292,8 +292,8 @@ - + - - - + + + \ No newline at end of file diff --git a/Data/samples/RecordSet/src/RecordSet.cpp b/Data/samples/RecordSet/src/RecordSet.cpp index a1a4823fb..f9a6c438c 100644 --- a/Data/samples/RecordSet/src/RecordSet.cpp +++ b/Data/samples/RecordSet/src/RecordSet.cpp @@ -58,9 +58,6 @@ struct Person int main(int argc, char** argv) { - // register SQLite connector - Poco::Data::SQLite::Connector::registerConnector(); - // create a session Session session("SQLite", "sample.db"); diff --git a/Data/samples/RowFormatter/src/RowFormatter.cpp b/Data/samples/RowFormatter/src/RowFormatter.cpp index 37da2ac10..87b8f80eb 100644 --- a/Data/samples/RowFormatter/src/RowFormatter.cpp +++ b/Data/samples/RowFormatter/src/RowFormatter.cpp @@ -103,9 +103,6 @@ public: int main(int argc, char** argv) { - // register SQLite connector - Poco::Data::SQLite::Connector::registerConnector(); - // create a session Session session("SQLite", "sample.db"); diff --git a/Data/samples/Tuple/src/Tuple.cpp b/Data/samples/Tuple/src/Tuple.cpp index a0aa8636d..91c60384b 100644 --- a/Data/samples/Tuple/src/Tuple.cpp +++ b/Data/samples/Tuple/src/Tuple.cpp @@ -50,9 +50,6 @@ int main(int argc, char** argv) typedef Poco::Tuple Person; typedef std::vector People; - // register SQLite connector - Poco::Data::SQLite::Connector::registerConnector(); - // create a session Session session("SQLite", "sample.db"); diff --git a/Data/samples/TypeHandler/src/TypeHandler.cpp b/Data/samples/TypeHandler/src/TypeHandler.cpp index 52d8315f1..8e193d3e2 100644 --- a/Data/samples/TypeHandler/src/TypeHandler.cpp +++ b/Data/samples/TypeHandler/src/TypeHandler.cpp @@ -103,9 +103,6 @@ public: int main(int argc, char** argv) { - // register SQLite connector - Poco::Data::SQLite::Connector::registerConnector(); - // create a session Session session("SQLite", "sample.db"); diff --git a/Foundation/include/Poco/Config.h b/Foundation/include/Poco/Config.h index 49cd79bc0..2924e61e1 100644 --- a/Foundation/include/Poco/Config.h +++ b/Foundation/include/Poco/Config.h @@ -51,6 +51,14 @@ // #define POCO_NO_AUTOMATIC_LIBS +// Define to disable automatic initialization +// Defining this will disable ALL automatic +// initialization framework-wide (e.g. Net +// on Windows, all Data back-ends, etc). +// +// #define POCO_NO_AUTOMATIC_LIB_INIT + + // Define to disable FPEnvironment support // #define POCO_NO_FPENVIRONMENT diff --git a/Net/include/Poco/Net/Net.h b/Net/include/Poco/Net/Net.h index ebe456cb6..dcfad07fb 100644 --- a/Net/include/Poco/Net/Net.h +++ b/Net/include/Poco/Net/Net.h @@ -95,28 +95,42 @@ namespace Net { inline void Net_API initializeNetwork(); /// Initialize the network subsystem. + /// (Windows only, no-op elsewhere) inline void Net_API uninitializeNetwork(); /// Uninitialize the network subsystem. + /// (Windows only, no-op elsewhere) }} // namespace Poco::Net // -// Automate network initialization on Windows. +// Automate network initialization (only relevant on Windows). // -#if defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_NET_NO_WINDOWS_INIT) - #if defined(POCO_STATIC) - extern "C" const struct NetworkInitializer pocoNetworkInitializer; - #ifdef _WIN64 - #pragma comment(linker, "/include:pocoNetworkInitializer") - #else - #pragma comment(linker, "/include:_pocoNetworkInitializer") - #endif - #endif // POCO_STATIC -#endif // POCO_NET_NO_WINDOWS_INIT + +#if defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_NO_AUTOMATIC_LIB_INIT) + +extern "C" const struct Net_API NetworkInitializer pocoNetworkInitializer; + +#if defined(Net_EXPORTS) + #if defined(_WIN64) + #define POCO_NET_FORCE_SYMBOL(s) __pragma(comment (linker, "/export:"#s)) + #elif defined(_WIN32) + #define POCO_NET_FORCE_SYMBOL(s) __pragma(comment (linker, "/export:_"#s)) + #endif +#else // !Net_EXPORTS + #if defined(_WIN64) + #define POCO_NET_FORCE_SYMBOL(s) __pragma(comment (linker, "/include:"#s)) + #elif defined(_WIN32) + #define POCO_NET_FORCE_SYMBOL(s) __pragma(comment (linker, "/include:_"#s)) + #endif +#endif // Net_EXPORTS + +POCO_NET_FORCE_SYMBOL(pocoNetworkInitializer) + +#endif // POCO_OS_FAMILY_WINDOWS // diff --git a/Net/src/Net.cpp b/Net/src/Net.cpp index 2aa9c2f2a..520e22d13 100644 --- a/Net/src/Net.cpp +++ b/Net/src/Net.cpp @@ -37,9 +37,6 @@ #include "Poco/Net/Net.h" -#if defined(POCO_OS_FAMILY_WINDOWS) - - #include "Poco/Net/SocketDefs.h" #include "Poco/Net/NetException.h" @@ -50,68 +47,44 @@ namespace Net { void Net_API initializeNetwork() { +#if defined(POCO_OS_FAMILY_WINDOWS) WORD version = MAKEWORD(2, 2); WSADATA data; if (WSAStartup(version, &data) != 0) throw NetException("Failed to initialize network subsystem"); +#endif } void Net_API uninitializeNetwork() { +#if defined(POCO_OS_FAMILY_WINDOWS) WSACleanup(); +#endif } } } // namespace Poco::Net -#if !defined(POCO_NET_NO_WINDOWS_INIT) - #if defined (POCO_DLL) - BOOL APIENTRY DllMain(HANDLE, DWORD reasonForCall, LPVOID) - { - switch(reasonForCall) - { - case DLL_PROCESS_ATTACH: - Poco::Net::initializeNetwork(); - break; - case DLL_PROCESS_DETACH: - Poco::Net::uninitializeNetwork(); - } - return TRUE; - } - #else // POCO_STATIC - struct NetworkInitializer - /// Network initializer for windows statically - /// linked library. - { - NetworkInitializer() - /// Calls Poco::Net::initializeNetwork(); - { - Poco::Net::initializeNetwork(); - } - - ~NetworkInitializer() - /// Calls Poco::Net::uninitializeNetwork(); - { - Poco::Net::uninitializeNetwork(); - } - }; - - const NetworkInitializer pocoNetworkInitializer; - #endif // POCO_DLL/POCO_STATIC - -#endif // POCO_NET_NO_WINDOWS_INIT - - -#else // POCO_OS_FAMILY_WINDOWS - -void Net_API initializeNetwork() +struct NetworkInitializer + /// Network initializer for windows statically + /// linked library. { -} + NetworkInitializer() + /// Calls Poco::Net::initializeNetwork(); + { + Poco::Net::initializeNetwork(); + } -void Net_API uninitializeNetwork() -{ -} + ~NetworkInitializer() + /// Calls Poco::Net::uninitializeNetwork(); + { + Poco::Net::uninitializeNetwork(); + } +}; -#endif // POCO_OS_FAMILY_WINDOWS + +#ifndef POCO_NO_AUTOMATIC_LIB_INIT + const NetworkInitializer pocoNetworkInitializer; +#endif