GNU Radio Manual and C++ API Reference 3.10.5.1
The Free & Open Software Radio Ecosystem
buffer_type.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2020 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * SPDX-License-Identifier: GPL-3.0-or-later
8 *
9 */
10
11#ifndef INCLUDED_GR_RUNTIME_CUSTOM_BUFFER_TYPE_H
12#define INCLUDED_GR_RUNTIME_CUSTOM_BUFFER_TYPE_H
13
14#include <gnuradio/api.h>
16
17#include <cstdint>
18#include <functional>
19#include <mutex>
20#include <string>
21#include <typeinfo>
22#include <vector>
23
24namespace gr {
25
26/*!
27 * \brief Base class for describing a buffer's type.
28 */
30{
31public:
32 virtual ~buffer_type_base(){};
33
34 // Do not allow copying or assignment
35 // buffer_type_base(buffer_type_base const&) = delete;
36
37 // Temporarily define copy constructor to work around pybind issue with
38 // default non-copyable function argument
39 buffer_type_base(buffer_type_base const& other) : d_name(other.d_name) {}
40
41 void operator=(buffer_type_base const&) = delete;
42
43 // Allow equality and inequality comparison
44 bool operator==(const buffer_type_base& other) const
45 {
46 return d_name == other.d_name;
47 }
48
49 bool operator!=(const buffer_type_base& other) const
50 {
51 return d_name != other.d_name;
52 }
53
54 // Do not allow other comparison (just in case)
55 bool operator<(const buffer_type_base& other) = delete;
56 bool operator>(const buffer_type_base& other) = delete;
57 bool operator<=(const buffer_type_base& other) = delete;
58 bool operator>=(const buffer_type_base& other) = delete;
59 /*!
60 * \brief Get the human-readable name of the type
61 */
62 const std::string& name() const { return d_name; }
63
64 /*!
65 * \brief Make and return a buffer subclass of the corresponding type
66 */
67 virtual buffer_sptr
68 make_buffer([[maybe_unused]] int nitems,
69 [[maybe_unused]] size_t sizeof_item,
70 [[maybe_unused]] uint64_t downstream_lcm_nitems,
71 [[maybe_unused]] uint32_t downstream_max_out_mult,
72 [[maybe_unused]] block_sptr link = block_sptr(),
73 [[maybe_unused]] block_sptr buf_owner = block_sptr()) const
74 {
75 // NOTE: this is *never* intended to be called directly, instead the overridden
76 // version from a derived class (created from the template below) will be
77 // called.
78 return nullptr;
79 }
80
81protected:
82 const std::string d_name;
83
84 buffer_type_base(const std::string name) : d_name(name) {}
85};
86
88typedef std::vector<std::reference_wrapper<const buffer_type_base>> gr_vector_buffer_type;
89
90/*!
91 * \brief Template used to create buffer types. Note that the factory_class parameter
92 * must contain a static function make_buffer() that matches the signature below
93 * and will be used to create instances of the corresponding buffer type.
94 */
95template <typename classname, typename factory_class>
96struct buftype : public buffer_type_base {
97public:
98 using factory = factory_class;
99 buffer_sptr make_buffer(int nitems,
100 size_t sizeof_item,
101 uint64_t downstream_lcm_nitems,
102 uint32_t downstream_max_out_mult,
103 block_sptr link = block_sptr(),
104 block_sptr buf_owner = block_sptr()) const override
105 {
106 return factory::make_buffer(nitems,
107 sizeof_item,
108 downstream_lcm_nitems,
109 downstream_max_out_mult,
110 link,
111 buf_owner);
112 }
113
114 buftype() : buffer_type_base(typeid(classname).name()) {}
115};
116
117} // namespace gr
118
119#endif /* INCLUDED_GR_RUNTIME_CUSTOM_BUFFER_TYPE_H */
Base class for describing a buffer's type.
Definition: buffer_type.h:30
virtual buffer_sptr make_buffer(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult, block_sptr link=block_sptr(), block_sptr buf_owner=block_sptr()) const
Make and return a buffer subclass of the corresponding type.
Definition: buffer_type.h:68
const std::string d_name
Definition: buffer_type.h:82
bool operator>=(const buffer_type_base &other)=delete
bool operator<=(const buffer_type_base &other)=delete
bool operator!=(const buffer_type_base &other) const
Definition: buffer_type.h:49
buffer_type_base(const std::string name)
Definition: buffer_type.h:84
const std::string & name() const
Get the human-readable name of the type.
Definition: buffer_type.h:62
bool operator<(const buffer_type_base &other)=delete
buffer_type_base(buffer_type_base const &other)
Definition: buffer_type.h:39
virtual ~buffer_type_base()
Definition: buffer_type.h:32
bool operator>(const buffer_type_base &other)=delete
void operator=(buffer_type_base const &)=delete
bool operator==(const buffer_type_base &other) const
Definition: buffer_type.h:44
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29
const buffer_type_base & buffer_type
Definition: buffer_type.h:87
std::vector< std::reference_wrapper< const buffer_type_base > > gr_vector_buffer_type
Definition: buffer_type.h:88
GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult, block_sptr link=block_sptr(), block_sptr buf_owner=block_sptr())
Allocate a buffer that holds at least nitems of size sizeof_item.
Template used to create buffer types. Note that the factory_class parameter must contain a static fun...
Definition: buffer_type.h:96
buffer_sptr make_buffer(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult, block_sptr link=block_sptr(), block_sptr buf_owner=block_sptr()) const override
Make and return a buffer subclass of the corresponding type.
Definition: buffer_type.h:99
buftype()
Definition: buffer_type.h:114
factory_class factory
Definition: buffer_type.h:98