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