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_IO_h
7 #define YODA_IO_h
8 
9 #include "YODA/Writer.h"
10 #include "YODA/Reader.h"
11 #include "YODA/Index.h"
12 
13 namespace YODA {
14 
15 
16   /// @name Writer functions to files (with automatic format detection)
17   /// @{
18 
19   /// Write out object @a ao to file @a filename
write(const std::string & filename,const AnalysisObject & ao)20   inline void write(const std::string& filename, const AnalysisObject& ao) {
21     Writer& w = mkWriter(filename);
22     w.write(filename, ao);
23   }
24 
25   /// Write out a collection of objects @a objs to file @a filename.
26   template <typename RANGE>
write(const std::string & filename,const RANGE & aos)27   inline void write(const std::string& filename, const RANGE& aos) {
28     Writer& w = mkWriter(filename);
29     w.write(filename, aos);
30   }
31 
32   /// Write out the objects specified by start iterator @a begin and end
33   /// iterator @a end to file @a filename.
34   template <typename AOITER>
write(const std::string & filename,const AOITER & begin,const AOITER & end)35   inline void write(const std::string& filename, const AOITER& begin, const AOITER& end) {
36     Writer& w = mkWriter(filename);
37     w.write(filename, begin, end);
38   }
39 
40   /// @}
41 
42 
43   /// @name Writer functions to streams (with explicit format specification)
44   /// @{
45 
46   /// Write out object @a ao to stream @a os with format @a fmt
write(std::ostream & os,const AnalysisObject & ao,const std::string & fmt)47   inline void write(std::ostream& os, const AnalysisObject& ao, const std::string& fmt) {
48     Writer& w = mkWriter(fmt);
49     w.write(os, ao);
50   }
51 
52   /// Write out a collection of objects @a objs to file @a filename.
53   template <typename RANGE>
write(std::ostream & os,const RANGE & aos,const std::string & fmt)54   inline void write(std::ostream& os, const RANGE& aos, const std::string& fmt) {
55     Writer& w = mkWriter(fmt);
56     w.write(os, aos);
57   }
58 
59   /// Write out the objects specified by start iterator @a begin and end
60   /// iterator @a end to file @a filename.
61   template <typename AOITER>
write(std::ostream & os,const AOITER & begin,const AOITER & end,const std::string & fmt)62   inline void write(std::ostream& os, const AOITER& begin, const AOITER& end, const std::string& fmt) {
63     Writer& w = mkWriter(fmt);
64     w.write(os, begin, end);
65   }
66 
67   /// @}
68 
69 
70   /// @name Reader functions from files (with automatic format detection)
71   /// @{
72 
73   /// @brief Read in a collection of objects @a objs from file @a filename.
74   ///
75   /// This version fills (actually, appends to) a supplied vector, avoiding
76   /// copying, and is hence CPU efficient. The appropriate format reader will
77   /// be determined from the filename.
78   ///
79   /// @todo Use SFINAE magic to allow ~arbitrary collection<AnalysisObject*> (with push_back()?) to be passed
read(const std::string & filename,std::vector<AnalysisObject * > & aos)80   inline void read(const std::string& filename, std::vector<AnalysisObject*>& aos) {
81     Reader& r = mkReader(filename);
82     r.read(filename, aos);
83   }
84 
85   /// @brief Read in a collection of objects from file @a filename.
86   ///
87   /// This version returns a vector by value, involving copying, and is hence
88   /// less CPU efficient than the alternative version where a vector is filled
89   /// by reference. The appropriate format reader will be determined from the
90   /// filename.
read(const std::string & filename)91   inline std::vector<AnalysisObject*> read(const std::string& filename) {
92     std::vector<AnalysisObject*> rtn;
93     read(filename, rtn);
94     return rtn;
95   }
96 
97   /// @brief Make index of a file.
mkIndex(const std::string & filename)98   inline Index mkIndex(const std::string& filename) {
99     Reader& r = mkReader(filename);
100     return r.mkIndex(filename);
101   }
102 
103   /// @}
104 
105 
106   /// @name Reader functions from streams (with explicit format specification)
107   /// @{
108 
109   /// @brief Read in a collection of objects @a objs from stream @a is, expecting format @a fmt.
110   ///
111   /// This version fills (actually, appends to) a supplied vector, avoiding
112   /// copying, and is hence CPU efficient.
113   ///
114   /// @todo Use SFINAE magic to allow ~arbitrary collection<AnalysisObject*> (with push_back()?) to be passed
read(std::istream & is,std::vector<AnalysisObject * > & aos,const std::string & fmt)115   inline void read(std::istream& is, std::vector<AnalysisObject*>& aos, const std::string& fmt) {
116     Reader& r = mkReader(fmt);
117     r.read(is, aos);
118   }
119 
120   /// @brief Read in a collection of objects from stream @a is, expecting format @a fmt.
121   ///
122   /// This version returns a vector by value, involving copying, and is hence
123   /// less CPU efficient than the alternative version where a vector is filled
124   /// by reference.
read(std::istream & is,const std::string & fmt)125   inline std::vector<AnalysisObject*> read(std::istream& is, const std::string& fmt) {
126     std::vector<AnalysisObject*> rtn;
127     read(is, rtn, fmt);
128     return rtn;
129   }
130 
131   /// @}
132 
133 
134 }
135 
136 #endif
137