1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef KML_BASE_REFERENT_H__
27 #define KML_BASE_REFERENT_H__
28 
29 // This file contains the implementation of the Referent class which holds
30 // the reference counter used by boost::intrusive_ptr.  The Referent class
31 // is a base class of all KML DOM Elements and also the base TempFile
32 // class.  Neither the Referent class nor the methods here are part of the
33 // libkml public API.
34 
35 namespace kmlbase {
36 
37 // This class implements the reference count used by boost::intrusive_ptr.
38 class Referent {
39  public:
40   // The constructor only constructs the Referent object.  The reference
41   // count is incremented if and when the Referent-derived object is assigned
42   // to a boost::intrusive_ptr.
Referent()43   Referent() : ref_count_(0) {}
~Referent()44   virtual ~Referent() {}
45 
46   // This method is used by intrusive_ptr_add_ref() to increment the reference
47   // count of a given Referent-derived object.
add_ref()48   void add_ref() {
49     ++ref_count_;
50   }
51 
52   // This method is used by intrusive_ptr_release() to decrement the reference
53   // count of a given Referent-derived object.
release()54   int release() {
55     return --ref_count_;
56   }
57 
58   // This is for debugging purposes only.
get_ref_count()59   int get_ref_count() const {
60     return ref_count_;
61   }
62 
63  private:
64   int ref_count_;
65 };
66 
67 // These declarations are for the implementation of the functions used within
68 // boost::intrusive_ptr to manage Referent-derived objects..  See referent.cc
69 // and boost/intrusive_ptr.hpp.
70 void intrusive_ptr_add_ref(kmlbase::Referent* r);
71 void intrusive_ptr_release(kmlbase::Referent* r);
72 
73 } // end namespace kmlbase
74 
75 #endif  // KML_BASE_REFERENT_H__
76