[mlpack-git] master: Refactor singletons into one .cpp file. (2954e48)

gitdub at mlpack.org gitdub at mlpack.org
Tue Oct 11 09:31:24 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/84a940db9d227a0cf094e93e526b902da1bb4e75...86480008b76dbd6b87b4092fb4f104342604f356

>---------------------------------------------------------------

commit 2954e48e5e3dff58b0096747d37ab869c7df7bd6
Author: Ryan Curtin <ryan at ratml.org>
Date:   Tue Oct 11 09:31:24 2016 -0400

    Refactor singletons into one .cpp file.
    
    This causes these singletons to only appear once in libmlpack.so and not in
    every executable.


>---------------------------------------------------------------

2954e48e5e3dff58b0096747d37ab869c7df7bd6
 src/mlpack/core/util/CMakeLists.txt              |  2 ++
 src/mlpack/core/util/cli.cpp                     |  2 --
 src/mlpack/core/util/cli_deleter.hpp             |  5 +--
 src/mlpack/core/util/log.cpp                     | 32 ------------------
 src/mlpack/core/util/{log.cpp => singletons.cpp} | 43 +++++++-----------------
 src/mlpack/core/util/singletons.hpp              | 21 ++++++++++++
 6 files changed, 36 insertions(+), 69 deletions(-)

diff --git a/src/mlpack/core/util/CMakeLists.txt b/src/mlpack/core/util/CMakeLists.txt
index b7830b1..84b1a9c 100644
--- a/src/mlpack/core/util/CMakeLists.txt
+++ b/src/mlpack/core/util/CMakeLists.txt
@@ -21,6 +21,8 @@ set(SOURCES
   prefixedoutstream.cpp
   prefixedoutstream_impl.hpp
   sfinae_utility.hpp
+  singletons.hpp
+  singletons.cpp
   timers.hpp
   timers.cpp
   version.hpp
diff --git a/src/mlpack/core/util/cli.cpp b/src/mlpack/core/util/cli.cpp
index aa87167..a90e93d 100644
--- a/src/mlpack/core/util/cli.cpp
+++ b/src/mlpack/core/util/cli.cpp
@@ -15,8 +15,6 @@
 using namespace mlpack;
 using namespace mlpack::util;
 
-CLI* CLI::singleton = NULL;
-
 /* For clarity, we will alias boost's namespace. */
 namespace po = boost::program_options;
 
diff --git a/src/mlpack/core/util/cli_deleter.hpp b/src/mlpack/core/util/cli_deleter.hpp
index 168db8e..f1c0193 100644
--- a/src/mlpack/core/util/cli_deleter.hpp
+++ b/src/mlpack/core/util/cli_deleter.hpp
@@ -24,10 +24,7 @@ class CLIDeleter
   ~CLIDeleter();
 };
 
-//! Declare the deleter.
-static CLIDeleter cliDeleter;
-
-} // namespace io
+} // namespace util
 } // namespace mlpack
 
 #endif
diff --git a/src/mlpack/core/util/log.cpp b/src/mlpack/core/util/log.cpp
index 17a8e48..6189f17 100644
--- a/src/mlpack/core/util/log.cpp
+++ b/src/mlpack/core/util/log.cpp
@@ -10,41 +10,9 @@
   #include "backtrace.hpp"
 #endif
 
-// Color code escape sequences -- but not on Windows.
-#ifndef _WIN32
-  #define BASH_RED "\033[0;31m"
-  #define BASH_GREEN "\033[0;32m"
-  #define BASH_YELLOW "\033[0;33m"
-  #define BASH_CYAN "\033[0;36m"
-  #define BASH_CLEAR "\033[0m"
-#else
-  #define BASH_RED ""
-  #define BASH_GREEN ""
-  #define BASH_YELLOW ""
-  #define BASH_CYAN ""
-  #define BASH_CLEAR ""
-#endif
-
 using namespace mlpack;
 using namespace mlpack::util;
 
