Revision 395e1fa6

b/gnuradio-core/src/lib/general/gr_kludge_copy.cc
31 31
gr_kludge_copy_sptr
32 32
gr_make_kludge_copy(size_t itemsize)
33 33
{
34
  return gr_kludge_copy_sptr(new gr_kludge_copy(itemsize));
34
  return gnuradio::get_initial_sptr(new gr_kludge_copy(itemsize));
35 35
}
36 36

37 37
gr_kludge_copy::gr_kludge_copy(size_t itemsize)
b/gnuradio-core/src/lib/runtime/Makefile.am
50 50
	gr_realtime.cc				\
51 51
	gr_scheduler_thread.cc			\
52 52
	gr_single_threaded_scheduler.cc		\
53
	gr_sptr_magic.cc			\
53 54
	gr_sync_block.cc			\
54 55
	gr_sync_decimator.cc			\
55 56
	gr_sync_interpolator.cc			\
......
67 68
libruntime_qa_la_SOURCES = 			\
68 69
	qa_gr_block.cc				\
69 70
	qa_gr_hier_block2.cc			\
71
	qa_gr_hier_block2_derived.cc		\
70 72
	qa_gr_buffer.cc				\
71 73
	qa_gr_flowgraph.cc			\
72 74
	qa_gr_top_block.cc			\
......
98 100
	gr_scheduler_thread.h			\
99 101
	gr_select_handler.h			\
100 102
	gr_single_threaded_scheduler.h		\
103
	gr_sptr_magic.h				\
101 104
	gr_sync_block.h				\
102 105
	gr_sync_decimator.h			\
103 106
	gr_sync_interpolator.h			\
......
117 120
	qa_gr_block.h				\
118 121
	qa_gr_flowgraph.h			\
119 122
	qa_gr_hier_block2.h			\
123
	qa_gr_hier_block2_derived.h		\
120 124
	qa_gr_buffer.h				\
121 125
	qa_gr_io_signature.h			\
122 126
	qa_gr_top_block.h			\
b/gnuradio-core/src/lib/runtime/gr_basic_block.h
1 1
/* -*- c++ -*- */
2 2
/*
3
 * Copyright 2006 Free Software Foundation, Inc.
3
 * Copyright 2006,2008 Free Software Foundation, Inc.
4 4
 * 
5 5
 * This file is part of GNU Radio
6 6
 * 
......
24 24
#define INCLUDED_GR_BASIC_BLOCK_H
25 25

26 26
#include <gr_runtime_types.h>
27
#include <gr_sptr_magic.h>
27 28
#include <boost/enable_shared_from_this.hpp>
28 29
#include <string>
29 30

b/gnuradio-core/src/lib/runtime/gr_hier_block2.cc
1 1
/* -*- c++ -*- */
2 2
/*
3
 * Copyright 2006,2007 Free Software Foundation, Inc.
3
 * Copyright 2006,2007,2008 Free Software Foundation, Inc.
4 4
 * 
5 5
 * This file is part of GNU Radio
6 6
 * 
......
31 31

32 32
#define GR_HIER_BLOCK2_DEBUG 0
33 33

34
gr_hier_block2_sptr gr_make_hier_block2(const std::string &name, 
35
                                        gr_io_signature_sptr input_signature,
36
                                        gr_io_signature_sptr output_signature)
34

35
gr_hier_block2_sptr
36
gr_make_hier_block2(const std::string &name, 
37
                    gr_io_signature_sptr input_signature,
38
                    gr_io_signature_sptr output_signature)
37 39
{
38
  return gr_hier_block2_sptr(new gr_hier_block2(name, input_signature, output_signature));
40
  return gnuradio::get_initial_sptr(new gr_hier_block2(name, input_signature, output_signature));
39 41
}
40 42

41 43
gr_hier_block2::gr_hier_block2(const std::string &name,
......
44 46
  : gr_basic_block(name, input_signature, output_signature),
45 47
    d_detail(new gr_hier_block2_detail(this))
46 48
{
49
  // This bit of magic ensures that self() works in the constructors of derived classes.
50
  gnuradio::detail::sptr_magic::create_and_stash_initial_sptr(this);
47 51
}
48 52

49 53
gr_hier_block2::~gr_hier_block2()
......
51 55
  delete d_detail;
52 56
}
53 57

58
gr_hier_block2::opaque_self
59
gr_hier_block2::self()
60
{
61
  return shared_from_this();
62
}
63

64

54 65
void 
55 66
gr_hier_block2::connect(gr_basic_block_sptr block)
56 67
{
b/gnuradio-core/src/lib/runtime/gr_hier_block2.h
1 1
/* -*- c++ -*- */
2 2
/*
3
 * Copyright 2006,2007 Free Software Foundation, Inc.
3
 * Copyright 2006,2007,2008 Free Software Foundation, Inc.
4 4
 * 
5 5
 * This file is part of GNU Radio
6 6
 * 
......
51 51
   * \brief Private implementation details of gr_hier_block2
52 52
   */
