astrotypes  0.0
SigProcFormat.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  */
24 
25 namespace pss {
26 namespace astrotypes {
27 namespace sigproc {
28 
29 namespace {
30  //should work with any iterator
31  template<typename IteratorType, typename IteratorTag, typename ValueType>
32  struct do_write {
33  static void exec(IteratorType begin, IteratorType const end, std::ostream& os) {
34  while(begin != end) {
35  os.write(reinterpret_cast<const char*>(&(*begin)), sizeof(ValueType));
36  ++begin;
37  }
38  }
39  };
40 
41  // we can speed things up if the memory is contiguous
42  template<typename IteratorType, typename ValueType>
43  struct do_write<IteratorType, std::random_access_iterator_tag, ValueType>
44  {
45 
46  static void exec(IteratorType begin, IteratorType const end, std::ostream& os) {
47  os.write(reinterpret_cast<const char*>(&(*begin)), std::distance(begin, end) * sizeof(ValueType));
48  }
49  };
50 
51  template<typename IteratorType, typename IteratorTag, typename ValueType>
52  struct do_read {
53  static void exec(IteratorType begin, IteratorType const end, std::istream& is) {
54  while(begin != end) {
55  is.read(reinterpret_cast<char*>(&(*begin)), sizeof(ValueType));
56  ++begin;
57  }
58  }
59  };
60 
61  template<typename IteratorType, typename ValueType>
62  struct do_read<IteratorType, std::random_access_iterator_tag, ValueType>
63  {
64  static void exec(IteratorType begin, IteratorType const end, std::istream& is) {
65  is.read(reinterpret_cast<char*>(&(*begin)), std::distance(begin, end) * sizeof(ValueType));
66  }
67  };
68 
69  // calls the correct specialization
70  template<typename IteratorType>
71  void write(IteratorType begin, IteratorType const end, std::ostream& os) {
72  do_write<IteratorType
73  , typename std::iterator_traits<typename std::remove_reference<IteratorType>::type>::iterator_category
74  , typename std::iterator_traits<typename std::remove_reference<IteratorType>::type>::value_type
75  >::exec(begin, end, os);
76  }
77 
78  template<typename IteratorType>
79  void read(IteratorType begin, IteratorType const end, std::istream& is) {
80  do_read<IteratorType
81  , typename std::iterator_traits<typename std::remove_reference<IteratorType>::type>::iterator_category
82  , typename std::iterator_traits<typename std::remove_reference<IteratorType>::type>::value_type
83  >::exec(begin, end, is);
84  }
85 }
86 
87 template<typename T>
88 typename std::enable_if<has_exact_dimensions<T, units::Time, units::Frequency>::value, SigProcFormat<units::Time, units::Frequency>::ISigProcFormat const&>::type
90 {
91  read(d.begin(), d.end(), _is);
92  return *this;
93 }
94 
95 template<typename T>
96 typename std::enable_if<has_exact_dimensions<T, units::Time>::value, SigProcFormat<units::Time, units::Frequency>::ISigProcFormat const&>::type
98 {
99  read(d.begin(), d.end(), _is);
100  return *this;
101 }
102 
103 template<typename T>
104 typename std::enable_if<has_exact_dimensions<T, units::Time, units::Frequency>::value, SigProcFormat<units::Time, units::Frequency>::OSigProcFormat const&>::type
106 {
107  write(d.cbegin(), d.cend(), _os);
108  return *this;
109 }
110 
111 template<typename T>
112 typename std::enable_if<has_exact_dimensions<T, units::Frequency, units::Time>::value, SigProcFormat<units::Time, units::Frequency>::ISigProcFormat const&>::type
114 {
115  for(DimensionIndex<units::Time> spectrum_num(0); spectrum_num < d.template size<units::Time>(); ++spectrum_num)
116  {
117  auto spectrum = d[spectrum_num];
118  read(spectrum.begin(), spectrum.end(), _is);
119  }
120  return *this;
121 }
122 
123 template<typename T>
124 typename std::enable_if<has_exact_dimensions<T, units::Time>::value, SigProcFormat<units::Time, units::Frequency>::OSigProcFormat const&>::type
126 {
127  write(d.cbegin(), d.cend(), _os);
128  return *this;
129 }
130 
131 template<typename T>
132 typename std::enable_if<has_exact_dimensions<T, units::Frequency>::value, SigProcFormat<units::Time, units::Frequency>::OSigProcFormat const&>::type
134 {
135  write(d.cbegin(), d.cend(), _os);
136  return *this;
137 }
138 
139 template<typename T>
140 typename std::enable_if<has_exact_dimensions<T, units::Frequency, units::Time>::value, SigProcFormat<units::Time, units::Frequency>::OSigProcFormat const&>::type
142 {
143  for(DimensionIndex<units::Time> spectrum_num(0); spectrum_num < d.template size<units::Time>(); ++spectrum_num)
144  {
145  auto const spectrum = d[spectrum_num];
146  write(spectrum.cbegin(), spectrum.cend(), _os);
147  }
148  return *this;
149 }
150 
151 template<typename T, typename Alloc>
153 {
154  for(DimensionIndex<units::Frequency> channel_num(0); channel_num < d.template size<units::Frequency>(); ++channel_num)
155  {
156  auto const& channel = d.channel(channel_num);
157  write(channel.cbegin(), channel.cend(), _os);
158  }
159  return *this;
160 }
161 
162 template<typename T, typename Alloc>
164 {
165  write(d.cbegin(), d.cend(), _os);
166  return *this;
167 }
168 
169 template<typename T>
170 typename std::enable_if<has_exact_dimensions<T, units::Time, units::Frequency>::value, SigProcFormat<units::Frequency, units::Time>::ISigProcFormat const&>::type
172 {
173  for(DimensionIndex<units::Frequency> channel_num(0); channel_num < d.template size<units::Frequency>(); ++channel_num)
174  {
175  auto channel = d.channel(channel_num);
176  read(channel.begin(), channel.end(), _is);
177  }
178  return *this;
179 }
180 
181 template<typename T>
182 typename std::enable_if<has_exact_dimensions<T, units::Frequency, units::Time>::value, SigProcFormat<units::Frequency, units::Time>::ISigProcFormat const&>::type
184 {
185  read(d.begin(), d.end(), _is);
186  return *this;
187 }
188 
189 template<typename T>
190 typename std::enable_if<has_exact_dimensions<T, units::Time>::value, SigProcFormat<units::Frequency, units::Time>::OSigProcFormat const&>::type
192 {
193  write(d.cbegin(), d.cend(), _os);
194  return *this;
195 }
196 
197 template<typename T>
198 typename std::enable_if<has_exact_dimensions<T, units::Frequency>::value, SigProcFormat<units::Frequency, units::Time>::OSigProcFormat const&>::type
200 {
201  write(d.cbegin(), d.cend(), _os);
202  return *this;
203 }
204 
206 {
207  return OSigProcFormat(os);
208 }
209 
211 {
212  return ISigProcFormat(is);
213 }
214 
216 {
217  return OSigProcFormat(os);
218 }
219 
221 {
222  return ISigProcFormat(is);
223 }
224 
225 template<typename Dimension1, typename Dimension2>
226 typename SigProcFormat<Dimension1, Dimension2>::OSigProcFormat operator<<(std::ostream& os, SigProcFormat<Dimension1, Dimension2> const& f)
227 {
228  return f << os;
229 }
230 
231 template<typename Dimension1, typename Dimension2>
233 {
234  return f >> is;
235 }
236 
237 } // namespace sigproc
238 } // namespace astrotypes
239 } // namespace pss
A tagged dimensionIndex variable.
STL namespace.
A template class representing values associated with a time and frequecny such as Stokes values or vo...
SigProcFormat< Dimension1, Dimension2 >::ISigProcFormat operator>>(std::istream &is, SigProcFormat< Dimension1, Dimension2 > const &f)
A template class representing values associated with a time and frequecny such as Stokes values or vo...
std::istream & operator>>(std::istream &stream, HeaderBase< Derived > &headers)
Definition: HeaderBase.cpp:180
Channel channel(std::size_t channel_number)
retrun a single channel across all time samples
stream adapter to convert TimeFrequency or FrequencyTime types to filterbank output ...
Definition: SigProcFormat.h:39
SigProcFormat< Dimension1, Dimension2 >::OSigProcFormat operator<<(std::ostream &os, SigProcFormat< Dimension1, Dimension2 > const &f)