/** @file
$ Id $
$ Date $
$ Author $
@ copy & copy ; Marc W & auml ; ckerlin
@ license LGPL , see file < a href = " license.html " > COPYING < / a >
$ Log $
Revision 1.6 2005 / 02 / 18 15 : 47 : 23 marc
missing # ifndef
new functions ifelse
Revision 1.5 2005 / 01 / 28 07 : 52 : 33 marc
typo in doc
Revision 1.4 2004 / 12 / 20 07 : 40 : 36 marc
documentation improved , new grouping
Revision 1.3 2004 / 12 / 17 16 : 27 : 28 marc
error in documentation : group forgotten
Revision 1.2 2004 / 10 / 13 11 : 18 : 33 marc
getline reads a whole line from a stream
Revision 1.1 2004 / 10 / 11 18 : 30 : 16 marc
* * * empty log message * * *
*/
# ifndef __MRW__STDEXT_HPP__
# define __MRW__STDEXT_HPP__
# include <string>
# include <iostream>
# include <algorithm>
namespace mrw {
/** @addtogroup StdExt
*/
//@{
/** @defgroup stdextFunction Useful Global Functions
- @ ref getline read exactly one line from a stream
- @ ref ifelse implements a dual < code > ? : < / code > operator , like :
< code > a & nbsp ; ? & nbsp ; a & nbsp ; : & nbsp ; b < / code >
@ section stdextReadline Read Line
The global functions mrw : : getline read exactly one line from a
stream , without the need of a buffer . It is therefore guaranteed
that a whole line is read , regardless of the length of the
line . The result is returned as std : : string .
@ code
// first syntax returns a string:
std : : string line ( mrw : : readline ( std : : cin ) ) ;
// second syntax returns the stream:
while ( mrw : : readline ( std : : cin , line ) )
std : : cout < < " Read: " < < line < < std : : endl ;
@ endcode
*/
//@{
/** @brief Deprecated! Do not use any more!
@ deprecated Use @ c std : : min from @ c
# include <algorithm> instead!
Get the lower of two values .
If both values are equal , @ c a is returned .
@ param a first value to compare
@ param b second value to compare
@ return the lower of both values
@ pre # include < mrw / stdext . hpp >
@ pre @ c T must support < code > bool operator > ( const T & , constT & ) < / code >
*/
template < typename T > const T & min ( const T & a , const T & b ) {
/// calls @c std::min(a, b);
/// @deprecated will be removed in next major release
return std : : min ( a , b ) ;
}
/** @brief Deprecated! Do not use any more!
@ deprecated Use @ c std : : max from @ c
# include <algorithm> instead!
Get the higher of two values .
If both values are equal , @ c a is returned .
@ param a first value to compare
@ param b second value to compare
@ return the higher of both values
@ pre # include < mrw / stdext . hpp >
@ pre @ c T must support < code > bool operator < ( const T & , constT & ) < / code >
*/
template < typename T > const T & max ( const T & a , const T & b ) {
/// calls @c std::max(a, b);
/// @deprecated will be removed in next major release
return std : : max ( a , b ) ;
}
/** @brief Dual <code>?:</code> operation:
< code > a & nbsp ; ? & nbsp ; a & nbsp ; : & nbsp ; b < / code >
If the first parameter is not @ c 0 , then it is returned ,
otherwise the second parameter is returned . It is equivalent to
the construct : < code > a & nbsp ; ? & nbsp ; a & nbsp ; : & nbsp ; b < / code > , but without
the side effect of duplicated evaluation of parameter
< code > a < / code > .
Older gcc compiler implemented the non standard extension of
< code > a ? : b < / code > , which was exactly the same .
@ param a criteria that is checked and returned if not @ c 0
@ param b alternative value to be returned if @ c a is @ c 0
@ return < code > a & nbsp ; ? & nbsp ; a & nbsp ; : & nbsp ; b < / code >
@ pre # include < mrw / stdext . hpp >
@ pre @ c T must be convertible to @ c bool
*/
template < typename T > const T & ifelse ( const T & a , const T & b ) {
return a ? a : b ;
}
/** @brief Dual <code>?:</code> operation:
< code > a & nbsp ; ? & nbsp ; a & nbsp ; : & nbsp ; b < / code >
@ copydoc ifelse ( const T & a , const T & b ) */
template < typename T > const T * ifelse ( const T * a , const T * b ) {
return a ? a : b ;
}
/** @brief read one line from a stream
Reads one line from a stream , up to delimiter @ c d .
The delimiter is not appended to the string .
@ param is the stream to read from
@ param d the end of line delimiter
@ return the line read from the stream
@ pre # include < mrw / stdext . hpp >
*/
std : : string getline ( std : : istream & is , char d = ' \n ' ) ;
/** @brief read one line from a stream
Reads one line from a stream , up to delimiter @ c d .
The delimiter is not appended to the string .
@ param is the stream to read from
@ param s the string to place the line in
@ param d the end of line delimiter
@ return @ c s : the line read from the stream
@ return the stream after extraction of line
@ pre # include < mrw / stdext . hpp >
*/
std : : istream & getline ( std : : istream & is , std : : string & s , char d = ' \n ' ) ;
//@}
//@}
}
# endif