]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGReferenced.hxx
Merge branch 'maint' into next
[simgear.git] / simgear / structure / SGReferenced.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2005-2006 Mathias Froehlich 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  */
20
21 #ifndef SGReferenced_HXX
22 #define SGReferenced_HXX
23 #define USE_OPENTHREADS_ATOMIC
24
25 #ifndef USE_OPENTHREADS_ATOMIC
26 #include "SGAtomic.hxx"
27 #else
28 #include <OpenThreads/Atomic>
29 #endif
30
31 /// Base class for all reference counted SimGear objects
32 /// Classes derived from this one are meant to be managed with
33 /// the SGSharedPtr class.
34 /// For more info see @SGSharedPtr.
35
36 class SGReferenced {
37 public:
38   SGReferenced(void) : _refcount(0u)
39   {}
40   /// Do not copy reference counts. Each new object has it's own counter
41   SGReferenced(const SGReferenced&) : _refcount(0u)
42   {}
43   /// Do not copy reference counts. Each object has it's own counter
44   SGReferenced& operator=(const SGReferenced&)
45   { return *this; }
46
47   static unsigned get(const SGReferenced* ref)
48   { if (ref) return ++(ref->_refcount); else return ~0u; }
49   static unsigned put(const SGReferenced* ref)
50   { if (ref) return --(ref->_refcount); else return ~0u; }
51   static unsigned count(const SGReferenced* ref)
52   { if (ref) return ref->_refcount; else return ~0u; }
53   static bool shared(const SGReferenced* ref)
54   { if (ref) return 1u < ref->_refcount; else return false; }
55
56 private:
57 #ifndef USE_OPENTHREADS_ATOMIC
58   mutable SGAtomic _refcount;
59 #else
60   mutable OpenThreads::Atomic _refcount;
61 #endif
62 };
63
64 #endif