Tapkee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
formatting.hpp
Go to the documentation of this file.
1 
30 #ifndef FORMATTING_H_
31 #define FORMATTING_H_
32 
33 #include <string>
34 #include <stdexcept>
35 #include <sstream>
36 
37 #include <formatting/wrappers.hpp>
39 
40 namespace formatting
41 {
42  static const int WORLD_VERSION = 0;
43  static const int MAJOR_VERSION = 1;
44  static const int MINOR_VERSION = 2;
45 
47  const std::string placeholder = "{}";
48 
52  class ValueWrapper;
53 
57  class formatting_error : public std::logic_error
58  {
59  public:
60  explicit formatting_error(const std::string& reason) :
61  std::logic_error(reason)
62  {
63  }
64  };
65 
84  std::string format(const std::string& fmt,
85  const ValueWrapper a);
86 
107  std::string format(const std::string& fmt,
108  const ValueWrapper a, const ValueWrapper b);
109 
132  std::string format(const std::string& fmt,
133  const ValueWrapper a, const ValueWrapper b,
134  const ValueWrapper c);
135 
160  std::string format(const std::string& fmt,
161  const ValueWrapper a, const ValueWrapper b,
162  const ValueWrapper c, const ValueWrapper d);
163 
190  std::string format(const std::string& fmt,
191  const ValueWrapper a, const ValueWrapper b,
192  const ValueWrapper c, const ValueWrapper d,
193  const ValueWrapper e);
194 
223  std::string format(const std::string& fmt,
224  const ValueWrapper a, const ValueWrapper b,
225  const ValueWrapper c, const ValueWrapper d,
226  const ValueWrapper e, const ValueWrapper f);
227 
258  std::string format(const std::string& fmt,
259  const ValueWrapper a, const ValueWrapper b,
260  const ValueWrapper c, const ValueWrapper d,
261  const ValueWrapper e, const ValueWrapper f,
262  const ValueWrapper g);
263 
296  std::string format(const std::string& fmt,
297  const ValueWrapper a, const ValueWrapper b,
298  const ValueWrapper c, const ValueWrapper d,
299  const ValueWrapper e, const ValueWrapper f,
300  const ValueWrapper g, const ValueWrapper h);
301 
336  std::string format(const std::string& fmt,
337  const ValueWrapper a, const ValueWrapper b,
338  const ValueWrapper c, const ValueWrapper d,
339  const ValueWrapper e, const ValueWrapper f,
340  const ValueWrapper g, const ValueWrapper h,
341  const ValueWrapper i);
342 
379  std::string format(const std::string& fmt,
380  const ValueWrapper a, const ValueWrapper b,
381  const ValueWrapper c, const ValueWrapper d,
382  const ValueWrapper e, const ValueWrapper f,
383  const ValueWrapper g, const ValueWrapper h,
384  const ValueWrapper i, const ValueWrapper j);
385 
387  namespace internal
388  {
392 
394  std::string formatImplementation(const std::string& formatting,
395  const ValueWrapper** handlers,
396  std::size_t n_handlers);
397  }
398 
400  {
401  public:
402  template<typename T> ValueWrapper(T value);
403  ValueWrapper();
404  ValueWrapper(const ValueWrapper& wrapper);
405  ~ValueWrapper();
406  std::string representation() const;
407  private:
409  };
410 
411  std::string format(const std::string& fmt,
412  const ValueWrapper a)
413  {
414  const ValueWrapper* handlers[] = {&a};
415  return formatting::internal::formatImplementation(fmt, handlers, 1);
416  }
417 
418  std::string format(const std::string& fmt,
419  const ValueWrapper a, const ValueWrapper b)
420  {
421  const ValueWrapper* handlers[] = {&a, &b};
422  return formatting::internal::formatImplementation(fmt, handlers, 2);
423  }
424 
425  std::string format(const std::string& fmt,
426  const ValueWrapper a, const ValueWrapper b,
427  const ValueWrapper c)
428  {
429  const ValueWrapper* handlers[] = {&a, &b, &c};
430  return formatting::internal::formatImplementation(fmt, handlers, 3);
431  }
432 
433  std::string format(const std::string& fmt,
434  const ValueWrapper a, const ValueWrapper b,
435  const ValueWrapper c, const ValueWrapper d)
436  {
437  const ValueWrapper* handlers[] = {&a, &b, &c, &d};
438  return formatting::internal::formatImplementation(fmt, handlers, 4);
439  }
440 
441  std::string format(const std::string& fmt,
442  const ValueWrapper a, const ValueWrapper b,
443  const ValueWrapper c, const ValueWrapper d,
444  const ValueWrapper e)
445  {
446  const ValueWrapper* handlers[] = {&a, &b, &c, &d, &e};
447  return formatting::internal::formatImplementation(fmt, handlers, 5);
448  }
449 
450  std::string format(const std::string& fmt,
451  const ValueWrapper a, const ValueWrapper b,
452  const ValueWrapper c, const ValueWrapper d,
453  const ValueWrapper e, const ValueWrapper f)
454  {
455  const ValueWrapper* handlers[] = {&a, &b, &c, &d, &e, &f};
456  return formatting::internal::formatImplementation(fmt, handlers, 6);
457  }
458 
459  std::string format(const std::string& fmt,
460  const ValueWrapper a, const ValueWrapper b,
461  const ValueWrapper c, const ValueWrapper d,
462  const ValueWrapper e, const ValueWrapper f,
463  const ValueWrapper g)
464  {
465  const ValueWrapper* handlers[] = {&a, &b, &c, &d, &e, &f, &g};
466  return formatting::internal::formatImplementation(fmt, handlers, 7);
467  }
468 
469  std::string format(const std::string& fmt,
470  const ValueWrapper a, const ValueWrapper b,
471  const ValueWrapper c, const ValueWrapper d,
472  const ValueWrapper e, const ValueWrapper f,
473  const ValueWrapper g, const ValueWrapper h)
474  {
475  const ValueWrapper* handlers[] = {&a, &b, &c, &d, &e, &f, &g, &h};
476  return formatting::internal::formatImplementation(fmt, handlers, 8);
477  }
478 
479  std::string format(const std::string& fmt,
480  const ValueWrapper a, const ValueWrapper b,
481  const ValueWrapper c, const ValueWrapper d,
482  const ValueWrapper e, const ValueWrapper f,
483  const ValueWrapper g, const ValueWrapper h,
484  const ValueWrapper i)
485  {
486  const ValueWrapper* handlers[] = {&a, &b, &c, &d, &e, &f, &g, &h, &i};
487  return formatting::internal::formatImplementation(fmt, handlers, 9);
488  }
489 
490  std::string format(const std::string& fmt,
491  const ValueWrapper a, const ValueWrapper b,
492  const ValueWrapper c, const ValueWrapper d,
493  const ValueWrapper e, const ValueWrapper f,
494  const ValueWrapper g, const ValueWrapper h,
495  const ValueWrapper i, const ValueWrapper j)
496  {
497  const ValueWrapper* handlers[] = {&a, &b, &c, &d, &e, &f, &g, &h, &i, &j};
498  return formatting::internal::formatImplementation(fmt, handlers, 10);
499  }
500 
501  namespace internal
502  {
503  std::string formatImplementation(const std::string& formatter_string,
504  const ValueWrapper** handlers,
505  std::size_t n_handlers)
506  {
507  std::string formatted = formatter_string;
508  std::size_t placeholder_position = 0;
509  for (std::size_t i=0; i<n_handlers; i++)
510  {
511  placeholder_position = formatted.find(placeholder, placeholder_position);
512  if (placeholder_position != std::string::npos)
513  {
514  const std::string representation = handlers[i]->representation();
515  formatted.replace(placeholder_position,placeholder.length(),
516  representation,0,std::string::npos);
517  placeholder_position += representation.length();
518  }
519  else
520  throw formatting_error("The number of placeholders doesn't match the number of provided arguments");
521  }
522  return formatted;
523  }
524  }
525 
526  template<typename T>
528  implementation_(new formatting::internal::ValueWrapperImplementation<T>(v))
529  {
530  }
531 
533  implementation_(new formatting::internal::ValueWrapperImplementation<const char*>("invalid argument"))
534  {
535  }
536 
538  implementation_(wrapper.implementation_)
539  {
540  }
541 
543  {
544  delete implementation_;
545  }
546 
547  std::string ValueWrapper::representation() const
548  {
550  }
551 }
552 #endif
formatting::internal::ValueWrapperImplementationBase * implementation_
Definition: formatting.hpp:408
static const int WORLD_VERSION
Definition: formatting.hpp:42
static const int MAJOR_VERSION
Definition: formatting.hpp:43
formatting_error(const std::string &reason)
Definition: formatting.hpp:60
const std::string placeholder
Definition: formatting.hpp:47
static const int MINOR_VERSION
Definition: formatting.hpp:44
std::string format(const std::string &fmt, const ValueWrapper a)
Definition: formatting.hpp:411
std::string formatImplementation(const std::string &formatting, const ValueWrapper **handlers, std::size_t n_handlers)
Definition: formatting.hpp:503
std::string representation() const
Definition: formatting.hpp:547