diff --git a/wrap/callback.h b/wrap/callback.h index c496591c..3e60c76b 100644 --- a/wrap/callback.h +++ b/wrap/callback.h @@ -19,40 +19,62 @@ * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * -****************************************************************************/ -/**************************************************************************** - History - -$Log: not supported by cvs2svn $ -Revision 1.3 2004/05/12 10:18:55 ganovelli -new line added at the end of file - -Revision 1.2 2004/05/10 14:26:33 ganovelli -argument name removed to avoid "unreference variable"warning - -Revision 1.1 2004/03/03 15:00:51 cignoni -Initial commit - ****************************************************************************/ #ifndef __VCGLIB_CALLBACK #define __VCGLIB_CALLBACK +#include // For va_start, etc. +#include // For std::unique_ptr + namespace vcg { -// Generic Callback function: -// Used to make algorithms interumpable -// Return value: true continue, false break -// The second callback is to know where we are (useful for progress bar) -typedef bool CallBack( const char * str ); +/*! \brief This function allow lenghty algorithms to report progress and status. + * + * This function is usually called from inside a long processing algorithm + * with a reasonable frequency (e.g. not more that 10 times per sec) + * reporting the what the algorithm is doing and at what point of the processing (as a int percentage) + * we currently are. + * + * Users of the library usually rewrote this function to handle this reporting in the correct way + * (e.g. in a log window and in a progress bar). + * + * The algorithms in the library should also check the return value + * to see if the user asked for an interruption, but they can ignore this request + * (hint: most of algs in the lib ignore any request). + */ + typedef bool CallBackPos(const int pos, const char * str ); -inline bool DummyCallBack( const char * ) {return true;} inline bool DummyCallBackPos(const int , const char * ) {return true;} +inline bool COutCallBackPos(const int , const char *str ) { + std::cout << str; return true; +} +inline bool CErrCallBackPos(const int , const char *str ) { + std::cerr << str; return true; +} +/*! \brief Helper function that formats a string a la printf and returns a simple char* + * + * Quite useful for formatting the above callback... + */ -/// interruptible function -typedef void TaskFunc(int t,void * contest); - -} // End namespace +char* StrFormat(const std::string fmt_str, ...) { + int final_n, n = ((int)fmt_str.size()) * 2; /* Reserve two times as much as the length of the fmt_str */ + std::unique_ptr formatted; + va_list ap; + while(1) { + formatted.reset(new char[n]); /* Wrap the plain char array into the unique_ptr */ + strcpy(&formatted[0], fmt_str.c_str()); + va_start(ap, fmt_str); + final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap); + va_end(ap); + if (final_n < 0 || final_n >= n) + n += abs(final_n - n + 1); + else + break; + } + return formatted.get(); +} +}// End namespace #endif