]> www.ginac.de Git - cln.git/commitdiff
Replace CL_REQUIRE/CL_PROVIDE(cl_prin_globals) with portable code.
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Thu, 21 Aug 2008 09:56:15 +0000 (13:56 +0400)
committerAlexei Sheplyakov <varg@theor.jinr.ru>
Wed, 27 Aug 2008 04:41:02 +0000 (08:41 +0400)
The order of initialization of non-local objects in different compilation units
is not specified in C++. Hence special care should be taken to avoid static
initialization order fiasco. CLN solved the problem with some evil (GCC
specific, and even GCC-version-specific) hack. Replace it with a technique
similar to one used in STL to initialize std::cout and friends.

include/cln/io.h
src/base/output/cl_prin_globals.cc

index cc8ca1e51dab0a10f44bd9b3b5c59f3bdae55da7..435490e8545756110bf222eec06595ecc99c7b98 100644 (file)
@@ -88,8 +88,15 @@ class cl_print_number_flags;
 class cl_print_real_flags;
 class cl_print_rational_flags;
 class cl_print_float_flags;
-CL_REQUIRE(cl_prin_globals)
 
+class cl_prin_globals_init_helper
+{
+       static int count;
+public:
+       cl_prin_globals_init_helper();
+       ~cl_prin_globals_init_helper();
+};
+static cl_prin_globals_init_helper cl_prin_globals_init_helper_instance;
 
 // Define the customary << and >> operators.
 
index 543c865d93ea8ba9a7fac71da54f71707743b0ef..fd61467303570f0884e56af3bd298ab3335cd2fc 100644 (file)
@@ -3,8 +3,6 @@
 // General includes.
 #include "cl_sysdep.h"
 
-CL_PROVIDE(cl_prin_globals)
-
 // Specification.
 #include "cln/output.h"
 
@@ -14,6 +12,23 @@ CL_PROVIDE(cl_prin_globals)
 namespace cln {
 
 cl_print_flags default_print_flags;
+
+int cl_prin_globals_init_helper::count = 0;
+
+cl_prin_globals_init_helper::cl_prin_globals_init_helper()
+{
+       if (count++ == 0)
+               new ((void *)&default_print_flags) cl_print_flags();
+}
+
+cl_prin_globals_init_helper::~cl_prin_globals_init_helper()
+{
+       if (--count == 0) {
+               // Nothing to clean up.
+       }
+}
+
+
 #if 0 // The default constructors already do this.
 AT_INITIALIZATION(default_print_flags)
 {
@@ -29,4 +44,3 @@ AT_INITIALIZATION(default_print_flags)
 
 }  // namespace cln
 
-CL_PROVIDE_END(cl_prin_globals)