.. _program_listing_file__tmp_ws_src_ecl_core_ecl_formatters_include_ecl_formatters_strings.hpp: Program Listing for File strings.hpp ==================================== |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/ecl_core/ecl_formatters/include/ecl/formatters/strings.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /***************************************************************************** ** Ifdefs *****************************************************************************/ #ifndef ECL_FORMATTERS_STRINGS_HPP_ #define ECL_FORMATTERS_STRINGS_HPP_ /***************************************************************************** ** Includes *****************************************************************************/ #include #include #include "common.hpp" #include "macros.hpp" /***************************************************************************** ** Namespaces *****************************************************************************/ namespace ecl { /***************************************************************************** ** Format Interface [String types] *****************************************************************************/ template <> class ecl_formatters_PUBLIC Format { public: /****************************************** ** C&D's *******************************************/ Format(int w = -1, Alignment a = NoAlign) : width_(w),alignment_(a) {} virtual ~Format() {} /****************************************** ** Set *******************************************/ Format& width(int w) { width_ = w; return *this; } Format& align(Alignment a) { alignment_ = a; return *this; } /****************************************** * Set Format Combinations ******************************************/ Format& operator ()(int w, Alignment a); /****************************************** ** Format a value *******************************************/ Format& operator() (const std::string &input_string); /****************************************** ** Common format usages *******************************************/ Format& operator() (const std::string &input_string, int w); Format& operator() (const std::string &input_string, int w, Alignment a); /****************************************** ** Insert the formatter into a stream *******************************************/ template friend OutputStream& operator << (OutputStream& ostream, Format& formatter); private: /****************************************** ** Parameters *******************************************/ int width_; Alignment alignment_; bool ready_to_format; std::string s; /****************************************** ** Padding *******************************************/ template void pad(int n, OutputStream &ostream) const; template void prePad(int n, OutputStream &ostream) const; template void postPad(int n, OutputStream &ostream) const; /****************************************** ** Formatter Functions *******************************************/ template void format(OutputStream &ostream) const; }; /***************************************************************************** ** Implementation [Format][Templates] *****************************************************************************/ template void Format::format(OutputStream &ostream) const { int size = s.size(); prePad(width_ - (size),ostream); // previously had (size+2) here...why? ostream << s; postPad(width_ - (size),ostream); } template void Format::prePad(int n, OutputStream &ostream) const { if ( n <= 0 ) { return; } switch ( alignment_ ) { case ( NoAlign ) : { break; } case ( LeftAlign ) : { break; } case ( RightAlign ) : { pad(n,ostream); break; } case ( CentreAlign ) : { pad(n/2+ n%2,ostream); break; } // Add the remainder default : break; } } template void Format::postPad(int n, OutputStream &ostream) const { if ( n <= 0 ) { return; } switch ( alignment_ ) { case ( NoAlign ) : { break; } case ( LeftAlign ) : { pad(n,ostream); break; } case ( RightAlign ) : { break; } case ( CentreAlign ) : { pad(n/2,ostream); break; } // Do not add the remainder default : break; } } template void Format::pad(int n, OutputStream &ostream) const { for (int i = n; i > 0; --i ) { ostream << ' '; } } /***************************************************************************** * Implementation [streaming] *****************************************************************************/ template OutputStream& operator << (OutputStream &ostream, Format& formatter ) { bool ready = formatter.ready_to_format; ecl_assert_throw(ready, StandardException(LOC,UsageError,"The formatter cannot print any data - " "either there is no data available, or you have tried to use the " "formatter more than once in a single streaming operation. " "C++ produces unspecified results when functors are used multiply " "in the same stream sequence, so this is not permitted here.") ); if ( ready ) { formatter.format(ostream); formatter.ready_to_format = false; } return ostream; } } // namespace ecl #endif /*ECL_FORMATTERS_STRINGS_HPP_*/