Referenceable.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Referenceable.h This file contains the declaration of class Referenceable.
4 //
5 // Copyright (C) 2010, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef REFERENCEABLE_H
25 #define REFERENCEABLE_H
26 
27 #ifndef USE_PCH
28 #include <glib.h>
29 #endif
30 
31 namespace util {
32 
34  private:
35  mutable volatile gint M_ref_count;
36  protected:
37  Referenceable(void) : M_ref_count(1) { }
38  virtual ~Referenceable() { }
39  public:
40  void reference(void) const { g_atomic_int_inc(&M_ref_count); }
41  void unreference(void) const { if (g_atomic_int_dec_and_test(&M_ref_count)) delete const_cast<Referenceable*>(this); }
42 };
43 
44 } // namespace util
45 
46 #endif // REFERENCEABLE_H

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.