Note: This tutorial assumes that you have completed the previous tutorials: Using Ecl Formatters.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Writing a Custom Formatter

Description: Writing your own custom formatters.

Keywords: ecl formatters

Tutorial Level: INTERMEDIATE

Formatters must implement

  • Formatting setters
  • Input value operator
  • Temporary formatting operators
  • Streaming friend function

   1 class MatrixFormatter {
   2 public:
   3     MatrixFormatter& precision(int p);     // Setter
   4     MatrixFormatter& operator()(Matrix M); // Input Operator
   5     MatrixFormatter& operator(int p, int w ... ); // Combination setter
   6     MatrixFormatter& operator(Matrix M, int p, int w ...); //Temporary formatting operator
   7     template <typename OutputStream, typename N> friend OutputStream& operator << (OutputStream& ostream, MatrixFormatter& formatter);
   8 }

Note that the setters and operators must return the formatter so that the friend function can implement the formatting.

For the Format<Type> class to automagically find this formatter, you can implement one of two methods. If you have access to the class's internals, simply typedef it as "Formatter" inside the class, otherwise simply specialise the Format<> class for your type and inherit the formatter.

   1 // Method one, if you have access to the classes internals
   2 class Matrix {
   3 public:
   4     typedef MatrixFormatter Formatter;
   5 };
   6 
   7 // Method two
   8 template <>
   9 class Format<Matrix> : public MatrixFormatter {};

Example

See include/ecl/containers/array$ vim formatters.hpp in ecl_containers for a concrete example.

Wiki: ecl_formatters/Tutorials/Writing a Custom Formatter (last edited 2012-01-22 10:01:34 by DanielStonier)