astrotypes  0.0
DataFactory.h
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 #ifndef PSS_ASTROTYPES_SIGPROC_DATAFACTORY_H
25 #define PSS_ASTROTYPES_SIGPROC_DATAFACTORY_H
26 
27 #include "Header.h"
29 #include "SigProcFormat.h"
30 #include <array>
31 #include <memory>
32 #include <exception>
33 
34 namespace pss {
35 namespace astrotypes {
36 namespace sigproc {
37 
75 template<template<typename> class FnTemplate, typename DataFactoryTraits = DefaultDataFactoryTraits>
77 {
78  public:
79  // type parameter passed to the users templated function
80  template<typename DataT, typename AdapterT>
89  typedef DataT DataType;
90 
95  typedef AdapterT Adapter;
96  };
97 
98  protected:
99 
100  // type generator helpers
101  template<Header::DataType DataType, std::size_t nifs, std::size_t nbits>
103 
104  template<std::size_t nifs, std::size_t nbits>
105  struct MultiArrayType<Header::DataType::FilterBank, nifs, nbits> {
106  //typedef TimeFrequency<typename ElementType<nifs, nbits>::type, Allocator> type;
108  };
109 
110  template<std::size_t nifs, std::size_t nbits>
111  struct MultiArrayType<Header::DataType::TimeSeries, nifs, nbits> {
112  //typedef FrequencyTime<typename ElementType<nifs, nbits>::type, Allocator> type;
114  };
115 
116  template<Header::DataType DataType, bool Dummy=true>
118 
119  template<bool Dummy>
120  struct SigProcAdapterType<Header::DataType::FilterBank, Dummy> {
122  };
123 
124  template<bool Dummy>
125  struct SigProcAdapterType<Header::DataType::TimeSeries, Dummy> {
127  };
128 
129  private:
130  // these maps generate the code to select the correct type from runtime variables
131  template<Header::DataType DataType, unsigned Nifs, unsigned Nbits=8>
132  struct BitsRuntimeMap {
133  template<typename... Args>
134  static
135  void exec(unsigned number_of_bits, Args&&... args) {
136  if(number_of_bits != Nbits) BitsRuntimeMap<DataType, Nifs, BitToUnsignedInt<Nbits>::next>::exec(number_of_bits, std::forward<Args>(args)...);
137  FnTemplate<
141  >>::exec(std::forward<Args>(args)...);
142  }
143  };
144 
145  template<Header::DataType DataType, unsigned Nifs>
146  struct BitsRuntimeMap<DataType, Nifs, 0> {
147  template<typename... Args>
148  static
149  void exec(unsigned number_of_bits, Args&&...) {
150  static const std::string err(std::string("number of bits not supported: "));
151  throw std::runtime_error(std::string(err + std::to_string(number_of_bits)));
152  }
153  };
154 
155  template<Header::DataType DataType, unsigned N=1>
156  struct NifsRuntimeMap {
157  template<typename... Args>
158  static
159  void exec(unsigned nifs, unsigned number_of_bits, Args&&... args) {
160  if(nifs != N) NifsRuntimeMap<DataType, N+1>::exec(nifs, number_of_bits, std::forward<Args>(args)...);
161  BitsRuntimeMap<DataType, N>::exec(number_of_bits, std::forward<Args>(args)...);
162  }
163  };
164 
165  template<Header::DataType DataType>
166  struct NifsRuntimeMap<DataType, DataFactoryTraits::MaxNifs + 1> {
167  template<typename... Args>
168  static
169  void exec(unsigned, unsigned, Args&&...) {
170  static const std::string err(std::string("max nifs supported=") + std::to_string(DataFactoryTraits::MaxNifs));
171  throw std::runtime_error(err);
172  }
173  };
174 
175  public:
176  template<typename... Args>
177  static
178  void exec(Header const& h, Args&&... args)
179  {
181  {
182  NifsRuntimeMap<Header::DataType::FilterBank>::exec(h.number_of_ifs(), h.number_of_bits(), std::forward<Args>(args)...);
183  }
185  {
186  NifsRuntimeMap<Header::DataType::TimeSeries>::exec(h.number_of_ifs(), h.number_of_bits(), std::forward<Args>(args)...);
187  }
188  else {
189  throw std::runtime_error("sigproc: unsupported data type");
190  }
191  }
192 };
193 
194 } // namespace sigproc
195 } // namespace astrotypes
196 } // namespace pss
197 
198 #endif // PSS_ASTROTYPES_SIGPROC_DATAFACTORY_H
AdapterT Adapter
The type of Stream Adapter required to interpret input streams.
Definition: DataFactory.h:95
unsigned number_of_ifs() const
return the number of IF streams (e.g. polarisations) in each channel
Definition: Header.cpp:374
unsigned number_of_bits() const
return the number of bits
Definition: Header.cpp:348
Store SigProc header information.
Definition: Header.h:73
A template class representing values associated with a time and frequecny such as Stokes values or vo...
DataT DataType
the datatype that matched the format of the file
Definition: DataFactory.h:89
void data_type(DataType type)
set the data type
Definition: Header.cpp:188
A template class representing values associated with a time and frequecny such as Stokes values or vo...
FrequencyTime< typename DataFactoryTraits::template ElementTypeMap< nifs, nbits >::type > type
Definition: DataFactory.h:113
TimeFrequency< typename DataFactoryTraits::template ElementTypeMap< nifs, nbits >::type > type
Definition: DataFactory.h:107
Determines the static data type from a set of runtime parmmeters Once determined a user provided temp...
Definition: DataFactory.h:76
static void exec(Header const &h, Args &&...args)
Definition: DataFactory.h:178