-// Only output debugging output if in debug mode.
-#ifdef DEBUG
-PrefixedOutStream Log::Debug = PrefixedOutStream(std::cout,
-    BASH_CYAN "[DEBUG] " BASH_CLEAR);
-#else
-NullOutStream Log::Debug = NullOutStream();
-#endif
-
-PrefixedOutStream Log::Info = PrefixedOutStream(std::cout,
-    BASH_GREEN "[INFO ] " BASH_CLEAR, true /* unless --verbose */, false);
-PrefixedOutStream Log::Warn = PrefixedOutStream(std::cout,
-    BASH_YELLOW "[WARN ] " BASH_CLEAR, false, false);
-PrefixedOutStream Log::Fatal = PrefixedOutStream(std::cerr,
-    BASH_RED "[FATAL] " BASH_CLEAR, false, true /* fatal */);
-
-std::ostream& Log::cout = std::cout;
-
 // Only do anything for Assert() if in debugging mode.
 #ifdef DEBUG
 void Log::Assert(bool condition, const std::string& message)
diff --git a/src/mlpack/core/util/log.cpp b/src/mlpack/core/util/singletons.cpp
similarity index 63%
copy from src/mlpack/core/util/log.cpp
copy to src/mlpack/core/util/singletons.cpp
index 17a8e48..e0fcd23 100644
--- a/src/mlpack/core/util/log.cpp
+++ b/src/mlpack/core/util/singletons.cpp
@@ -1,14 +1,13 @@
 /**
- * @file log.cpp
- * @author Matthew Amidon
- *
- * Implementation of the Log class.
+ * @file singletons.cpp
+ * @author Ryan Curtin
  */
+#include "singletons.hpp"
 #include "log.hpp"
+#include "cli.hpp"
 
-#ifdef HAS_BFD_DL
-  #include "backtrace.hpp"
-#endif
+using namespace mlpack;
+using namespace mlpack::util;
 
 // Color code escape sequences -- but not on Windows.
 #ifndef _WIN32
@@ -25,8 +24,7 @@
   #define BASH_CLEAR ""
 #endif
 
-using namespace mlpack;
-using namespace mlpack::util;
+CLI* CLI::singleton = NULL;
 
 // Only output debugging output if in debug mode.
 #ifdef DEBUG
@@ -43,25 +41,8 @@ PrefixedOutStream Log::Warn = PrefixedOutStream(std::cout,
 PrefixedOutStream Log::Fatal = PrefixedOutStream(std::cerr,
     BASH_RED "[FATAL] " BASH_CLEAR, false, true /* fatal */);
 
-std::ostream& Log::cout = std::cout;
-
-// Only do anything for Assert() if in debugging mode.
-#ifdef DEBUG
-void Log::Assert(bool condition, const std::string& message)
-{
-  if (!condition)
-  {
-#ifdef HAS_BFD_DL
-    Backtrace bt;
-
-    Log::Debug << bt.ToString();
-#endif
-    Log::Debug << message << std::endl;
-
-    throw std::runtime_error("Log::Assert() failed: " + message);
-  }
-}
-#else
-void Log::Assert(bool /* condition */, const std::string& /* message */)
-{ }
-#endif
+/**
+ * This has to be last, so that the CLI object is destroyed before the Log
+ * output objects are destroyed.
+ */
+CLIDeleter cliDeleter;
diff --git a/src/mlpack/core/util/singletons.hpp b/src/mlpack/core/util/singletons.hpp
new file mode 100644
index 0000000..ec43cc8
--- /dev/null
+++ b/src/mlpack/core/util/singletons.hpp
@@ -0,0 +1,21 @@
+/**
+ * @file singletons.hpp
+ * @author Ryan Curtin
+ *
+ * Definitions of singletons used by libmlpack.so.
+ */
+#ifndef MLPACK_CORE_UTIL_SINGLETONS_HPP
+#define MLPACK_CORE_UTIL_SINGLETONS_HPP
+
+#include "cli_deleter.hpp"
+#include <mlpack/mlpack_export.hpp>
+
+namespace mlpack {
+namespace util {
+
+extern MLPACK_EXPORT CLIDeleter cliDeleter;
+
+} // namespace util
+} // namespace mlpack
+
+#endif




More information about the mlpack-git mailing list