Output.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 /*
32  * Time.h
33  *
34  * Created on: Aug 31, 2010
35  * Author: mpeura
36  */
37 
38 #include <string>
39 #include <iostream>
40 #include <fstream>
41 
42 #ifndef DRAIN_OUTPUT
43 #define DRAIN_OUTPUT
44 
45 #include <drain/Log.h>
46 #include <drain/util/FilePath.h>
47 
48 namespace drain {
49 
51 
56 class Output {
57 
58 public:
59 
60  inline
61  Output(){};
62 
63  inline
64  Output(const Output & output){}; // Should this copy something?
65 
67  inline
68  Output(const std::string & filename){
69  open(filename);
70  };
71 
73  inline
74  Output(const char *filename){
75  open(filename);
76  };
77 
79  inline
80  Output(const drain::FilePath & filePath){
81  open(filePath.str());
82  };
83 
84  // Output(std::ostream & ostr);
85 
87  ~Output();
88 
89  // Opens with std::open. Can be overridden with open(<filename>);
90  void open(const std::string & filename);
91 
92  operator std::ostream & ();
93 
94  inline
95  operator std::ofstream & (){
96  return ofstr;
97  }
98 
99  inline
100  operator bool (){
101  return static_cast<bool>(ofstr);
102  };
103 
104  inline // std::ofstream &
105  std::ostream & getStream(){
106  if (ofstr.is_open()){
107  return ofstr;
108  }
109  else {
110  return std::cout;
111  }
112  // return ofstr;
113  }
114 
115 
116 protected:
117 
118  //std::ostream ostr;
119  std::ofstream ofstr;
120 
121 };
122 
123 template <class T>
124 inline
125 Output & operator<<(Output & output, const T& value){
126  output.getStream() << value;
127  return output;
128 }
129 
130 } // drain::
131 
132 
133 #endif /* DRAIN_OUTPUT_H_ */
134 
Extracts and stores directory path, base filename and extension.
Definition: FilePath.h:58
Output utility. Opens stdout with "-". Closes upon destruction.
Definition: Output.h:56
Output(const std::string &filename)
Constructor that directly opens a file, or stdout with "-".
Definition: Output.h:68
~Output()
Closes upon destruction.
Definition: Output.cpp:62
Output(const char *filename)
Constructor that directly opens a file, or stdout with "-".
Definition: Output.h:74
Output(const drain::FilePath &filePath)
Constructor that directly opens a file.
Definition: Output.h:80
Definition: DataSelector.cpp:1277