1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/progress.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,143 @@
1.4 +// boost progress.hpp header file ------------------------------------------//
1.5 +
1.6 +// Copyright Beman Dawes 1994-99. Distributed under the Boost
1.7 +// Software License, Version 1.0. (See accompanying file
1.8 +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
1.9 +
1.10 +// See http://www.boost.org/libs/timer for documentation.
1.11 +
1.12 +// Revision History
1.13 +// 1 Dec 01 Add leading progress display strings (suggested by Toon Knapen)
1.14 +// 20 May 01 Introduce several static_casts<> to eliminate warning messages
1.15 +// (Fixed by Beman, reported by Herve Bronnimann)
1.16 +// 12 Jan 01 Change to inline implementation to allow use without library
1.17 +// builds. See docs for more rationale. (Beman Dawes)
1.18 +// 22 Jul 99 Name changed to .hpp
1.19 +// 16 Jul 99 Second beta
1.20 +// 6 Jul 99 Initial boost version
1.21 +
1.22 +#ifndef BOOST_PROGRESS_HPP
1.23 +#define BOOST_PROGRESS_HPP
1.24 +
1.25 +#include <boost/timer.hpp>
1.26 +#include <boost/utility.hpp> // for noncopyable
1.27 +#include <boost/cstdint.hpp> // for uintmax_t
1.28 +#include <iostream> // for ostream, cout, etc
1.29 +#include <string> // for string
1.30 +
1.31 +namespace boost {
1.32 +
1.33 +// progress_timer ----------------------------------------------------------//
1.34 +
1.35 +// A progress_timer behaves like a timer except that the destructor displays
1.36 +// an elapsed time message at an appropriate place in an appropriate form.
1.37 +
1.38 +class progress_timer : public timer, private noncopyable
1.39 +{
1.40 +
1.41 + public:
1.42 + explicit progress_timer( std::ostream & os = std::cout )
1.43 + // os is hint; implementation may ignore, particularly in embedded systems
1.44 + : m_os(os) {}
1.45 + ~progress_timer()
1.46 + {
1.47 + // A) Throwing an exception from a destructor is a Bad Thing.
1.48 + // B) The progress_timer destructor does output which may throw.
1.49 + // C) A progress_timer is usually not critical to the application.
1.50 + // Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
1.51 + try
1.52 + {
1.53 + // use istream instead of ios_base to workaround GNU problem (Greg Chicares)
1.54 + std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
1.55 + std::istream::floatfield );
1.56 + std::streamsize old_prec = m_os.precision( 2 );
1.57 + m_os << elapsed() << " s\n" // "s" is System International d'Unités std
1.58 + << std::endl;
1.59 + m_os.flags( old_flags );
1.60 + m_os.precision( old_prec );
1.61 + }
1.62 +
1.63 + catch (...) {} // eat any exceptions
1.64 + } // ~progress_timer
1.65 +
1.66 + private:
1.67 + std::ostream & m_os;
1.68 +};
1.69 +
1.70 +
1.71 +// progress_display --------------------------------------------------------//
1.72 +
1.73 +// progress_display displays an appropriate indication of
1.74 +// progress at an appropriate place in an appropriate form.
1.75 +
1.76 +// NOTE: (Jan 12, 2001) Tried to change unsigned long to boost::uintmax_t, but
1.77 +// found some compilers couldn't handle the required conversion to double.
1.78 +// Reverted to unsigned long until the compilers catch up.
1.79 +
1.80 +class progress_display : private noncopyable
1.81 +{
1.82 + public:
1.83 + explicit progress_display( unsigned long expected_count,
1.84 + std::ostream & os = std::cout,
1.85 + const std::string & s1 = "\n", //leading strings
1.86 + const std::string & s2 = "",
1.87 + const std::string & s3 = "" )
1.88 + // os is hint; implementation may ignore, particularly in embedded systems
1.89 + : m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count); }
1.90 +
1.91 + void restart( unsigned long expected_count )
1.92 + // Effects: display appropriate scale
1.93 + // Postconditions: count()==0, expected_count()==expected_count
1.94 + {
1.95 + _count = _next_tic_count = _tic = 0;
1.96 + _expected_count = expected_count;
1.97 +
1.98 + m_os << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100%\n"
1.99 + << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
1.100 + << std::endl // endl implies flush, which ensures display
1.101 + << m_s3;
1.102 + if ( !_expected_count ) _expected_count = 1; // prevent divide by zero
1.103 + } // restart
1.104 +
1.105 + unsigned long operator+=( unsigned long increment )
1.106 + // Effects: Display appropriate progress tic if needed.
1.107 + // Postconditions: count()== original count() + increment
1.108 + // Returns: count().
1.109 + {
1.110 + if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
1.111 + return _count;
1.112 + }
1.113 +
1.114 + unsigned long operator++() { return operator+=( 1 ); }
1.115 + unsigned long count() const { return _count; }
1.116 + unsigned long expected_count() const { return _expected_count; }
1.117 +
1.118 + private:
1.119 + std::ostream & m_os; // may not be present in all imps
1.120 + const std::string m_s1; // string is more general, safer than
1.121 + const std::string m_s2; // const char *, and efficiency or size are
1.122 + const std::string m_s3; // not issues
1.123 +
1.124 + unsigned long _count, _expected_count, _next_tic_count;
1.125 + unsigned int _tic;
1.126 + void display_tic()
1.127 + {
1.128 + // use of floating point ensures that both large and small counts
1.129 + // work correctly. static_cast<>() is also used several places
1.130 + // to suppress spurious compiler warnings.
1.131 + unsigned int tics_needed =
1.132 + static_cast<unsigned int>(
1.133 + (static_cast<double>(_count)/_expected_count)*50.0 );
1.134 + do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
1.135 + _next_tic_count =
1.136 + static_cast<unsigned long>((_tic/50.0)*_expected_count);
1.137 + if ( _count == _expected_count ) {
1.138 + if ( _tic < 51 ) m_os << '*';
1.139 + m_os << std::endl;
1.140 + }
1.141 + } // display_tic
1.142 +};
1.143 +
1.144 +} // namespace boost
1.145 +
1.146 +#endif // BOOST_PROGRESS_HPP