summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/vector_sink_impl.cc
blob: eafe7a47c788ccf2a638ba8abb716bdcf4f7f315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- c++ -*- */
/*
 * Copyright 2004,2008,2010,2013,2017-2018 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "vector_sink_impl.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/thread/thread.h>
#include <algorithm>
#include <iostream>

namespace gr {
namespace blocks {

template <class T>
typename vector_sink<T>::sptr vector_sink<T>::make(unsigned int vlen,
                                                   const int reserve_items)
{
    return gnuradio::make_block_sptr<vector_sink_impl<T>>(vlen, reserve_items);
}

template <class T>
vector_sink_impl<T>::vector_sink_impl(unsigned int vlen, const int reserve_items)
    : sync_block("vector_sink",
                 io_signature::make(1, 1, sizeof(T) * vlen),
                 io_signature::make(0, 0, 0)),
      d_vlen(vlen)
{
    gr::thread::scoped_lock guard(d_data_mutex);
    d_data.reserve(d_vlen * reserve_items);
}

template <class T>
vector_sink_impl<T>::~vector_sink_impl()
{
}

template <class T>
std::vector<T> vector_sink_impl<T>::data() const
{
    gr::thread::scoped_lock guard(d_data_mutex);
    return d_data;
}

template <class T>
std::vector<tag_t> vector_sink_impl<T>::tags() const
{
    gr::thread::scoped_lock guard(d_data_mutex);
    return d_tags;
}


template <class T>
void vector_sink_impl<T>::reset()
{
    gr::thread::scoped_lock guard(d_data_mutex);
    d_tags.clear();
    d_data.clear();
}

template <class T>
int vector_sink_impl<T>::work(int noutput_items,
                              gr_vector_const_void_star& input_items,
                              gr_vector_void_star& output_items)
{
    T* iptr = (T*)input_items[0];

    // can't touch this (as long as work() is working, the accessors shall not
    // read the data
    gr::thread::scoped_lock guard(d_data_mutex);
    for (unsigned int i = 0; i < noutput_items * d_vlen; i++)
        d_data.push_back(iptr[i]);
    std::vector<tag_t> tags;
    this->get_tags_in_range(
        tags, 0, this->nitems_read(0), this->nitems_read(0) + noutput_items);
    d_tags.insert(d_tags.end(), tags.begin(), tags.end());
    return noutput_items;
}

template class vector_sink<std::uint8_t>;
template class vector_sink<std::int16_t>;
template class vector_sink<std::int32_t>;
template class vector_sink<float>;
template class vector_sink<gr_complex>;
} /* namespace blocks */
} /* namespace gr */