Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:43:50

0001 #ifndef FORMATTER_H
0002 #define FORMATTER_H
0003 
0004 /**
0005  * @file Formatter.h
0006  * @author Bryan BERTHOU (SPhN / CEA Saclay)
0007  * @author <contibutor> http://stackoverflow.com/questions/12261915/howto-throw-stdexceptions-with-variable-messages/12262626#12262626
0008  * @date 16 September 2014
0009  * @version 1.0
0010  */
0011 
0012 #include <iomanip>
0013 #include <limits>
0014 #include <sstream>
0015 #include <string>
0016 #include <clocale>
0017 #include <iostream>
0018 
0019 #include "StringUtils.h"
0020 
0021 namespace ElemUtils {
0022 
0023 /** @class Formatter
0024  *
0025  * @brief
0026  */
0027 class Formatter {
0028 public:
0029     Formatter() {
0030 
0031         // use to print full precision double
0032         m_stream << std::setprecision(std::numeric_limits<double>::digits10);
0033 
0034         //locale
0035         static std::locale loc("en_US.UTF-8");
0036         m_stream.imbue(loc);
0037     }
0038 
0039     ~Formatter() {
0040     }
0041 
0042     template<typename Type>
0043     Formatter & operator <<(const Type & value) {
0044         m_stream << value;
0045         return *this;
0046     }
0047 
0048     std::string str() const {
0049         return m_stream.str();
0050     }
0051 
0052     /**
0053      * Automatic cast to string.
0054      */
0055     operator std::string() const {
0056         return m_stream.str();
0057     }
0058 
0059     /**
0060      * Automatic cast to stringstream.
0061      */
0062     operator std::stringstream&() {
0063         return m_stream;
0064     }
0065 
0066     enum ConvertToString {
0067         to_str
0068     };
0069 
0070     std::string operator >>(ConvertToString) {
0071         return m_stream.str();
0072     }
0073 
0074     void print();
0075 
0076     void clear() {
0077         m_stream.clear();
0078         m_stream.str(StringUtils::EMPTY);
0079     }
0080 
0081 private:
0082     std::stringstream m_stream;
0083 
0084     Formatter(const Formatter &);
0085     Formatter & operator =(Formatter &);
0086 };
0087 
0088 } // namespace ElemUtils
0089 
0090 #endif /* FORMATTER_H */