bpipexx  1.0.6_01
 All Classes Functions Pages
bpipexx.h
1 // -*-c++-*-
2 
3 // --8<--8<--8<--8<--
4 //
5 // Copyright (C) 2013 Smithsonian Astrophysical Observatory
6 //
7 // This file is part of bpipexx
8 //
9 // bpipexx is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or (at
12 // your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 // -->8-->8-->8-->8--
23 
24 #ifndef BPIPEXX_H
25 #define BPIPEXX_H
26 
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 #include <bpipe/bpipe.h>
33 #include <bpipe/datatypes.h>
34 
35 #include <Exception/Exception.h>
36 
37 using namespace std;
38 
39 namespace SAOTrace {
40 
41  namespace BPipeX {
42 
54  template< typename T >
55  struct HdrF {
56 
57  string name;
58  BPMatrix *matrix;
59  BPDataType BPT;
60  T datum;
61  bool copy;
62 
70  HdrF(const char* cname, T idatum );
71 
76  T* data() { return &datum; }
77 
78  };
79 
80 
81 
93  template< typename T >
94  struct DpktF {
95 
96  string name;
97  BPMatrix *matrix;
98  BPDataType BPT;
99 
106  DpktF( const char* cname );
107 
108  };
109 
110 
111  class Simple {
112 
113  public:
114 
115  ~Simple( );
116  Simple( );
117 
118  Simple( const string& input, const string& output );
119 
132  template<typename T>
133  void add( HdrF<T> field) {
134 
135  if ( bpipe_hdrf_add( _bpipe, field.name.c_str(),
136  field.BPT, field.matrix,
137  field.data(), field.copy ) )
138  throw Exception( bpipe_strerror( bpipe_errno ) );
139  }
140 
153  template<typename T>
154  void add( const DpktF<T>& field ) {
155 
156  int res = bpipe_dpktf_add( _bpipe, field.name.c_str(),
157  field.BPT, field.matrix );
158  if ( res < 0 )
159  throw Exception( bpipe_strerror( bpipe_errno ) );
160 
161  if ( res == 1 ) {
162 
163  DpktField *dpktf = bpipe_dpktf( _bpipe, field.name.c_str() );
164 
165  if ( NULL == dpktf )
166  throw Exception( string("internal error: " ) + field.name + "should exist but doesn't" );
167 
168  if ( bpipe_dpktf_type( dpktf) != field.BPT ) {
169  ostringstream errstr;
170  errstr << "requested type of " << field.name << "(" << field.BPT << ")"
171  << "not equal to type of existing field" << "(" << bpipe_dpktf_type( dpktf ) << ")";
172 
173  throw Exception( errstr.str() );
174  }
175 
176  }
177 
178 
179  }
180 
181  void delete_hdrf( const char* name, size_t index=BPHdrfIdx_ALL );
182  void delete_dpktf( const char* name,
183  BPDataSite site = BPDSite_OUTPUT,
184  BPipeOutput *channel = BPOutputChannel_ALL );
185 
186 
187  bool has_hdrf( const char* name, size_t index=BPHdrfIdx_LAST );
188  bool has_dpktf( const char* name );
189 
190 
191 
201  template<typename T>
202  T* dpktf_data( const char* name ) {
203 
204  DpktField *dpktf = bpipe_dpktf( _bpipe, name );
205 
206  if ( NULL == dpktf )
207  throw Exception( string("non-existent data packet field: ") + name );
208 
209  return static_cast<T*>( static_cast<void*>( bpipe_dpktf_data( dpktf, core_image ) ) );
210  }
211 
222  template<typename T>
223  T* hdrf_data( const char* name, size_t idx = BPHdrfIdx_LAST ) {
224 
225  void *data = bpipe_hdrf_data( _bpipe, name, idx );
226 
227  if ( NULL == data )
228  throw Exception( string("non-existent data packet field: ") + name );
229 
230  return static_cast<T*>( data );
231  }
232  void map( int npkts = 1 );
233 
234  size_t read_dpkts ();
235 
236  void write_hdr ();
237 
238  void write_dpkts( BPipeOutput* bpo = BPOutputChannel_ALL );
239 
240  BPipe* bpipe( ) { return _bpipe ; }
241 
242 
243  private:
244  std::vector< BPipeOutput* > bpo;
245 
246  BPipe* _bpipe;
247  void* core_image;
248  size_t core_image_size;
249  int n_pkts;
250 
251  };
252 
253  }
254 }
255 
256 
257 
258 #endif // ! BPIPEXX_H