53 53
  gr_hier_block2_detail *d_detail;
54
  
54
    
55 55
protected: 
56 56
  gr_hier_block2(const std::string &name,
57 57
		 gr_io_signature_sptr input_signature,
......
61 61
  virtual ~gr_hier_block2();
62 62
  
63 63
  /*!
64
   * \brief typedef for object returned from self().
65
   *
66
   * This type is only guaranteed to be passable to connect and disconnect.
67
   * No other assumptions should be made about it.
68
   */
69
  typedef gr_basic_block_sptr	opaque_self;
70

71
  /*!
72
   * \brief Return an object, representing the current block, which can be passed to connect.
73
   *
74
   * The returned object may only be used as an argument to connect or disconnect.
75
   * Any other use of self() results in unspecified (erroneous) behavior.
76
   */
77
  opaque_self self();
78

79
  /*!
64 80
   * \brief Add a stand-alone (possibly hierarchical) block to internal graph
65 81
   *
66 82
   * This adds a gr-block or hierarchical block to the internal graph
......
132 148
  gr_flat_flowgraph_sptr flatten() const;
133 149
};
134 150

135
inline gr_hier_block2_sptr make_hier_block2_sptr(gr_basic_block_sptr block) {
151
inline gr_hier_block2_sptr cast_to_hier_block2_sptr(gr_basic_block_sptr block) {
136 152
  return boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(block);
137 153
}
138 154

b/gnuradio-core/src/lib/runtime/gr_hier_block2_detail.cc
78 78
  if (src.get() == dst.get())
79 79
    throw std::invalid_argument("connect: src and destination blocks cannot be the same");
80 80

81
  gr_hier_block2_sptr src_block(make_hier_block2_sptr(src));
82
  gr_hier_block2_sptr dst_block(make_hier_block2_sptr(dst));
81
  gr_hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
82
  gr_hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));
83 83

84 84
  if (src_block && src.get() != d_owner) {
85 85
    if (GR_HIER_BLOCK2_DETAIL_DEBUG)
......
147 147
  if (src.get() == dst.get())
148 148
    throw std::invalid_argument("disconnect: source and destination blocks cannot be the same");
149 149

150
  gr_hier_block2_sptr src_block(make_hier_block2_sptr(src));
151
  gr_hier_block2_sptr dst_block(make_hier_block2_sptr(dst));
150
  gr_hier_block2_sptr src_block(cast_to_hier_block2_sptr(src));
151
  gr_hier_block2_sptr dst_block(cast_to_hier_block2_sptr(dst));
152 152

153 153
  if (src_block && src.get() != d_owner) {
154 154
    if (GR_HIER_BLOCK2_DETAIL_DEBUG)
......
307 307
    return endp;
308 308
  
309 309
  // Check if endpoint is a hierarchical block
310
  gr_hier_block2_sptr hier_block2(make_hier_block2_sptr(endp.block()));
310
  gr_hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(endp.block()));
311 311
  if (hier_block2) {
312 312
    if (GR_HIER_BLOCK2_DETAIL_DEBUG)
313 313
      std::cout << "Resolving endpoint " << endp << " as an " 
......
351 351

352 352
  // Recurse hierarchical children
353 353
  for (gr_basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
354
    gr_hier_block2_sptr hier_block2(make_hier_block2_sptr(*p));
354
    gr_hier_block2_sptr hier_block2(cast_to_hier_block2_sptr(*p));
355 355
    if (hier_block2)
356 356
      hier_block2->d_detail->flatten_aux(sfg);
357 357
  }
b/gnuradio-core/src/lib/runtime/gr_sptr_magic.cc
1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2008 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 along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
#ifdef HAVE_CONFIG_H
22
#include <config.h>
23
#endif
24
#include <gr_sptr_magic.h>
25
#include <gr_hier_block2.h>
26
#include <map>
27
#include <stdexcept>
28

29

30
#if 0
31
  #include <boost/thread.hpp>
32
  typedef boost::mutex			mutex;
33
  typedef boost::mutex::scoped_lock	scoped_lock;
34
#else
35
  #include <omnithread.h>
36
  typedef omni_mutex			mutex;
37
  typedef omni_mutex_lock		scoped_lock;
38
#endif
39

40
namespace gnuradio {
41

42
  static mutex		s_mutex;
43
  typedef std::map<gr_basic_block*, gr_basic_block_sptr> sptr_map;
44
  static sptr_map	s_map;
45

46
  void
47
  detail::sptr_magic::create_and_stash_initial_sptr(gr_hier_block2 *p)
48
  {
49
    gr_basic_block_sptr sptr(p);
50
    scoped_lock	l();
51
    s_map.insert(sptr_map::value_type(static_cast<gr_basic_block *>(p), sptr));
52
  }
53

54

55
  gr_basic_block_sptr 
56
  detail::sptr_magic::fetch_initial_sptr(gr_basic_block *p)
57
  {
58
    /*
59
     * If p isn't a subclass of gr_hier_block2, just create the
60
     * shared ptr and return it.
61
     */
62
    gr_hier_block2 *hb2 = dynamic_cast<gr_hier_block2 *>(p);
63
    if (!hb2){
64
      return gr_basic_block_sptr(p);
65
    }
66

67
    /*
68
     * p is a subclass of gr_hier_block2, thus we've already created the shared pointer
69
     * and stashed it away.  Fish it out and return it.
70
     */
71
    scoped_lock	l();
72
    sptr_map::iterator pos = s_map.find(static_cast<gr_basic_block *>(p));
73
    if (pos == s_map.end())
74
      throw std::invalid_argument("gr_sptr_magic: invalid pointer!");
75

76
    gr_basic_block_sptr sptr = pos->second;
77
    s_map.erase(pos);
78
    return sptr;
79
  }
