yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.9.0
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-2021 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 "YODA/Index.h"
12 #include <string>
13 #include <fstream>
14 #include <vector>
15 #include <type_traits>
16 #include <iostream>
17 #include <sstream>
18 
19 namespace YODA {
20 
21 
23  class Reader {
24  public:
25 
27  virtual ~Reader() {}
28 
29 
32 
39  template<typename CONT>
40  typename std::enable_if<YODA::Pushable<CONT,AnalysisObject*>::value>::type
41  read(std::istream& stream, CONT& aos) {
42  // if CONT==std::vector<AnalysisObject*>, the compiler should select
43  // the virtual method below, since it prefers non-templated methods in the lookup
44  // otherwise we would enter a endless recursion. Check in case of problems.
45  std::vector<AnalysisObject*> v_aos;
46  read(stream, v_aos);
47  for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
48  }
49 
55  virtual void read(std::istream& stream, std::vector<AnalysisObject*>& aos) = 0;
56 
61  std::vector<AnalysisObject*> read(std::istream& stream) {
62  std::vector<AnalysisObject*> rtn;
63  read(stream, rtn);
64  return rtn;
65  }
66 
67 
75  template<typename CONT>
76  typename std::enable_if<YODA::Pushable<CONT,AnalysisObject*>::value>::type
77  read(const std::string& filename, CONT& aos) {
78  // if CONT==std::vector<AnalysisObject*>, the compiler should select
79  // the virtual method below, since it prefers non-templated methods in the lookup
80  // otherwise we would enter a endless recursion. Check in case of problems.
81  std::vector<AnalysisObject*> v_aos;
82  read(filename, v_aos);
83  for (const AnalysisObject* ao : v_aos) aos.push_back(ao);
84  }
85 
91  void read(const std::string& filename, std::vector<AnalysisObject*>& aos) {
92  if (filename != "-") {
93  try {
94  std::ifstream instream;
95  instream.open(filename.c_str());
96  read(instream, aos);
97  instream.close();
98  } catch (std::ifstream::failure& e) {
99  throw WriteError("Writing to filename " + filename + " failed: " + e.what());
100  }
101  } else {
102  try {
103  read(std::cin, aos);
104  } catch (std::runtime_error& e) {
105  throw ReadError("Writing to stdout failed: " + std::string(e.what()));
106  }
107  }
108  }
109 
114  std::vector<AnalysisObject*> read(const std::string& filename) {
115  std::vector<AnalysisObject*> rtn;
116  read(filename, rtn);
117  return rtn;
118  }
119 
121 
122 
129  Index mkIndex(const std::string& filename) {
130  if (filename != "-") {
131  try {
132  std::ifstream instream;
133  instream.open(filename.c_str());
134  Index idx(mkIndex(instream));
135  instream.close();
136  return idx;
137  } catch (std::ifstream::failure& e) {
138  throw WriteError("Reading of filename " + filename +
139  " failed: " + e.what());
140  }
141  } else {
142  try {
143  return mkIndex(std::cin);
144  } catch (std::runtime_error& e) {
145  throw ReadError("Reading from stdin failed: " + std::string(e.what()));
146  }
147  }
148  }
149 
156  virtual Index mkIndex(std::istream& stream) = 0;
157  };
158 
159 
161  Reader& mkReader(const std::string& format_name);
162 
163 
164 }
165 
166 #endif
std::vector< AnalysisObject * > read(std::istream &stream)
Read in a collection of objects from output stream stream.
Definition: Reader.h:61
Index mkIndex(const std::string &filename)
Make file index.
Definition: Reader.h:129
Error for file writing errors.
Definition: Exceptions.h:93
virtual ~Reader()
Virtual destructor.
Definition: Reader.h:27
Index of a file.
Definition: Index.h:13
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:41
Pure virtual base class for various output writers.
Definition: Reader.h:23
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:77
std::vector< AnalysisObject * > read(const std::string &filename)
Read in a collection of objects from output stream stream.
Definition: Reader.h:114
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:91