core/vnl/vnl_matlab_print2.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_matlab_print2.h
00002 #ifndef vnl_matlab_print2_h_
00003 #define vnl_matlab_print2_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 //
00010 // After including this header file, the client should be able to say :
00011 // \code
00012 //   vnl_matrix<double> foo(3, 14);
00013 //  ....
00014 //   vcl_cerr << "and the blasted matrix is :" << endl
00015 //            << vnl_matlab_print(foo)
00016 //            << vnl_matlab_print(foo, "foo")
00017 //            << vnl_matlab_print(foo, 0, vnl_matlab_fmt_long);
00018 // \endcode
00019 // instead of
00020 // \code
00021 //  ....
00022 //   vcl_cerr << "and the blasted matrix is :" << endl;
00023 //   vnl_matlab_print(vcl_cerr, foo);
00024 //   vnl_matlab_print(vcl_cerr, foo, "foo");
00025 //   vnl_matlab_print(vcl_cerr, foo, 0, vnl_matlab_fmt_long);
00026 // \endcode
00027 //
00028 // \author fsm
00029 
00030 #include <vnl/vnl_matlab_print.h>
00031 #include <vcl_iosfwd.h>
00032 
00033 // The proxy classes.
00034 template <class T>
00035 struct vnl_matlab_print_proxy
00036 {
00037   T const &obj;
00038   char const *name;
00039   vnl_matlab_print_format format;
00040   vnl_matlab_print_proxy(T const &obj_,
00041                          char const *name_,
00042                          vnl_matlab_print_format format_)
00043     : obj(obj_), name(name_), format(format_) { }
00044   ~vnl_matlab_print_proxy() { }
00045 };
00046 
00047 // Output operator for the proxies.
00048 template <class T>
00049 inline
00050 vcl_ostream &operator<<(vcl_ostream &os, vnl_matlab_print_proxy<T> const &mp)
00051 {
00052   return vnl_matlab_print(os, mp.obj, mp.name, mp.format);
00053 }
00054 
00055 // Functions to make proxies. This should work for objects of types
00056 // derived from vnl_vector, vnl_matrix etc because the overload
00057 // resolution is done in the operator<< above.
00058 template <class T>
00059 inline
00060 vnl_matlab_print_proxy<T>
00061 vnl_matlab_print(T const &obj,
00062                  char const *name = 0,
00063                  vnl_matlab_print_format format = vnl_matlab_print_format_default)
00064 {
00065   return vnl_matlab_print_proxy<T>(obj, name, format);
00066 }
00067 
00068 #define VNL_MATLAB_PRINT2_INSTANTIATE(T) \
00069 template struct vnl_matlab_print_proxy<T >; \
00070 VCL_INSTANTIATE_INLINE(vcl_ostream& \
00071                        operator<<(vcl_ostream&, vnl_matlab_print_proxy<T > const&)); \
00072 VCL_INSTANTIATE_INLINE(vnl_matlab_print_proxy<T > \
00073                        vnl_matlab_print(T const&, char const*, vnl_matlab_print_format))
00074 
00075 #endif // vnl_matlab_print2_h_