From 164e784d773cef093f46b6aed156269fe46bf676 Mon Sep 17 00:00:00 2001 From: dsarrut Date: Tue, 15 Feb 2011 10:41:19 +0000 Subject: [PATCH] add option to display memory usage if statgrab is installed add fromString to convert numbers from String --- common/clitkCommon.cxx | 27 +++++++++++++++++++++++++-- common/clitkCommon.h | 16 ++++++++++++++++ common/clitkCommon.txx | 13 +++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/common/clitkCommon.cxx b/common/clitkCommon.cxx index 7afeb07..df47f51 100644 --- a/common/clitkCommon.cxx +++ b/common/clitkCommon.cxx @@ -19,10 +19,14 @@ #ifndef CLITKCOMMON_CXX #define CLITKCOMMON_CXX +// clitk include #include "clitkCommon.h" + +// std include #include #include #include +#include //------------------------------------------------------------------ // skip line which begin with a sharp '#' @@ -240,7 +244,9 @@ void clitk::openFileForReading(std::ifstream & is, const std::string & filename) { is.open(filename.c_str(), std::ios::in); if ( is.fail() ) { - clitkExceptionMacro("Could not open file (for reading): " << filename); + clitkExceptionMacro("Could not open file for reading: " + << filename << ". Error is : <" + << strerror(errno) << ">"); } } //-------------------------------------------------------------------- @@ -251,7 +257,9 @@ void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) { os.open(filename.c_str(), std::ios::out); if ( os.fail() ) { - clitkExceptionMacro("Could not open file (for writing): " << filename); + clitkExceptionMacro("Could not open file for writing: " + << filename << ". Error is : <" + << strerror(errno) << ">"); } } //-------------------------------------------------------------------- @@ -308,5 +316,20 @@ void clitk::readDoubleFromFile(const std::string & filename, std::vector //-------------------------------------------------------------------- +//-------------------------------------------------------------------- +void clitk::PrintMemoryUsed() +{ +#if defined(unix) || defined(__APPLE__) + rusage usage; + getrusage(RUSAGE_SELF, &usage); + DD(usage.ru_maxrss); /* maximum resident set size */ + // DD(usage.ru_ixrss); /* integral shared memory size */ + // DD(usage.ru_idrss); /* integral unshared data size */ + // DD(usage.ru_isrss); /* integral unshared stack size */ +#endif +} +//-------------------------------------------------------------------- + + #endif /* end #define CLITKCOMMON_CXX */ diff --git a/common/clitkCommon.h b/common/clitkCommon.h index e3145b1..44f9940 100644 --- a/common/clitkCommon.h +++ b/common/clitkCommon.h @@ -30,8 +30,18 @@ #include #include +// std include #include +// Include for "rusage" +#include +#if defined(unix) || defined(__APPLE__) +# include +# include +#elif defined(WIN32) +# include +#endif + //-------------------------------------------------------------------- namespace clitk { @@ -71,6 +81,9 @@ namespace clitk { template std::string toStringVector(const T * t, const int n); template std::string toStringVector(const T & t, const int n); template std::string toStringVector(const std::vector & t); + template bool fromString(T& t, + const std::string& s, + std::ios_base& (*f)(std::ios_base&)=std::dec); //-------------------------------------------------------------------- // Display a progress % @@ -193,6 +206,9 @@ namespace clitk { template void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output); + //-------------------------------------------------------------------- + void PrintMemoryUsed(); + #include "clitkCommon.txx" } // end namespace diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index b5a3471..553c9b5 100644 --- a/common/clitkCommon.txx +++ b/common/clitkCommon.txx @@ -76,6 +76,19 @@ std::string toString(const T & t) } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// http://www.codeguru.com/forum/showthread.php?t=231054 +template +bool fromString(T& t, const std::string& s, + std::ios_base& (*f)(std::ios_base&)) +{ + std::istringstream iss(s); + return !(iss >> f >> t).fail(); +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- // Convert float*, double* ... to string template -- 2.47.1