astrotypes  0.0
ResizeAdapter.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 
28 namespace {
29 // unpack tuple in to arguments
30 template <typename TupleType, std::size_t Index=std::tuple_size<TupleType>::value>
31 struct ResizeHelper
32 {
33  template<typename DataType, typename... Ts>
34  static inline void exec(DataType& data, TupleType const& tuple, Ts&&... args)
35  {
36  ResizeHelper<TupleType, Index-1>::exec(data, tuple, std::forward<Ts>(args)..., std::get<Index-1>(tuple));
37  }
38 
39  template<typename DataType>
40  static inline void exec(DataType& data, TupleType const& tuple)
41  {
42  ResizeHelper<TupleType, Index-1>::exec(data, tuple, std::get<Index-1>(tuple));
43  }
44 };
45 
46 template<typename TupleType>
47 struct ResizeHelper<TupleType, 0>
48 {
49  template<typename DataType, typename... Ts>
50  static inline void exec(DataType& data, TupleType const&, Ts&&... args) {
51  data.resize(std::forward<Ts>(args)...);
52  }
53 };
54 
55 } // namespace
56 
57 template<typename Dimension, typename... Dimensions>
58 ResizeAdapter<Dimension, Dimensions...>::ResizeAdapter()
59 {
60 }
61 
62 template<typename Dimension, typename... Dimensions>
63 template<typename Dim, typename... Dims>
64 ResizeAdapter<Dimension, Dimensions...>::ResizeAdapter(DimensionSize<Dim> dim, DimensionSize<Dims>... dims)
65 {
66  tuple_insert_type(_sizes, dim, dims...);
67 }
68 
69 // helper funtion for Stream construction
70 namespace {
71 template<typename... Dimensions>
72 struct DimensionCopy;
73 
74 template<typename Dimension>
75 struct DimensionCopy<Dimension>
76 {
77  template<typename StreamType, typename TupleType>
78  inline static void exec(StreamType const& stream, TupleType& tuple)
79  {
80  tuple_insert_type(tuple, stream.template dimension<Dimension>());
81  }
82 };
83 
84 template<typename Dimension, typename... Dimensions>
85 struct DimensionCopy<Dimension, Dimensions...>
86 {
87  template<typename StreamType, typename TupleType>
88  inline static void exec(StreamType const& stream, TupleType& tuple)
89  {
90  tuple_insert_type(tuple, stream.template dimension<Dimension>());
91  DimensionCopy<Dimensions...>::exec(stream, tuple);
92  }
93 };
94 
95 } // namespace
96 template<typename Dimension, typename... Dimensions>
97 template<typename StreamType>
98 ResizeAdapter<Dimension, Dimensions...>::Stream<StreamType>::Stream(StreamType& is, ResizeAdapter& ra)
99  : BaseT(is, ra._sizes)
100 {
101  // transfer dimension info fromt the stream into the _sizes tuple
102  DimensionCopy<Dimension, Dimensions...>::exec(is, ra._sizes);
103 }
104 
105 template<typename Stream, typename Dim, typename... Dims>
106 template<typename T>
107 typename std::enable_if<has_dimensions<typename std::decay<T>::type, Dim, Dims...>::value, Stream>::type&
109 {
110  ResizeHelper<typename std::remove_reference<decltype(this->_sizes)>::type>::exec(tf, this->_sizes);
111  this->_stream >> tf;
112  return _stream;
113 }
114 
115 template<typename Stream, typename Dim, typename... Dims>
116 template<typename... ODims>
117 ExtendedResizeAdapterStream<Stream, typename merge_tuples_type<std::tuple<DimensionSize<ODims>...>, typename ResizeAdapterStreamBase<Stream, Dim, Dims...>::TupleType>::type>
118 ResizeAdapterStreamBase<Stream, Dim, Dims...>::operator>>(ResizeAdapter<ODims...>& adapter)
119 {
120  return ExtendedResizeAdapterStream<Stream, typename merge_tuples_type<std::tuple<DimensionSize<ODims>...>, TupleType>::type>(this->_stream, merge_tuples(adapter._sizes, this->_sizes));
121 }
122 
123 } // namespace astrotypes
124 } // namespace pss
void tuple_insert_type(std::tuple< TupleTs...> &tuple, Ts const &...values)
Definition: TypeTraits.h:208
TestDimensionStream< SDims...> & operator>>(TestDimensionStream< SDims...> &is, TestMultiArray< T, Dim, Dims...> &)
merge_tuples_type< Tuple1, Tuple2 >::type merge_tuples(Tuple1 const &tuple1, Tuple2 const &tuple2)
Definition: TypeTraits.h:285