yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Reader.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of YODA -- Yet more Objects for Data Analysis
4 // Copyright (C) 2008-2017 The YODA collaboration (see AUTHORS for details)
5 //
6 #ifndef YODA_READER_H
7 #define YODA_READER_H
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Utils/Traits.h"
11 #include <string>
12 #include <fstream>
13 #include <vector>
14 #include <type_traits>
15 #include <iostream>
16 
17 namespace YODA {
18 
19 
21  class Reader {
22  public:
23 
25  virtual ~Reader() {}
26 
27 
29 
30 
37  template<typename CONT>
38  typename std::enable_if<YODA::Pushable<CONT,AnalysisObject*>::value>::type
39  read(std::istream& stream, CONT& aos) {
40  // if CONT==std::vector<AnalysisObject*>, the compiler should select
41  // the virtual method below, since it prefers non-templated methods in the lookup
42  // otherwise we would enter a endless recursion. Check in case of problems.
43  std::vector<AnalysisObject*> v_aos;
44  read(stream, v_aos);
45  for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
46  }
47 
53  virtual void read(std::istream& stream, std::vector<AnalysisObject*>& aos) = 0;
54 
59  std::vector<AnalysisObject*> read(std::istream& stream) {
60  std::vector<AnalysisObject*> rtn;
61  read(stream, rtn);
62  return rtn;
63  }
64 
65 
73  template<typename CONT>
74  typename std::enable_if<YODA::Pushable<CONT,AnalysisObject*>::value>::type
75  read(const std::string& filename, CONT& aos) {
76  // if CONT==std::vector<AnalysisObject*>, the compiler should select
77  // the virtual method below, since it prefers non-templated methods in the lookup
78  // otherwise we would enter a endless recursion. Check in case of problems.
79  std::vector<AnalysisObject*> v_aos;
80  read(filename, v_aos);
81  for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
82  }
83 
89  void read(const std::string& filename, std::vector<AnalysisObject*>& aos) {
90  if (filename != "-") {
91  try {
92  std::ifstream instream;
93  instream.open(filename.c_str());
94  read(instream, aos);
95  instream.close();
96  } catch (std::ifstream::failure& e) {
97  throw WriteError("Writing to filename " + filename + " failed: " + e.what());
98  }
99  } else {
100  try {
101  read(std::cin, aos);
102  } catch (std::runtime_error& e) {
103  throw ReadError("Writing to stdout failed: " + std::string(e.what()));
104  }
105  }
106  }
107 
112  std::vector<AnalysisObject*> read(const std::string& filename) {
113  std::vector<AnalysisObject*> rtn;
114  read(filename, rtn);
115  return rtn;
116  }
117 
119 
120 
121  };
122 
123 
125  Reader& mkReader(const std::string& format_name);
126 
127 
128 }
129 
130 #endif
std::vector< AnalysisObject * > read(std::istream &stream)
Read in a collection of objects from output stream stream.
Definition: Reader.h:59
Error for file writing errors.
Definition: Exceptions.h:93
virtual ~Reader()
Virtual destructor.
Definition: Reader.h:25
Reader & mkReader(const std::string &format_name)
Factory function to make a writer object by format name or a filename.
Definition: Reader.cc:17
std::enable_if< YODA::Pushable< CONT, AnalysisObject * >::value >::type read(std::istream &stream, CONT &aos)
Read in a collection of objects objs from output stream stream.
Definition: Reader.h:39
Pure virtual base class for various output writers.
Definition: Reader.h:21
AnalysisObject is the base class for histograms and scatters.
std::enable_if< YODA::Pushable< CONT, AnalysisObject * >::value >::type read(const std::string &filename, CONT &aos)
Read in a collection of objects objs from file filename.
Definition: Reader.h:75
std::vector< AnalysisObject * > read(const std::string &filename)
Read in a collection of objects from output stream stream.
Definition: Reader.h:112
Error for file reading errors.
Definition: Exceptions.h:86
void read(const std::string &filename, std::vector< AnalysisObject *> &aos)
Read in a collection of objects objs from file filename.
Definition: Reader.h:89