Commented callback, cleaned and added helper StrFormat
This commit is contained in:
parent
59ccf586c4
commit
ac5c368d2a
|
|
@ -19,40 +19,62 @@
|
||||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
||||||
* for more details. *
|
* 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
|
#ifndef __VCGLIB_CALLBACK
|
||||||
#define __VCGLIB_CALLBACK
|
#define __VCGLIB_CALLBACK
|
||||||
|
|
||||||
|
#include <stdarg.h> // For va_start, etc.
|
||||||
|
#include <memory> // For std::unique_ptr
|
||||||
|
|
||||||
namespace vcg {
|
namespace vcg {
|
||||||
// Generic Callback function:
|
/*! \brief This function allow lenghty algorithms to report progress and status.
|
||||||
// Used to make algorithms interumpable
|
*
|
||||||
// Return value: true continue, false break
|
* This function is usually called from inside a long processing algorithm
|
||||||
// The second callback is to know where we are (useful for progress bar)
|
* with a reasonable frequency (e.g. not more that 10 times per sec)
|
||||||
typedef bool CallBack( const char * str );
|
* 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 );
|
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 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
|
char* StrFormat(const std::string fmt_str, ...) {
|
||||||
typedef void TaskFunc(int t,void * contest);
|
int final_n, n = ((int)fmt_str.size()) * 2; /* Reserve two times as much as the length of the fmt_str */
|
||||||
|
std::unique_ptr<char[]> formatted;
|
||||||
} // End namespace
|
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
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue