Note: This tutorial assumes that you have completed the previous tutorials: Arrays, Stencils.
(!) 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.

Container Utilities

Description: container formatters and converters.

Keywords: ecl container utilities

Tutorial Level: INTERMEDIATE

Formatters

Currently there is only limited support for formatters of the array and stencil classes classes:

  • precision/width formatting for float arrays.
  • hex formatting for byte arrays (signed char/char/unsigned char).
  • default formatting for other types of arrays (provided they have the required Format for the element type).
  • formatting extends to stencils operating on the array.

   1 #include <ecl/array.hpp>
   2 
   3 Array<unsigned char>::Formatter format;
   4 Array<unsigned char> array;
   5 array << 0x01, 0x02, 0x03, 0x04;
   6 std::cout << format(array) << std::endl;
   7 // OR
   8 #include <ecl/array.hpp>
   9 #include <ecl/format.hpp>
  10 
  11 Format< Array<unsigned char> > format;
  12 Array<unsigned char> array;
  13 array << 0x01, 0x02, 0x03, 0x04;
  14 std::cout << format(array) << std::endl;

See also ecl_formatters.

Byte Array Converters

These are often useful for interacting with io devices.

Some converters utilising the FromByteArray template class from ecl_converters have specialisations here for arrays and stencils respectively. These allow conversion to integers of any time from a corresponding, little endian filled array/stencil of chars.

   1 typedef Array<char, 4> ByteArray;
   2 ByteArray byte_array;
   3 
   4 byte_array << 0x01, 0x02, 0x03;
   5 ecl::Converter<int,ByteStencil> toInt;
   6 std::cout << "Conversion: " << toInt(byte_array) << std::endl; // results in 197121
   7 std::cout << "Conversion: " << toInt(byte_array.stencil(0,2)) << std::endl; // only first two bytes -> 513
   8 

and going the other way, we pass it the byte array for storage when converting (works on both arrays and stencils)...

   1 Array<char,20> byte_array = Array<char,20>::Constant(0x00);
   2 Stencil< Array<char, 20> > stencil = byte_array.stencil(2,4);
   3 ecl::Converter< Stencil< Array<char, 20> >, ecl::int32 > fromInt;
   4 fromInt(stencil, ecl::int32(197121));
   5 std::cout << byte_array << std::endl;    // 0x00, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, ...
   6 std::cout << stencil << std::endl;       // 0x01, 0x02, 0x03, 0x00
   7 

Wiki: ecl_containers/Tutorials/Utilities (last edited 2012-01-18 14:39:39 by DanielStonier)