strings.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Ifdefs
00010 *****************************************************************************/
00011 
00012 #ifndef ECL_FORMATTERS_STRINGS_HPP_
00013 #define ECL_FORMATTERS_STRINGS_HPP_
00014 
00015 /*****************************************************************************
00016 ** Includes
00017 *****************************************************************************/
00018 
00019 #include <string>
00020 #include <ecl/exceptions/standard_exception.hpp>
00021 #include "common.hpp"
00022 #include "macros.hpp"
00023 
00024 /*****************************************************************************
00025 ** Namespaces
00026 *****************************************************************************/
00027 
00028 namespace ecl {
00029 
00030 /*****************************************************************************
00031 ** Format Interface [String types]
00032 *****************************************************************************/
00033 
00041 template <>
00042 class ecl_formatters_PUBLIC Format<std::string> {
00043     public:
00044         /******************************************
00045         ** C&D's
00046         *******************************************/
00052         Format(int w = -1, Alignment a = NoAlign) : width_(w),alignment_(a) {}
00053                 virtual ~Format() {}
00054 
00055         /******************************************
00056         ** Set
00057         *******************************************/
00062         Format<std::string>& width(int w) { width_ = w; return *this; }
00068         Format<std::string>& align(Alignment a) { alignment_ = a;  return *this; }
00069 
00070         /******************************************
00071          * Set Format Combinations
00072          ******************************************/
00079         Format<std::string>& operator ()(int w, Alignment a);
00080 
00081         /******************************************
00082         ** Format a value
00083         *******************************************/
00089         Format<std::string>& operator() (const std::string &input_string);
00090 
00091         /******************************************
00092         ** Common format usages
00093         *******************************************/
00100         Format<std::string>& operator() (const std::string &input_string, int w);
00109         Format<std::string>& operator() (const std::string &input_string, int w, Alignment a);
00110 
00111         /******************************************
00112         ** Insert the formatter into a stream
00113         *******************************************/
00126         template <typename OutputStream> friend OutputStream& operator << (OutputStream& ostream, Format<std::string>& formatter) ecl_assert_throw_decl(StandardException);
00127 
00128     private:
00129         /******************************************
00130         ** Parameters
00131         *******************************************/
00132         int width_;
00133         Alignment alignment_;
00134         bool ready_to_format;
00135         std::string s;
00136 
00137         /******************************************
00138         ** Padding
00139         *******************************************/
00145         template <typename OutputStream> void pad(int n, OutputStream &ostream) const;
00151         template <typename OutputStream> void prePad(int n, OutputStream &ostream) const;
00157         template <typename OutputStream> void postPad(int n, OutputStream &ostream) const;
00158 
00159         /******************************************
00160         ** Formatter Functions
00161         *******************************************/
00166         template <typename OutputStream> void format(OutputStream &ostream) const;
00167 };
00168 
00169 /*****************************************************************************
00170 ** Implementation [Format<string>][Templates]
00171 *****************************************************************************/
00172 
00173 template <typename OutputStream>
00174 void Format<std::string>::format(OutputStream &ostream) const
00175 {
00176     int size = s.size();
00177 
00178     prePad(width_ - (size),ostream); // previously had (size+2) here...why?
00179     ostream << s;
00180     postPad(width_ - (size),ostream);
00181 }
00182 
00183 template <typename OutputStream>
00184 void Format<std::string>::prePad(int n, OutputStream &ostream) const
00185 {
00186     if ( n <= 0 ) { return; }
00187     switch ( alignment_ )
00188     {
00189         case ( NoAlign ) : { break; }
00190         case ( LeftAlign ) : { break; }
00191         case ( RightAlign ) : { pad(n,ostream); break; }
00192         case ( CentreAlign ) : { pad(n/2+ n%2,ostream); break; } // Add the remainder
00193         default : break;
00194     }
00195 }
00196 template <typename OutputStream>
00197 void Format<std::string>::postPad(int n, OutputStream &ostream) const
00198 {
00199     if ( n <= 0 ) { return; }
00200     switch ( alignment_ )
00201     {
00202         case ( NoAlign ) : { break; }
00203         case ( LeftAlign ) : { pad(n,ostream); break; }
00204         case ( RightAlign ) : { break; }
00205         case ( CentreAlign ) : { pad(n/2,ostream); break; }  // Do not add the remainder
00206         default : break;
00207     }
00208 }
00209 template <typename OutputStream>
00210 void Format<std::string>::pad(int n, OutputStream &ostream) const
00211 {
00212     for (int i = n; i > 0; --i )
00213     {
00214         ostream << ' ';
00215     }
00216 }
00217 
00218 /*****************************************************************************
00219 * Implementation [streaming]
00220 *****************************************************************************/
00221 
00222 template <typename OutputStream>
00223 OutputStream& operator << (OutputStream &ostream, Format<std::string>& formatter ) ecl_assert_throw_decl(StandardException)
00224 {
00225     bool ready = formatter.ready_to_format;
00226 
00227     ecl_assert_throw(ready, StandardException(LOC,UsageError,"The formatter cannot print any data - "
00228             "either there is no data available, or you have tried to use the "
00229             "formatter more than once in a single streaming operation. "
00230             "C++ produces unspecified results when functors are used multiply "
00231             "in the same stream sequence, so this is not permitted here.") );
00232 
00233     if ( ready ) {
00234         formatter.format(ostream);
00235         formatter.ready_to_format = false;
00236     }
00237 
00238     return ostream;
00239 }
00240 
00241 }; // namespace ecl
00242 
00243 #endif /*ECL_FORMATTERS_STRINGS_HPP_*/


ecl_formatters
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:21:32