strings.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_FORMATTERS_STRINGS_HPP_
13 #define ECL_FORMATTERS_STRINGS_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <string>
21 #include "common.hpp"
22 #include "macros.hpp"
23 
24 /*****************************************************************************
25 ** Namespaces
26 *****************************************************************************/
27 
28 namespace ecl {
29 
30 /*****************************************************************************
31 ** Format Interface [String types]
32 *****************************************************************************/
33 
41 template <>
42 class ecl_formatters_PUBLIC Format<std::string> {
43  public:
44  /******************************************
45  ** C&D's
46  *******************************************/
52  Format(int w = -1, Alignment a = NoAlign) : width_(w),alignment_(a) {}
53  virtual ~Format() {}
54 
55  /******************************************
56  ** Set
57  *******************************************/
62  Format<std::string>& width(int w) { width_ = w; return *this; }
68  Format<std::string>& align(Alignment a) { alignment_ = a; return *this; }
69 
70  /******************************************
71  * Set Format Combinations
72  ******************************************/
79  Format<std::string>& operator ()(int w, Alignment a);
80 
81  /******************************************
82  ** Format a value
83  *******************************************/
89  Format<std::string>& operator() (const std::string &input_string);
90 
91  /******************************************
92  ** Common format usages
93  *******************************************/
100  Format<std::string>& operator() (const std::string &input_string, int w);
109  Format<std::string>& operator() (const std::string &input_string, int w, Alignment a);
110 
111  /******************************************
112  ** Insert the formatter into a stream
113  *******************************************/
126  template <typename OutputStream> friend OutputStream& operator << (OutputStream& ostream, Format<std::string>& formatter);
127 
128  private:
129  /******************************************
130  ** Parameters
131  *******************************************/
132  int width_;
135  std::string s;
136 
137  /******************************************
138  ** Padding
139  *******************************************/
145  template <typename OutputStream> void pad(int n, OutputStream &ostream) const;
151  template <typename OutputStream> void prePad(int n, OutputStream &ostream) const;
157  template <typename OutputStream> void postPad(int n, OutputStream &ostream) const;
158 
159  /******************************************
160  ** Formatter Functions
161  *******************************************/
166  template <typename OutputStream> void format(OutputStream &ostream) const;
167 };
168 
169 /*****************************************************************************
170 ** Implementation [Format<string>][Templates]
171 *****************************************************************************/
172 
173 template <typename OutputStream>
174 void Format<std::string>::format(OutputStream &ostream) const
175 {
176  int size = s.size();
177 
178  prePad(width_ - (size),ostream); // previously had (size+2) here...why?
179  ostream << s;
180  postPad(width_ - (size),ostream);
181 }
182 
183 template <typename OutputStream>
184 void Format<std::string>::prePad(int n, OutputStream &ostream) const
185 {
186  if ( n <= 0 ) { return; }
187  switch ( alignment_ )
188  {
189  case ( NoAlign ) : { break; }
190  case ( LeftAlign ) : { break; }
191  case ( RightAlign ) : { pad(n,ostream); break; }
192  case ( CentreAlign ) : { pad(n/2+ n%2,ostream); break; } // Add the remainder
193  default : break;
194  }
195 }
196 template <typename OutputStream>
197 void Format<std::string>::postPad(int n, OutputStream &ostream) const
198 {
199  if ( n <= 0 ) { return; }
200  switch ( alignment_ )
201  {
202  case ( NoAlign ) : { break; }
203  case ( LeftAlign ) : { pad(n,ostream); break; }
204  case ( RightAlign ) : { break; }
205  case ( CentreAlign ) : { pad(n/2,ostream); break; } // Do not add the remainder
206  default : break;
207  }
208 }
209 template <typename OutputStream>
210 void Format<std::string>::pad(int n, OutputStream &ostream) const
211 {
212  for (int i = n; i > 0; --i )
213  {
214  ostream << ' ';
215  }
216 }
217 
218 /*****************************************************************************
219 * Implementation [streaming]
220 *****************************************************************************/
221 
222 template <typename OutputStream>
223 OutputStream& operator << (OutputStream &ostream, Format<std::string>& formatter )
224 {
225  bool ready = formatter.ready_to_format;
226 
227  ecl_assert_throw(ready, StandardException(LOC,UsageError,"The formatter cannot print any data - "
228  "either there is no data available, or you have tried to use the "
229  "formatter more than once in a single streaming operation. "
230  "C++ produces unspecified results when functors are used multiply "
231  "in the same stream sequence, so this is not permitted here.") );
232 
233  if ( ready ) {
234  formatter.format(ostream);
235  formatter.ready_to_format = false;
236  }
237 
238  return ostream;
239 }
240 
241 }; // namespace ecl
242 
243 #endif /*ECL_FORMATTERS_STRINGS_HPP_*/
Embedded control libraries.
Format< std::string > & width(int w)
Definition: strings.hpp:62
Formatter for string types.
Definition: strings.hpp:42
Align to the left.
Definition: common.hpp:32
#define LOC
Common formatting definitions.
Primary template for all formatter classes.
Definition: common.hpp:99
#define ecl_assert_throw(expression, exception)
Format< std::string > & align(Alignment a)
Definition: strings.hpp:68
Macros for ecl formatters.
No alignment used.
Definition: common.hpp:31
UsageError
Format(int w=-1, Alignment a=NoAlign)
Definition: strings.hpp:52
Align to the right.
Definition: common.hpp:33
#define ecl_formatters_PUBLIC
Definition: macros.hpp:37
Alignment
Alignment tags for text formatting.
Definition: common.hpp:29
Align in the centre.
Definition: common.hpp:34


ecl_formatters
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:36