1 /* -*- c++ -*- */
2 /*
3 * Copyright 2004,2008,2010,2013,2017-2018 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "vector_sink_impl.h"
29 #include <gnuradio/io_signature.h>
30 #include <gnuradio/thread/thread.h>
31 #include <algorithm>
32 #include <iostream>
33
34 namespace gr {
35 namespace blocks {
36
37 template <class T>
make(unsigned int vlen,const int reserve_items)38 typename vector_sink<T>::sptr vector_sink<T>::make(unsigned int vlen,
39 const int reserve_items)
40 {
41 return gnuradio::get_initial_sptr(new vector_sink_impl<T>(vlen, reserve_items));
42 }
43
44 template <class T>
vector_sink_impl(unsigned int vlen,const int reserve_items)45 vector_sink_impl<T>::vector_sink_impl(unsigned int vlen, const int reserve_items)
46 : sync_block("vector_sink",
47 io_signature::make(1, 1, sizeof(T) * vlen),
48 io_signature::make(0, 0, 0)),
49 d_vlen(vlen)
50 {
51 gr::thread::scoped_lock guard(d_data_mutex);
52 d_data.reserve(d_vlen * reserve_items);
53 }
54
55 template <class T>
~vector_sink_impl()56 vector_sink_impl<T>::~vector_sink_impl()
57 {
58 }
59
60 template <class T>
data() const61 std::vector<T> vector_sink_impl<T>::data() const
62 {
63 gr::thread::scoped_lock guard(d_data_mutex);
64 return d_data;
65 }
66
67 template <class T>
tags() const68 std::vector<tag_t> vector_sink_impl<T>::tags() const
69 {
70 gr::thread::scoped_lock guard(d_data_mutex);
71 return d_tags;
72 }
73
74
75 template <class T>
reset()76 void vector_sink_impl<T>::reset()
77 {
78 gr::thread::scoped_lock guard(d_data_mutex);
79 d_tags.clear();
80 d_data.clear();
81 }
82
83 template <class T>
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)84 int vector_sink_impl<T>::work(int noutput_items,
85 gr_vector_const_void_star& input_items,
86 gr_vector_void_star& output_items)
87 {
88 T* iptr = (T*)input_items[0];
89
90 // can't touch this (as long as work() is working, the accessors shall not
91 // read the data
92 gr::thread::scoped_lock guard(d_data_mutex);
93 for (unsigned int i = 0; i < noutput_items * d_vlen; i++)
94 d_data.push_back(iptr[i]);
95 std::vector<tag_t> tags;
96 this->get_tags_in_range(
97 tags, 0, this->nitems_read(0), this->nitems_read(0) + noutput_items);
98 d_tags.insert(d_tags.end(), tags.begin(), tags.end());
99 return noutput_items;
100 }
101
102 template class vector_sink<std::uint8_t>;
103 template class vector_sink<std::int16_t>;
104 template class vector_sink<std::int32_t>;
105 template class vector_sink<float>;
106 template class vector_sink<gr_complex>;
107 } /* namespace blocks */
108 } /* namespace gr */
109