GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
sptr_magic.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2013 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  * The magic here is a way of constructing std::shared_ptr<> to blocks, in a way
10  * that allows the constructors of those blocks to reference them.
11  * This is only used for blocks that derive from hier_blocks2, so that they can
12  * create and connect sub-blocks in their constructor.
13  */
14 
15 #ifndef INCLUDED_GR_RUNTIME_SPTR_MAGIC_H
16 #define INCLUDED_GR_RUNTIME_SPTR_MAGIC_H
17 
18 #include <gnuradio/api.h>
19 #include <memory>
20 
21 namespace gr {
22 class basic_block;
23 class hier_block2;
24 } // namespace gr
25 
26 namespace gnuradio {
27 namespace detail {
28 
30 {
31 public:
32  static std::shared_ptr<gr::basic_block> fetch_initial_sptr(gr::basic_block* p);
33  static void create_and_stash_initial_sptr(gr::hier_block2* p);
34  static void cancel_initial_sptr(gr::hier_block2* p);
35 };
36 } // namespace detail
37 
38 /*
39  * \brief New! Improved! Standard method to get/create the
40  * std::shared_ptr for a block.
41  */
42 template <class T>
43 std::shared_ptr<T> get_initial_sptr(T* p)
44 {
45  return std::dynamic_pointer_cast<T, gr::basic_block>(
46  detail::sptr_magic::fetch_initial_sptr(p));
47 }
48 
49 /*
50  * \brief GNU Radio version of std::make_shared<> that also provides magic. For
51  * blocks that don't reference self() in any constructor it's equivalent to
52  * std::make_shared<>.
53  */
54 template <typename T, typename... Args>
55 std::shared_ptr<T> make_block_sptr(Args&&... args)
56 {
57  return get_initial_sptr(new T(std::forward<Args>(args)...));
58 }
59 } // namespace gnuradio
60 
61 #endif /* INCLUDED_GR_RUNTIME_SPTR_MAGIC_H */
Definition: sptr_magic.h:30
The abstract base class for all signal processing blocks.
Definition: basic_block.h:63
Hierarchical container class for gr::block's and gr::hier_block2's.
Definition: hier_block2.h:34
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
Definition: sptr_magic.h:26
GNU Radio logging wrapper.
Definition: basic_block.h:29