3 #ifndef SWIG__COMMON__NDS_BUFFER_HH
4 #define SWIG__COMMON__NDS_BUFFER_HH
11 #include "nds_export.hh"
12 #include "nds_channel.hh"
17 inline namespace abi_0
27 class buffer :
public channel
33 typedef long gps_second_type;
37 typedef long gps_nanosecond_type;
41 typedef size_t size_type;
45 typedef std::vector< unsigned char > data_type;
47 static const gps_second_type GPS_INF = 1999999999;
76 buffer(
const channel& ChannelInfo,
77 gps_second_type Second,
78 gps_nanosecond_type NanoSecond,
80 size_type BufferSize );
99 buffer(
const channel& ChannelInfo,
100 gps_second_type Second,
101 gps_nanosecond_type NanoSecond,
102 data_type&& Buffer );
110 DLL_EXPORT size_type Samples( )
const;
118 DLL_EXPORT gps_second_type Start( )
const;
126 DLL_EXPORT gps_nanosecond_type StartNano( )
const;
134 DLL_EXPORT gps_second_type Stop( )
const;
142 DLL_EXPORT gps_nanosecond_type StopNano( )
const;
146 DLL_EXPORT
void swap( buffer& Source );
154 DLL_EXPORT
void resize( size_type N );
168 template <
typename T >
172 const auto val_type =
173 NDS::channel_data_type_conversion< T >::value;
175 if ( val_type == DataType( ) ||
176 ( std::is_same< T, void >::value &&
177 val_type == NDS::channel::DATA_TYPE_UNKNOWN ) )
179 return reinterpret_cast< const T*
>( data.data( ) );
181 throw std::runtime_error(
182 "Invalid cast, the buffer data type is "
183 "not compatible with your requested "
195 template <
typename T >
199 const auto val_type =
200 NDS::channel_data_type_conversion< T >::value;
202 if ( val_type == DataType( ) ||
203 ( std::is_same< T, void >::value &&
204 val_type == NDS::channel::DATA_TYPE_UNKNOWN ) )
206 return reinterpret_cast< const T*
>(
207 data.data( ) + ( Samples( ) * DataTypeSize( ) ) );
209 throw std::runtime_error(
210 "Invalid cast, the buffer data type is "
211 "not compatible with your requested "
225 template <
typename T >
227 at( size_type index )
const
229 const T* start = cbegin< T >( );
230 if ( array_out_of_range( index, Samples( ) ) )
232 throw std::out_of_range(
233 "The requested index is out of range" );
235 return start[ index ];
254 DLL_EXPORT gps_second_type
255 samples_to_seconds( size_type offset_samples )
const;
272 DLL_EXPORT gps_second_type
273 samples_to_trailing_nanoseconds( size_type offset_samples )
const;
287 bytes_to_samples( size_type offset_bytes )
const;
306 seconds_to_samples( gps_second_type offset_seconds,
307 gps_nanosecond_type offset_nano = 0 )
const;
322 samples_to_bytes( size_type offset_samples )
const;
340 reset_channel_info(
const channel& ChannelInfo,
341 gps_second_type Second,
342 gps_nanosecond_type NanoSecond );
345 gps_second_type gps_second;
346 gps_nanosecond_type gps_nanosecond;
349 template <
typename IndexType >
350 bool array_out_of_range( IndexType Index, IndexType Max )
const;
353 bool is_minute_trend( )
const;
356 typedef std::vector< buffer > buffers_type;
359 buffer::is_minute_trend( )
const
361 return Type( ) == CHANNEL_TYPE_MTREND;
366 inline buffer::gps_second_type
367 buffer::samples_to_seconds( buffer::size_type offset_samples )
const
369 if ( is_minute_trend( ) )
371 return static_cast< gps_second_type
>( offset_samples * 60 );
373 return static_cast< gps_second_type
>( offset_samples /
379 inline buffer::gps_nanosecond_type
380 buffer::samples_to_trailing_nanoseconds(
381 NDS::buffer::size_type offset_samples )
const
383 if ( is_minute_trend( ) )
387 gps_nanosecond_type samples = offset_samples %
388 static_cast< gps_nanosecond_type
>( SampleRate( ) );
389 return static_cast< gps_nanosecond_type
>( 1e+9 * samples /
395 inline buffer::size_type
396 buffer::bytes_to_samples( buffer::size_type offset_bytes )
const
398 return offset_bytes / DataTypeSize( );
403 inline buffer::size_type
404 buffer::seconds_to_samples(
405 buffer::gps_second_type offset_seconds,
406 buffer::gps_nanosecond_type offset_nano )
const
408 if ( is_minute_trend( ) )
410 return static_cast< size_type
>( offset_seconds / 60 );
412 return static_cast< size_type
>( offset_seconds * SampleRate( ) );
417 inline buffer::size_type
418 buffer::samples_to_bytes( buffer::size_type offset_samples )
const
420 return offset_samples * DataTypeSize( );
428 inline buffer::size_type
429 buffer::Samples( )
const
431 return data.size( ) / DataTypeSize( );
436 inline buffer::gps_second_type
437 buffer::Start( )
const
444 inline buffer::gps_nanosecond_type
445 buffer::StartNano( )
const
447 return gps_nanosecond;
452 inline buffer::gps_second_type
453 buffer::Stop( )
const
455 auto samples = Samples( );
457 StartNano( ) + samples_to_trailing_nanoseconds( samples );
458 gps_second_type additional_from_nano =
459 ( total_ns >=
static_cast< gps_nanosecond_type
>( 1e+9 ) ? 1
461 return gps_second + samples_to_seconds( samples ) +
462 additional_from_nano;
465 inline buffer::gps_nanosecond_type
466 buffer::StopNano( )
const
468 auto samples = Samples( );
470 StartNano( ) + samples_to_trailing_nanoseconds( samples );
471 return total_ns % 1000000000;
477 buffer::resize( size_type N )
479 data.resize( N * DataTypeSize( ) );
485 DLL_EXPORT
extern std::ostream& operator<<( std::ostream& os,
488 DLL_EXPORT
extern std::ostream& operator<<( std::ostream& os,
489 const buffers_type& obj );
496 buffer::array_out_of_range< size_t >(
size_t Index,
size_t Max )
const
498 return ( Index >= Samples( ) );
501 template <
typename IndexType >
503 buffer::array_out_of_range( IndexType Index, IndexType Max )
const
505 return ( Index <= 0 || Index >= Samples( ) );