blob: 6800ea1db7094304f94f0c3abcbbc006ea8ac3e7 (
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
|
/* -*- c++ -*- */
/*
* Copyright 2008,2013 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* The magic here is a way of constructing std::shared_ptr<> to blocks, in a way
* that allows the constructors of those blocks to reference them.
* This is only used for blocks that derive from hier_blocks2, so that they can
* create and connect sub-blocks in their constructor.
*/
#ifndef INCLUDED_GR_RUNTIME_SPTR_MAGIC_H
#define INCLUDED_GR_RUNTIME_SPTR_MAGIC_H
#include <gnuradio/api.h>
#include <memory>
namespace gr {
class basic_block;
class hier_block2;
} // namespace gr
namespace gnuradio {
namespace detail {
class GR_RUNTIME_API sptr_magic
{
public:
static std::shared_ptr<gr::basic_block> fetch_initial_sptr(gr::basic_block* p);
static void create_and_stash_initial_sptr(gr::hier_block2* p);
static void cancel_initial_sptr(gr::hier_block2* p);
};
} // namespace detail
/*
* \brief New! Improved! Standard method to get/create the
* std::shared_ptr for a block.
*/
template <class T>
std::shared_ptr<T> get_initial_sptr(T* p)
{
return std::dynamic_pointer_cast<T, gr::basic_block>(
detail::sptr_magic::fetch_initial_sptr(p));
}
/*
* \brief GNU Radio version of std::make_shared<> that also provides magic. For
* blocks that don't reference self() in any constructor it's equivalent to
* std::make_shared<>.
*/
template <typename T, typename... Args>
std::shared_ptr<T> make_block_sptr(Args&&... args)
{
return get_initial_sptr(new T(std::forward<Args>(args)...));
}
} // namespace gnuradio
#endif /* INCLUDED_GR_RUNTIME_SPTR_MAGIC_H */
|