00001 #ifndef IOF_FMT_SPEC_HPP_
00002 #define IOF_FMT_SPEC_HPP_
00003
00016 #include "iof/process_fmt_in.hpp"
00017 #include "iof/process_fmt_out.hpp"
00018 #include "iof/stream_fmt.hpp"
00019
00020
00021 namespace iof
00022 {
00023
00024
00025 namespace iof_private
00026 {
00027
00028
00029 template <typename TT>
00030 class formatted
00031 {
00032 public:
00033 const std::string fmt;
00034 const TT& obj;
00035
00036 formatted(const std::string& fmt, const TT& obj): fmt(fmt), obj(obj) {}
00037 inline void applyTo(std::ostream&, fmt_extensions& fmtExtensions) const;
00038
00039 private:
00040 };
00041
00042 }
00043
00044
00053 class fmt_spec
00054 {
00055 public:
00056 fmt_spec() {}
00057 fmt_spec(const std::string& fmt): my_fmt(fmt) {}
00058 void operator=(const std::string& fmt) {my_fmt = fmt;}
00059
00060 bool operator==(const std::string& fmt) const {return my_fmt == fmt;}
00061 bool operator==(const fmt_spec& rhs) const { return rhs.my_fmt == my_fmt; }
00062 bool operator!=(const fmt_spec& rhs) const { return ! (*this == rhs); }
00063
00064 const std::string& str() const {return my_fmt;}
00065
00067 template <typename TT>
00068 iof_private::formatted<TT> operator()(const TT& obj) const
00069 {
00070 return iof_private::formatted<TT>(my_fmt, obj);
00071 }
00072
00073 private:
00074 std::string my_fmt;
00075 };
00076
00077
00079 inline std::ostream&
00080 operator<<(std::ostream& out, const fmt_spec& fmt)
00081 {
00082 bool restore;
00083 iof_private::fmt_extensions fmtExtensions;
00084 iof_private::process1FmtSpec(out, fmt.str().c_str(), fmtExtensions, restore, false);
00085 if (fmtExtensions.extensionsUsed())
00086 throw std::runtime_error(
00087 "Formatting extensions (centering, ...) not supported without iof::fmtr");
00088 return out;
00089 }
00090
00091
00093 inline std::istream&
00094 operator>>(std::istream& in, const fmt_spec& fmt)
00095 {
00096 bool restore;
00097 char delim = 0;
00098 iof_private::process1FmtSpec(in, fmt.str().c_str(), restore, delim, false);
00099 return in;
00100 }
00101
00102
00103
00104 template <typename TT>
00105 inline void
00106 iof_private::formatted<TT>::applyTo(std::ostream& out, fmt_extensions& fmtExtensions)
00107 const
00108 {
00109 bool restore;
00110 iof_private::process1FmtSpec(out, fmt.c_str(), fmtExtensions, restore, false);
00111 outputAdvanced(obj, out, fmtExtensions);
00112 }
00113
00114
00116 template <typename TT>
00117 inline std::ostream&
00118 operator<<(std::ostream& out, const iof_private::formatted<TT>& fmtdObj)
00119 {
00120 iof_private::fmt_extensions fmtExtensions;
00121 fmtdObj.applyTo(out, fmtExtensions);
00122 if (fmtExtensions.extensionsUsed())
00123 throw std::runtime_error(
00124 "Formatting extensions (centering, ...) not supported without iof::fmtr");
00125 return out;
00126 }
00127
00128
00129 }
00130
00131 #endif // IOF_FMT_SPEC_HPP_
00132