~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/intrusive_ptr_base.h

Merge in trunk to catalogs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 * Copyright (C) 2010 Brian Aker
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 *     * Redistributions of source code must retain the above copyright
 
10
 *       notice, this list of conditions and the following disclaimer.
 
11
 *     * Redistributions in binary form must reproduce the above copyright
 
12
 *       notice, this list of conditions and the following disclaimer in the
 
13
 *       documentation and/or other materials provided with the distribution.
 
14
 *     * Neither the name of the <organization> nor the
 
15
 *       names of its contributors may be used to endorse or promote products
 
16
 *       derived from this software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 *
 
29
 */
 
30
 
 
31
/*
 
32
  I've looked at a number of examples on the web, this is a composite of what I have seen/liked.
 
33
  -Brian
 
34
*/
 
35
 
 
36
#include <boost/checked_delete.hpp>
 
37
#include <boost/detail/atomic_count.hpp>
 
38
#include <boost/intrusive_ptr.hpp>
 
39
#include <boost/thread.hpp>
 
40
 
 
41
#ifndef DRIZZLED_UTIL_INTRUSIVE_BASE_PTR_H
 
42
#define DRIZZLED_UTIL_INTRUSIVE_BASE_PTR_H
 
43
 
 
44
namespace drizzled {
 
45
namespace util {
 
46
 
 
47
template<class T>
 
48
class intrusive_ptr_base
 
49
{
 
50
public:
 
51
  intrusive_ptr_base():
 
52
    _reference_count(0)
 
53
  {
 
54
  }
 
55
 
 
56
  intrusive_ptr_base(intrusive_ptr_base<T> const&) :
 
57
    _reference_count(0)
 
58
  {
 
59
  }
 
60
 
 
61
  intrusive_ptr_base& operator=(intrusive_ptr_base const& rhs)
 
62
  { 
 
63
    return *this; 
 
64
  }
 
65
 
 
66
  friend void intrusive_ptr_add_ref(intrusive_ptr_base<T> const* s)
 
67
  {
 
68
    assert(s->_reference_count >= 0);
 
69
    assert(s != 0);
 
70
    ++s->_reference_count;
 
71
  }
 
72
 
 
73
  friend void intrusive_ptr_release(intrusive_ptr_base<T> const* s)
 
74
  {
 
75
    assert(s->_reference_count > 0);
 
76
    assert(s != 0);
 
77
 
 
78
    if (--s->_reference_count == 0)
 
79
      boost::checked_delete(static_cast<T const*>(s));
 
80
  }
 
81
 
 
82
  boost::intrusive_ptr<T> self()
 
83
  { 
 
84
    return boost::intrusive_ptr<T>((T*)this); 
 
85
  }
 
86
 
 
87
  boost::intrusive_ptr<const T> self() const
 
88
  { 
 
89
    return boost::intrusive_ptr<const T>((T const*)this); 
 
90
  }
 
91
 
 
92
  bool unique() const 
 
93
  {
 
94
    return _reference_count == 0;
 
95
  }
 
96
 
 
97
  int use_count() const 
 
98
  { 
 
99
    return _reference_count; 
 
100
  }
 
101
 
 
102
private:
 
103
  mutable boost::detail::atomic_count _reference_count;
 
104
};
 
105
 
 
106
} // namespace util
 
107
} // namespace drizzled
 
108
 
 
109
#endif /* DRIZZLED_UTIL_INTRUSIVE_BASE_PTR_H */