Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Handle_for.h
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // Copyright (c) 1999 The CGAL Consortium
4 
5 // This software and related documentation is part of the Computational
6 // Geometry Algorithms Library (CGAL).
7 // This software and documentation is provided "as-is" and without warranty
8 // of any kind. In no event shall the CGAL Consortium be liable for any
9 // damage of any kind.
10 //
11 // Every use of CGAL requires a license.
12 //
13 // Academic research and teaching license
14 // - For academic research and teaching purposes, permission to use and copy
15 // the software and its documentation is hereby granted free of charge,
16 // provided that it is not a component of a commercial product, and this
17 // notice appears in all copies of the software and related documentation.
18 //
19 // Commercial licenses
20 // - A commercial license is available through Algorithmic Solutions, who also
21 // markets LEDA (http://www.algorithmic-solutions.de).
22 // - Commercial users may apply for an evaluation license by writing to
23 // Algorithmic Solutions (contact@algorithmic-solutions.com).
24 //
25 // The CGAL Consortium consists of Utrecht University (The Netherlands),
26 // ETH Zurich (Switzerland), Free University of Berlin (Germany),
27 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
28 // (Germany), Max-Planck-Institute Saarbrucken (Germany), RISC Linz (Austria),
29 // and Tel-Aviv University (Israel).
30 //
31 // ----------------------------------------------------------------------
32 //
33 // release : CGAL-2.2
34 // release_date : 2000, September 30
35 //
36 // source : Handle_for.fw
37 // file : include/CGAL/Handle_for.h
38 // package : Kernel_basic (3.14)
39 // revision : 3.14
40 // revision_date : 15 Sep 2000
41 // author(s) : Stefan Schirra
42 //
43 //
44 // coordinator : MPI, Saarbruecken (<Stefan.Schirra>)
45 // email : contact@cgal.org
46 // www : http://www.cgal.org
47 //
48 // ======================================================================
49 
50 
51 #ifndef CGAL_HANDLE_FOR_H
52 #define CGAL_HANDLE_FOR_H
53 #include <CGAL/memory.h>
54 
55 namespace CGAL {
56 
57 
58 template <class RefCounted, class Allocator> class Handle_for;
59 class Object;
60 
61 
63 {
64  public:
65  Ref_counted() : count(1) {}
66  Ref_counted(const Ref_counted&) : count(1) {}
67 
68  void add_reference() { ++count; }
69  void remove_reference() { --count; }
70  bool is_referenced() { return (count != 0); }
71  bool is_shared() { return (count > 1); }
72 
73  friend class Object;
74 
75  protected:
76  unsigned int count;
77 };
78 
79 
80 template <class RefCounted,
81  class Allocator = CGAL_ALLOCATOR(RefCounted) >
82 // RefCounted must provide
83 // add_reference()
84 // remove_reference()
85 // bool is_referenced()
86 // bool is_shared()
87 // and initialize count to 1 in default and copy constructor
88 class Handle_for
89 {
90  public:
91  Handle_for(const RefCounted& rc)
92  {
93  ptr = allocator.allocate(1);
94  allocator.construct(ptr, rc);
95  }
96 
98  {
99  ptr = allocator.allocate(1);
100  }
101 
103  {
104  ptr = h.ptr;
105  ptr->add_reference();
106  }
107 
109  {
110  ptr->remove_reference();
111  if ( !ptr->is_referenced() )
112  {
113  allocator.destroy( ptr);
114  allocator.deallocate( ptr, 1);
115  }
116  }
117 
118  Handle_for&
120  {
121  h.ptr->add_reference();
122  ptr->remove_reference();
123  if ( !ptr->is_referenced() )
124  {
125  allocator.destroy( ptr);
126  allocator.deallocate( ptr, 1);
127  }
128  ptr = h.ptr;
129  return *this;
130  }
131 
132  void
133  initialize_with( const RefCounted& rc)
134  {
135  allocator.construct(ptr, rc);
136  }
137 
138  void
140  {
141  if ( ptr->is_shared() )
142  {
143  RefCounted* tmp_ptr = allocator.allocate(1);
144  allocator.construct( tmp_ptr, *ptr);
145  ptr->remove_reference();
146  ptr = tmp_ptr;
147  }
148  }
149 
150  bool
151  identical( const Handle_for& h) const
152  { return ptr == h.ptr; }
153 
154  long int
155  id() const
156  { return reinterpret_cast<long int>( &(*ptr)); }
157 
158  protected:
159  static Allocator allocator;
160  typename Allocator::pointer ptr;
161 };
162 
163 
164 template <class RefCounted, class Allocator>
166 
167 } // namespace CGAL
168 #endif // CGAL_HANDLE_FOR_H
Allocator::pointer ptr
Definition: Handle_for.h:160
unsigned int count
Definition: Handle_for.h:76
Handle_for(const RefCounted &rc)
Definition: Handle_for.h:91
bool is_referenced()
Definition: Handle_for.h:70
Handle_for(const Handle_for &h)
Definition: Handle_for.h:102
long int id() const
Definition: Handle_for.h:155
#define CGAL_ALLOCATOR(t)
Definition: memory.h:59
Handle_for & operator=(const Handle_for &h)
Definition: Handle_for.h:119
void initialize_with(const RefCounted &rc)
Definition: Handle_for.h:133
Ref_counted(const Ref_counted &)
Definition: Handle_for.h:66
static Allocator allocator
Definition: Handle_for.h:159
bool identical(const Handle_for &h) const
Definition: Handle_for.h:151
void copy_on_write()
Definition: Handle_for.h:139
void remove_reference()
Definition: Handle_for.h:69
void add_reference()
Definition: Handle_for.h:68