astrotypes  0.0
FileWriter.cpp
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018 PulsarSearchSoft
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
26 
27 namespace pss {
28 namespace astrotypes {
29 namespace sigproc {
30 
31 
32 template<typename HeaderType>
33 FileWriter<HeaderType>::FileWriter(std::string const& file_name)
34 {
35  do_open(file_name);
36 }
37 
38 template<typename HeaderType>
39 FileWriter<HeaderType>::FileWriter(std::string const& file_name, HeaderType const& header)
40  : BaseT(_header)
41 {
42  do_open(file_name);
43 }
44 
45 template<typename HeaderType>
46 FileWriter<HeaderType>::~FileWriter()
47 {
48 }
49 
50 template<typename HeaderType>
51 void FileWriter<HeaderType>::open(std::string const& file_name)
52 {
53  if(_stream.is_open()) _stream.close();
54  do_open(file_name);
55 }
56 
57 template<typename HeaderType>
58 void FileWriter<HeaderType>::do_open(std::string const& file_name)
59 {
60  _file_name = file_name;
61  _stream.open(_file_name, std::ios::out | std::ios::binary);
62  if(!_stream) throw std::runtime_error(file_name + " failed to open");
63  this->new_header();
64 }
65 
66 template<typename HeaderType>
67 template<typename DataType>
68 typename std::enable_if<has_dimensions<DataType, units::Time, units::Frequency>::value, FileWriter<HeaderType>>::type &
69 FileWriter<HeaderType>::operator<<(DataType const& data)
70 {
71  BaseT::write(_stream, data);
72  return *this;
73 }
74 
75 } // namespace sigproc
76 } // namespace astrotypes
77 } // namespace pss
std::ostream & operator<<(std::ostream &os, SlicePosition< Rank > const &pos)
Definition: SlicePosition.h:53
void do_open(std::string const &file_name)
Definition: FileReader.cpp:53