yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.0
WriterMethods.icc
Go to the documentation of this file.
1// This file contains boilerplate code for static writer functions in all
2// classes inheriting from Writer. These methods just forward to the methods on
3// the Writer base class, but code duplication can't be avoided without a
4// preprocessor hack like this, AFAIK.
5
6/// @name Writing a single analysis object.
7/// @{
8
9/// Write out object @a ao to output stream @a stream.
10static void write(std::ostream& stream, const AnalysisObject& ao) {
11 create().write(stream, ao);
12}
13
14/// Write out pointer-like object @a ao to output stream @a stream.
15template <typename T>
16static typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
17write(std::ostream& stream, const T& ao, int precision=-1) { write(stream, *ao, precision); }
18
19/// Write out object @a ao to file @a filename.
20static void write(const std::string& filename, const AnalysisObject& ao, int precision=-1) {
21 const size_t lastdot = filename.find_last_of(".");
22 std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
23 const bool compress = (fmt == "gz");
24 Writer& w = create();
25 if (precision > 0) w.setPrecision(precision);
26 w.useCompression(compress);
27 w.write(filename, ao);
28}
29
30/// Write out pointer-like object @a ao to file @a filename.
31template <typename T>
32static typename std::enable_if<DerefableToAO<T>::value>::type //< -> void if valid
33write(const std::string& filename, const T& ao, int precision=-1) { write(filename, *ao, precision); }
34
35/// @}
36
37
38/// @name Writing multiple analysis objects by collection.
39/// @{
40
41template <typename RANGE>
42static typename std::enable_if<CIterable<RANGE>::value>::type
43write(std::ostream& stream, const RANGE& aos, int precision=-1) {
44 create().write(stream, std::begin(aos), std::end(aos), precision);
45}
46
47template <typename RANGE>
48static typename std::enable_if<CIterable<RANGE>::value>::type
49write(const std::string& filename, const RANGE& aos, int precision=-1) {
50 const size_t lastdot = filename.find_last_of(".");
51 std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
52 const bool compress = (fmt == "gz");
53 Writer& w = create();
54 if (precision > 0) w.setPrecision(precision);
55 w.useCompression(compress);
56 w.write(filename, std::begin(aos), std::end(aos));
57}
58
59/// @}
60
61
62/// @name Writing multiple analysis objects by iterator range.
63/// @{
64
65/// Write out the objects specified by start iterator @a begin and end
66/// iterator @a end to output stream @a stream.
67///
68/// @todo Add SFINAE trait checking for AOITER = DerefableToAO
69template <typename AOITER>
70static void write(std::ostream& stream, const AOITER& begin, const AOITER& end, int precision=-1) {
71 create().write(stream, begin, end, precision);
72}
73
74/// Write out the objects specified by start iterator @a begin and end
75/// iterator @a end to file @a filename.
76///
77/// @todo Add SFINAE trait checking for AOITER = DerefableToAO
78template <typename AOITER>
79static void write(const std::string& filename, const AOITER& begin, const AOITER& end, int precision=-1) {
80 const size_t lastdot = filename.find_last_of(".");
81 std::string fmt = Utils::toLower(lastdot == std::string::npos ? filename : filename.substr(lastdot+1));
82 const bool compress = (fmt == "gz");
83 Writer& w = create();
84 if (precision > 0) w.setPrecision(precision);
85 w.useCompression(compress);
86 w.write(filename, begin, end);
87}
88
89/// @}