80
};
81

b/gnuradio-core/src/lib/runtime/gr_sptr_magic.h
1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2008 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 along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
#ifndef INCLUDED_GR_SPTR_MAGIC_H
22
#define INCLUDED_GR_SPTR_MAGIC_H
23

24
#include <boost/shared_ptr.hpp>
25

26
class gr_basic_block;
27
class gr_hier_block2;
28

29
namespace gnuradio {
30

31
  namespace detail {
32
    
33
    class sptr_magic {
34
    public:
35
      static boost::shared_ptr<gr_basic_block> fetch_initial_sptr(gr_basic_block *p);
36
      static void create_and_stash_initial_sptr(gr_hier_block2 *p);
37
    };
38
  };
39

40
  /* 
41
   * \brief New!  Improved!  Standard method to get/create the boost::shared_ptr for a block.
42
   */
43
  template<class T>
44
  boost::shared_ptr<T>
45
  get_initial_sptr(T *p)
46
  {
47
    return boost::dynamic_pointer_cast<T, gr_basic_block>(detail::sptr_magic::fetch_initial_sptr(p));
48
  }
49
};
50

51
#endif /* INCLUDED_GR_SPTR_MAGIC_H */
b/gnuradio-core/src/lib/runtime/gr_top_block.cc
34 34
gr_top_block_sptr 
35 35
gr_make_top_block(const std::string &name)
36 36
{
37
  return gr_top_block_sptr(new gr_top_block(name));
37
  return gnuradio::get_initial_sptr(new gr_top_block(name));
38 38
}
39 39

40 40
gr_top_block::gr_top_block(const std::string &name)
b/gnuradio-core/src/lib/runtime/qa_gr_hier_block2.cc
1 1
/* -*- c++ -*- */
2 2
/*
3
 * Copyright 2006 Free Software Foundation, Inc.
3
 * Copyright 2006,2008 Free Software Foundation, Inc.
4 4
 * 
5 5
 * This file is part of GNU Radio
6 6
 * 
......
33 33
void qa_gr_hier_block2::test_make()
34 34
{
35 35
    gr_hier_block2_sptr src1(gr_make_hier_block2("test",
36
						 gr_make_io_signature(1, 1, sizeof(int)),
37
						 gr_make_io_signature(1, 1, sizeof(int))));
36
						 gr_make_io_signature(1, 2, 1 * sizeof(int)),
37
						 gr_make_io_signature(3, 4, 2 * sizeof(int))));
38 38

39 39
    CPPUNIT_ASSERT(src1);
40 40
    CPPUNIT_ASSERT_EQUAL(std::string("test"), src1->name());
41
    CPPUNIT_ASSERT_EQUAL(1, src1->input_signature()->max_streams());
42
    CPPUNIT_ASSERT_EQUAL(1, src1->output_signature()->min_streams());
43
    CPPUNIT_ASSERT_EQUAL(1, src1->output_signature()->max_streams());
44
    CPPUNIT_ASSERT_EQUAL((int) sizeof(int), 
41

42
    CPPUNIT_ASSERT_EQUAL(1 * (int) sizeof(int), 
43
			 src1->input_signature()->sizeof_stream_item(0));
44

45
    CPPUNIT_ASSERT_EQUAL(1, src1->input_signature()->min_streams());
46
    CPPUNIT_ASSERT_EQUAL(2, src1->input_signature()->max_streams());
47

48

49
    CPPUNIT_ASSERT_EQUAL(2 * (int) sizeof(int), 
45 50
			 src1->output_signature()->sizeof_stream_item(0));
51

52
    CPPUNIT_ASSERT_EQUAL(3, src1->output_signature()->min_streams());
53
    CPPUNIT_ASSERT_EQUAL(4, src1->output_signature()->max_streams());
54

46 55
}
56

57

b/gnuradio-core/src/lib/runtime/qa_gr_hier_block2.h
32 32
    CPPUNIT_TEST_SUITE(qa_gr_hier_block2);
33 33

34 34
    CPPUNIT_TEST(test_make);
35
    
35
        
36 36
    CPPUNIT_TEST_SUITE_END();
37 37

38 38
private:
39 39
    void test_make();
40
    void test_derived();
41 40
};
42 41

43 42
#endif /* INCLUDED_QA_GR_HIER_BLOCK2_H */
b/gnuradio-core/src/lib/runtime/qa_gr_hier_block2_derived.cc
1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2006,2008 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
#ifdef HAVE_CONFIG_H
24
#include <config.h>
25
#endif
26

