File: ReferenceCounted.h

package info (click to toggle)
thepeg 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 9,312 kB
  • ctags: 11,509
  • sloc: cpp: 57,129; sh: 11,315; java: 3,212; lisp: 1,402; makefile: 830; ansic: 58; perl: 3
file content (126 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// -*- C++ -*-
//
// ReferenceCounted.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef ThePEG_ReferenceCounted_H
#define ThePEG_ReferenceCounted_H
// This is the declaration of the ReferenceCounted class.


#include "RCPtr.fh"
#include "ThePEG/Persistency/PersistentIStream.fh"

namespace ThePEG {
namespace Pointer {

/**
 * ReferenceCounted must be the (virtual) base class of all
 * classes which may be pointed to by the RCPtr smart
 * pointer class. It keeps track of all RCPtr and
 * ConstRCPtr pointers which are currently pointing to an
 * object.
 *
 * @see RCPtr
 * @see ConstRCPtr
 */
class ReferenceCounted {

  /** The RCPtrBase class needs to acces the private parts of ReferenceCounted. */
  friend class RCPtrBase;
  friend class ThePEG::PersistentIStream;

public:

  /**
   * The integer type used for counting.
   */
  typedef unsigned int CounterType;

protected:

  /** @name Standard constructors and assignment. */
  //@{
  /**
   * Default constructor.
   */
  ReferenceCounted() 
    : uniqueId(++objectCounter), 
      theReferenceCounter(CounterType(1)) {}

  /**
   * Copy-constructor.
   */
  ReferenceCounted(const ReferenceCounted &)
    : uniqueId(++objectCounter), 
      theReferenceCounter(CounterType(1)) {}

  /**
   * Assignment.
   */
  ReferenceCounted & operator=(const ReferenceCounted &)
  {
    return *this;
  }
    
  //@}

public:

  /**
   * Return the reference count.
   */
  CounterType referenceCount() const 
  { 
    return theReferenceCounter; 
  }

private:

  /**
   * Increment the reference count.
   */
  void incrementReferenceCount() const 
  { 
    ++theReferenceCounter; 
  }

  /**
   * Decrement with the reference count.
   */
  bool decrementReferenceCount() const 
  {
    return !--theReferenceCounter;
  }

public:

  /**
   * The unique ID. Can be used as sorting criterion, e.g. for set<> and map<>.
   */
  const unsigned long uniqueId;

private:

  /** 
   * A counter for issuing unique IDs. It will overflow back to 0 eventually,
   * but it is very unlikely that two identical IDs show up in the same event.
   */
  static unsigned long objectCounter;

  /**
   * The reference count.
   */
  mutable CounterType theReferenceCounter;

};


}
}

#endif /* ThePEG_ReferenceCounted_H */