Ice 3.7 C++11 API Reference
Shared.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_SHARED_H
6 #define ICE_UTIL_SHARED_H
7 
8 #include <IceUtil/Config.h>
9 #include <IceUtil/Atomic.h>
10 
11 //
12 // Base classes for reference counted types. The IceUtil::Handle
13 // template can be used for smart pointers to types derived from these
14 // bases.
15 //
16 // IceUtil::SimpleShared
17 // =====================
18 //
19 // A non thread-safe base class for reference-counted types.
20 //
21 // IceUtil::Shared
22 // ===============
23 //
24 // A thread-safe base class for reference-counted types.
25 //
26 namespace IceUtil
27 {
28 
30 {
31 public:
32 
35 
36  virtual ~SimpleShared();
37 
39  {
40  return *this;
41  }
42 
43  void __incRef()
44  {
45  assert(_ref >= 0);
46  ++_ref;
47  }
48 
49  void __decRef()
50  {
51  assert(_ref > 0);
52  if(--_ref == 0)
53  {
54  if(!_noDelete)
55  {
56  delete this;
57  }
58  }
59  }
60 
61  int __getRef() const
62  {
63  return _ref;
64  }
65 
66  void __setNoDelete(bool b)
67  {
68  _noDelete = b;
69  }
70 
71 private:
72 
73  int _ref;
74  bool _noDelete;
75 };
76 
78 {
79 public:
80 
81  //
82  // Flag constant used by the Shared class. Derived classes
83  // such as GCObject define more flag constants.
84  //
85  static const unsigned char NoDelete;
86 
87  Shared();
88  Shared(const Shared&);
89 
90  virtual ~Shared()
91  {
92  }
93 
95  {
96  return *this;
97  }
98 
99  virtual void __incRef();
100  virtual void __decRef();
101  virtual int __getRef() const;
102  virtual void __setNoDelete(bool);
103 
104  void __setFlag(unsigned char flag)
105  {
106  _flags |= flag;
107  }
108 
109  void __clearFlag(unsigned char flag)
110  {
111  _flags &= ~flag;
112  }
113 
114  bool __hasFlag(unsigned char flag)
115  {
116  return (_flags & flag) > 0;
117  }
118 
119 protected:
120 
121  IceUtilInternal::Atomic _ref;
122  unsigned char _flags;
123 };
124 
125 }
126 
127 #endif
Atomic.h
IceUtil::Shared::__decRef
virtual void __decRef()
IceUtil::SimpleShared::__incRef
void __incRef()
Definition: Shared.h:43
IceUtil::SimpleShared::SimpleShared
SimpleShared(const SimpleShared &)
IceUtil::SimpleShared::__decRef
void __decRef()
Definition: Shared.h:49
IceUtil::Shared::_flags
unsigned char _flags
Definition: Shared.h:122
IceUtil::Shared::__setNoDelete
virtual void __setNoDelete(bool)
IceUtil::SimpleShared::operator=
SimpleShared & operator=(const SimpleShared &)
Definition: Shared.h:38
IceUtil::SimpleShared::__setNoDelete
void __setNoDelete(bool b)
Definition: Shared.h:66
IceUtil::Shared::operator=
Shared & operator=(const Shared &)
Definition: Shared.h:94
IceUtil::Shared::Shared
Shared()
IceUtil::Shared::__hasFlag
bool __hasFlag(unsigned char flag)
Definition: Shared.h:114
IceUtil
Definition: Optional.h:1095
ICE_API
#define ICE_API
Definition: Config.h:197
IceUtil::Shared::_ref
IceUtilInternal::Atomic _ref
Definition: Shared.h:121
IceUtil::Shared::__getRef
virtual int __getRef() const
IceUtil::SimpleShared
Definition: Shared.h:30
IceUtil::Shared::~Shared
virtual ~Shared()
Definition: Shared.h:90
Config.h
IceUtil::Shared::__clearFlag
void __clearFlag(unsigned char flag)
Definition: Shared.h:109
IceUtil::Shared::__incRef
virtual void __incRef()
IceUtil::Shared::__setFlag
void __setFlag(unsigned char flag)
Definition: Shared.h:104
IceUtil::Shared::Shared
Shared(const Shared &)
IceUtil::SimpleShared::SimpleShared
SimpleShared()
IceUtil::Shared::NoDelete
static const unsigned char NoDelete
Definition: Shared.h:85
IceUtil::SimpleShared::~SimpleShared
virtual ~SimpleShared()
IceUtil::SimpleShared::__getRef
int __getRef() const
Definition: Shared.h:61
IceUtil::Shared
Definition: Shared.h:78