root / gnuradio-core / src / lib / runtime / gr_sptr_magic.cc @ 4cb301de
History | View | Annotate | Download (2.1 kB)
| 1 | /* -*- c++ -*- */
|
|---|---|
| 2 | /*
|
| 3 | * Copyright 2008,2009 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 | #include <gruel/thread.h> |
| 31 | |
| 32 | namespace gnuradio {
|
| 33 | |
| 34 | static gruel::mutex s_mutex;
|
| 35 | typedef std::map<gr_basic_block*, gr_basic_block_sptr> sptr_map;
|
| 36 | static sptr_map s_map;
|
| 37 | |
| 38 | void
|
| 39 | detail::sptr_magic::create_and_stash_initial_sptr(gr_hier_block2 *p) |
| 40 | {
|
| 41 | gr_basic_block_sptr sptr(p); |
| 42 | gruel::scoped_lock guard(s_mutex); |
| 43 | s_map.insert(sptr_map::value_type(static_cast<gr_basic_block *>(p), sptr));
|
| 44 | } |
| 45 | |
| 46 | |
| 47 | gr_basic_block_sptr |
| 48 | detail::sptr_magic::fetch_initial_sptr(gr_basic_block *p) |
| 49 | {
|
| 50 | /*
|
| 51 | * If p isn't a subclass of gr_hier_block2, just create the |
| 52 | * shared ptr and return it. |
| 53 | */ |
| 54 | gr_hier_block2 *hb2 = dynamic_cast<gr_hier_block2 *>(p);
|
| 55 | if (!hb2){
|
| 56 | return gr_basic_block_sptr(p);
|
| 57 | } |
| 58 | |
| 59 | /*
|
| 60 | * p is a subclass of gr_hier_block2, thus we've already created the shared pointer |
| 61 | * and stashed it away. Fish it out and return it. |
| 62 | */ |
| 63 | gruel::scoped_lock guard(s_mutex); |
| 64 | sptr_map::iterator pos = s_map.find(static_cast<gr_basic_block *>(p));
|
| 65 | if (pos == s_map.end())
|
| 66 | throw std::invalid_argument("gr_sptr_magic: invalid pointer!"); |
| 67 | |
| 68 | gr_basic_block_sptr sptr = pos->second; |
| 69 | s_map.erase(pos); |
| 70 | return sptr;
|
| 71 | } |
| 72 | }; |