RAUL  0.5.1
SharedPtr.hpp
1 /* A Reference Counting Smart Pointer.
2  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
3  *
4  * This is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This file is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_SHARED_PTR_HPP
19 #define RAUL_SHARED_PTR_HPP
20 
21 #include <cassert>
22 #include <cstddef>
23 
24 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
25 #include <iostream>
26 #include <list>
27 #include <algorithm>
28 
29 static std::list<void*> shared_ptr_counters;
30 
31 // Use debug hooks to ensure 2 shared_ptrs never point to the same thing
32 namespace boost {
33 
34  inline void sp_scalar_constructor_hook(void* object, unsigned long cnt, void* ptr) {
35  assert(std::find(shared_ptr_counters.begin(), shared_ptr_counters.end(),
36  (void*)object) == shared_ptr_counters.end());
37  shared_ptr_counters.push_back(object);
38  //std::cerr << "Creating SharedPtr to "
39  // << object << ", count = " << cnt << std::endl;
40  }
41 
42  inline void sp_scalar_destructor_hook(void* object, unsigned long cnt, void* ptr) {
43  shared_ptr_counters.remove(object);
44  //std::cerr << "Destroying SharedPtr to "
45  // << object << ", count = " << cnt << std::endl;
46  }
47 
48 }
49 #endif // BOOST_SP_ENABLE_DEBUG_HOOKS
50 
51 
52 #include <boost/shared_ptr.hpp>
53 
54 #ifdef BOOST_AC_USE_PTHREADS
55 #error "Boost is using mutex locking for pointer reference counting."
56 #error "This is VERY slow. Please report your platform to dave@drobilla.net"
57 #endif
58 
59 #define SharedPtr boost::shared_ptr
60 #define PtrCast boost::dynamic_pointer_cast
61 
62 #endif // RAUL_SHARED_PTR_HPP
63