array/formatters.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9  ** Ifdefs
10  *****************************************************************************/
11 
12 #ifndef ECL_CONTAINERS_ARRAY_FORMATTERS_HPP_
13 #define ECL_CONTAINERS_ARRAY_FORMATTERS_HPP_
14 
15 /*****************************************************************************
16  ** Includes
17  *****************************************************************************/
18 
19 #include <cmath>
20 #include <ecl/config/macros.hpp>
24 #include "../common/formatters.hpp"
25 #include "../array.hpp"
26 
27 /*****************************************************************************
28  ** Namespaces
29  *****************************************************************************/
30 
31 namespace ecl
32 {
33 namespace formatters
34 {
35 
36 /*****************************************************************************
37  ** Using
38  *****************************************************************************/
39 
63 template<typename Type, size_t N>
64 class ECL_PUBLIC ArrayFormatter
65 {
66 public:
67  virtual ~ArrayFormatter()
68  {}
75  ecl::Array<Type,N>& operator()(ecl::Array<Type,N> &array) {
76  return array;
77  }
78 };
79 
80 /*****************************************************************************
81  ** Interface [ByteArrayFormatter]
82  *****************************************************************************/
83 
106 template<typename Byte, size_t N>
107 class ECL_PUBLIC ByteArrayFormatter
108 {
109 public:
117  ByteArrayFormatter() : ready_to_format(false)
118  {};
126  ByteArrayFormatter<Byte,N>& operator()(const Array<Byte,N> &array)
127  {
128  begin_iterator = array.begin();
129  end_iterator = array.end();
130  ready_to_format = true;
131  return *this;
132  }
141  {
142  begin_iterator = stencil.begin();
143  end_iterator = stencil.end();
144  ready_to_format = true;
145  return *this;
146  }
158  ByteArrayFormatter<Byte,N>& operator()(typename Array<Byte,N>::const_iterator begin_iter,
159  typename Array<Byte,N>::const_iterator end_iter
160  )
161  {
162 
163  begin_iterator = begin_iter;
164  end_iterator = end_iter;
165  ready_to_format = true;
166  return *this;
167  }
168 
169  virtual ~ByteArrayFormatter()
170  {}
181  template <typename OutputStream, typename CharType, size_t M>
182  friend OutputStream& operator << (OutputStream& ostream, const ByteArrayFormatter<CharType,M> &formatter);
183 
184 private:
185  typename Array<Byte,N>::const_iterator begin_iterator;
186  typename Array<Byte,N>::const_iterator end_iterator;
187  bool ready_to_format;
188 };
189 
190 /*****************************************************************************
191  ** Implementation [ByteArrayFormatter]
192  *****************************************************************************/
193 
194 template <typename OutputStream, typename CharType, size_t M>
195 OutputStream& operator <<(OutputStream& ostream, const ByteArrayFormatter<CharType, M> &formatter)
196 {
197  ecl_assert_throw(formatter.ready_to_format, StandardException(LOC,UsageError,"The formatter cannot print any data - "
198  "either there is no data available, or you have tried to use the "
199  "formatter more than once in a single streaming operation. "
200  "C++ produces unspecified results when functors are used multiply "
201  "in the same stream sequence, so this is not permitted here.") );
202 
205 ostream << "[ ";
206  for ( iter = formatter.begin_iterator; iter != formatter.end_iterator; ++iter )
207  {
208  ostream << format(*iter) << " ";
209  }
210  ostream << "]";
211  ostream.flush();
212  return ostream;
213 }
214 
215 /*****************************************************************************
216  ** Specialisations [ArrayFormatter][Char Types]
217  *****************************************************************************/
231 template<size_t N>
232 class ECL_PUBLIC ArrayFormatter< signed char,N > : public ByteArrayFormatter<signed char, N>
233 {};
234 
248 template<size_t N>
249 class ECL_PUBLIC ArrayFormatter< char,N > : public ByteArrayFormatter<char, N>
250 {};
251 
265 template<size_t N>
266 class ECL_PUBLIC ArrayFormatter< unsigned char,N > : public ByteArrayFormatter<unsigned char, N>
267 {
268 };
269 
279 template<size_t N>
280 class ECL_PUBLIC ArrayFormatter< float,N > : public FloatContainerFormatter< Array<float,N> >
281 {
282 public:
291  ArrayFormatter(const int p=2, const int w=-1) : FloatContainerFormatter< Array<float,N> >(p,w)
292  {};
293  virtual ~ArrayFormatter()
294  {}
295 };
296 
306 template<size_t N>
307 class ECL_PUBLIC ArrayFormatter< double,N > : public FloatContainerFormatter< Array<double,N> >
308 {
309 public:
318  ArrayFormatter(const int p=2, const int w=-1) : FloatContainerFormatter< Array<double,N> >(p,w)
319  {};
320  virtual ~ArrayFormatter()
321  {}
322 };
323 
324 } // namespace formatters
325 } // namespace ecl
326 
327 #endif /* ECL_CONTAINERS_ARRAY_FORMATTERS_HPP_ */
ecl::formatters::operator<<
OutputStream & operator<<(OutputStream &ostream, const ByteArrayFormatter< CharType, M > &formatter)
Definition: array/formatters.hpp:207
ecl::Hex
Hex
ecl::NoAlign
NoAlign
ecl::UsageError
UsageError
ecl::formatters::ByteArrayFormatter::ready_to_format
bool ready_to_format
Definition: array/formatters.hpp:201
ecl::Stencil
A safe windowing class that opens onto array-like containers.
Definition: stencil/formatters.hpp:46
floats.hpp
common.hpp
LOC
#define LOC
number.hpp
ecl::StandardException
ecl::formatters::ByteArrayFormatter
Parent template for the byte array formatters.
Definition: array/formatters.hpp:119
ecl::Format
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
ecl::formatters::ArrayFormatter
Pseudo formatter for integral type arrays.
Definition: array_dynamic_mem_check.hpp:55
ecl::formatters::ByteArrayFormatter::operator()
ByteArrayFormatter< Byte, N > & operator()(const Array< Byte, N > &array)
Definition: array/formatters.hpp:140
ecl::Array
Fixed size container with a few bells and whistles.
Definition: array_no_mem_check.hpp:112
macros.hpp
ecl
Embedded control libraries.
ECL_PUBLIC
#define ECL_PUBLIC


ecl_containers
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:34