formatters.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009  ** Ifdefs
00010  *****************************************************************************/
00011 
00012 #ifndef ECL_CONTAINERS_ARRAY_FORMATTERS_HPP_
00013 #define ECL_CONTAINERS_ARRAY_FORMATTERS_HPP_
00014 
00015 /*****************************************************************************
00016  ** Includes
00017  *****************************************************************************/
00018 
00019 #include <cmath>
00020 #include <ecl/config/macros.hpp>
00021 #include <ecl/formatters/common.hpp>
00022 #include <ecl/formatters/number.hpp>
00023 #include <ecl/formatters/floats.hpp>
00024 #include "../common/formatters.hpp"
00025 #include "../array.hpp"
00026 
00027 /*****************************************************************************
00028  ** Namespaces
00029  *****************************************************************************/
00030 
00031 namespace ecl
00032 {
00033 namespace formatters
00034 {
00035 
00036 /*****************************************************************************
00037  ** Using
00038  *****************************************************************************/
00039 
00063 template<typename Type, size_t N>
00064 class ECL_PUBLIC ArrayFormatter
00065 {
00066 public:
00067   virtual ~ArrayFormatter()
00068   {}
00075   ecl::Array<Type,N>& operator()(ecl::Array<Type,N> &array) {
00076     return array;
00077   }
00078 };
00079 
00080 /*****************************************************************************
00081  ** Interface [ByteArrayFormatter]
00082  *****************************************************************************/
00083 
00106 template<typename Byte, size_t N>
00107 class ECL_PUBLIC ByteArrayFormatter
00108 {
00109 public:
00117   ByteArrayFormatter() : ready_to_format(false)
00118   {};
00126   ByteArrayFormatter<Byte,N>& operator()(const Array<Byte,N> &array)
00127   {
00128     begin_iterator = array.begin();
00129     end_iterator = array.end();
00130     ready_to_format = true;
00131     return *this;
00132   }
00140   ByteArrayFormatter<Byte,N>& operator()(const Stencil< Array<Byte,N> > &stencil)
00141   {
00142     begin_iterator = stencil.begin();
00143     end_iterator = stencil.end();
00144     ready_to_format = true;
00145     return *this;
00146   }
00158   ByteArrayFormatter<Byte,N>& operator()(typename Array<Byte,N>::const_iterator begin_iter,
00159       typename Array<Byte,N>::const_iterator end_iter
00160   ) ecl_assert_throw_decl(StandardException)
00161   {
00162 
00163     begin_iterator = begin_iter;
00164     end_iterator = end_iter;
00165     ready_to_format = true;
00166     return *this;
00167   }
00168 
00169   virtual ~ByteArrayFormatter()
00170   {}
00181   template <typename OutputStream, typename CharType, size_t M>
00182   friend OutputStream& operator << (OutputStream& ostream, const ByteArrayFormatter<CharType,M> &formatter) ecl_assert_throw_decl(StandardException);
00183 
00184 private:
00185   typename Array<Byte,N>::const_iterator begin_iterator;
00186   typename Array<Byte,N>::const_iterator end_iterator;
00187   bool ready_to_format;
00188 };
00189 
00190 /*****************************************************************************
00191  ** Implementation [ByteArrayFormatter]
00192  *****************************************************************************/
00193 
00194 template <typename OutputStream, typename CharType, size_t M>
00195 OutputStream& operator <<(OutputStream& ostream, const ByteArrayFormatter<CharType, M> &formatter)
00196 ecl_assert_throw_decl(StandardException)
00197 {
00198   ecl_assert_throw(formatter.ready_to_format, StandardException(LOC,UsageError,"The formatter cannot print any data - "
00199           "either there is no data available, or you have tried to use the "
00200           "formatter more than once in a single streaming operation. "
00201           "C++ produces unspecified results when functors are used multiply "
00202           "in the same stream sequence, so this is not permitted here.") );
00203 
00204   ecl::Format<CharType> format(-1, ecl::NoAlign, ecl::Hex);
00205   typename ecl::Array<CharType, M>::const_iterator iter;
00206 ostream  << "[ ";
00207   for ( iter = formatter.begin_iterator; iter != formatter.end_iterator; ++iter )
00208   {
00209     ostream << format(*iter) << " ";
00210   }
00211   ostream << "]";
00212   ostream.flush();
00213   return ostream;
00214 }
00215 
00216 /*****************************************************************************
00217  ** Specialisations [ArrayFormatter][Char Types]
00218  *****************************************************************************/
00232 template<size_t N>
00233 class ECL_PUBLIC ArrayFormatter< signed char,N > : public ByteArrayFormatter<signed char, N>
00234 {};
00235 
00249 template<size_t N>
00250 class ECL_PUBLIC ArrayFormatter< char,N > : public ByteArrayFormatter<char, N>
00251 {};
00252 
00266 template<size_t N>
00267 class ECL_PUBLIC ArrayFormatter< unsigned char,N > : public ByteArrayFormatter<unsigned char, N>
00268 {
00269 };
00270 
00280 template<size_t N>
00281 class ECL_PUBLIC ArrayFormatter< float,N > : public FloatContainerFormatter< Array<float,N> >
00282 {
00283 public:
00292   ArrayFormatter(const int p=2, const int w=-1) : FloatContainerFormatter< Array<float,N> >(p,w)
00293   {};
00294   virtual ~ArrayFormatter()
00295   {}
00296 };
00297 
00307 template<size_t N>
00308 class ECL_PUBLIC ArrayFormatter< double,N > : public FloatContainerFormatter< Array<double,N> >
00309 {
00310 public:
00319   ArrayFormatter(const int p=2, const int w=-1) : FloatContainerFormatter< Array<double,N> >(p,w)
00320   {};
00321   virtual ~ArrayFormatter()
00322   {}
00323 };
00324 
00325 } // namespace formatters
00326 } // namespace ecl
00327 
00328 #endif /* ECL_CONTAINERS_ARRAY_FORMATTERS_HPP_ */


ecl_containers
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:17:43