27
#include <qa_gr_hier_block2_derived.h>
28
#include <gr_top_block.h>
29
#include <gr_io_signature.h>
30
#include <gr_null_source.h>
31
#include <gr_null_sink.h>
32
#include <gr_head.h>
33
#include <gr_kludge_copy.h>
34

35
// Declare a test C++ hierarchical block
36

37
class gr_derived_block;
38
typedef boost::shared_ptr<gr_derived_block> gr_derived_block_sptr;
39
gr_derived_block_sptr gr_make_derived_block();
40

41
class gr_derived_block : public gr_hier_block2
42
{
43
private:
44
  friend gr_derived_block_sptr gr_make_derived_block();
45
  gr_derived_block();
46

47
public:
48
  ~gr_derived_block();
49
};
50

51

52
gr_derived_block_sptr
53
gr_make_derived_block()
54
{
55
  return gnuradio::get_initial_sptr(new gr_derived_block());
56
}
57

58
gr_derived_block::gr_derived_block()
59
  : gr_hier_block2("gr_derived_block",
60
		   gr_make_io_signature(1, 1, sizeof(int)), // Input signature
61
		   gr_make_io_signature(1, 1, sizeof(int))) // Output signature
62
{
63
  gr_block_sptr copy(gr_make_kludge_copy(sizeof(int)));
64

65
  connect(self(), 0, copy, 0);
66
  connect(copy, 0, self(), 0);
67
}
68

69
gr_derived_block::~gr_derived_block()
70
{
71
}
72

73
void qa_gr_hier_block2_derived::test_1()
74
{
75
  gr_top_block_sptr     tb(gr_make_top_block("test"));
76

77
  gr_block_sptr         src(gr_make_null_source(sizeof(int)));
78
  gr_block_sptr         head(gr_make_head(sizeof(int), 1000));
79
  gr_derived_block_sptr blk(gr_make_derived_block());
80
  gr_block_sptr         dst(gr_make_null_sink(sizeof(int)));
81
  
82
  tb->connect(src,  0, head, 0);
83
  tb->connect(head, 0, blk,  0);
84
  tb->connect(blk,  0, dst,  0);
85

86
  tb->run();
87
}
b/gnuradio-core/src/lib/runtime/qa_gr_hier_block2_derived.h
1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2006,2008 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
#ifndef INCLUDED_QA_GR_HIER_BLOCK2_DERIVED_H
24
#define INCLUDED_QA_GR_HIER_BLOCK2_DERIVED_H
25

26
#include <cppunit/extensions/HelperMacros.h>
27
#include <cppunit/TestCase.h>
28
#include <stdexcept>
29

30
// Declare a QA test case
31
class qa_gr_hier_block2_derived : public CppUnit::TestCase 
32
{
33
  CPPUNIT_TEST_SUITE(qa_gr_hier_block2_derived);
34
  CPPUNIT_TEST(test_1);
35
  CPPUNIT_TEST_SUITE_END();
36

37
private:
38
  void test_1();
39
};
40

41
#endif /* INCLUDED_QA_GR_HIER_BLOCK2_DERIVED_H */
b/gnuradio-core/src/lib/runtime/qa_runtime.cc
36 36
#include <qa_gr_flowgraph.h>
37 37
#include <qa_gr_top_block.h>
38 38
#include <qa_gr_hier_block2.h>
39
#include <qa_gr_hier_block2_derived.h>
39 40
#include <qa_gr_buffer.h>
40 41

41 42
CppUnit::TestSuite *
......
49 50
  s->addTest (qa_gr_flowgraph::suite ());
50 51
  s->addTest (qa_gr_top_block::suite ());
51 52
  s->addTest (qa_gr_hier_block2::suite ());
53
  s->addTest (qa_gr_hier_block2_derived::suite ());
52 54
  s->addTest (qa_gr_buffer::suite ());
53 55
  
54 56
  return s;

Also available in: Unified diff