Teamcenter C++ API Reference  2312
SharedPtrCountedBase.hxx
Go to the documentation of this file.
1 // Copyright 2022 Siemens Digital Industries Software
2 // ==================================================
3 // Copyright 2013.
4 // Siemens Product Lifecycle Management Software Inc.
5 // All Rights Reserved.
6 // ==================================================
7 // Copyright 2022 Siemens Digital Industries Software
8 
17 /*
18  Background:
19  Base implementation copied from NX. The code was copied as this class was available in
20  latest NX libraries which are currently not supported in Tc.
21  The implementation in NX uses code from Boost library, hence a reference to Boost license in included.
22 
23  Changes were made to NX code to conform it to Tc. These changes include :
24  - namespace change
25  - using default heap instead of SM.
26  - remove support for custom allocator. This was NX specific extension, not present in std::tr1
27  - cosmetic changes like separating class declaration and definition for clarity.
28 */
29 
30 /* Boost license: as base implementation comes for Boost, including its license.
31 
32 Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
33 Copyright 2004-2005 Peter Dimov
34 
35 Distributed under the Boost Software License, Version 1.0.
36 (See copy at http://www.boost.org/LICENSE_1_0.txt)
37 */
38 
39 #ifndef TEAMCENTER_BASE_UTILS_SHARED_PTR_COUNTED_BASE_HXX
40 #define TEAMCENTER_BASE_UTILS_SHARED_PTR_COUNTED_BASE_HXX
41 
42 namespace Teamcenter
43 {
44 namespace Internal
45 {
47  {
48 
49  public:
50  inline SharedPtrCountedBase();
51  inline virtual ~SharedPtrCountedBase(); // nothrow
52 
55  virtual void dispose() = 0;
56 
58  inline virtual void destroy();
59 
60  virtual void * get_deleter( std::type_info const & ti ) = 0;
61 
62  inline void add_ref_copy();
63  inline bool add_ref_lock();
64  inline void release(); // nothrow
65  inline void weak_add_ref(); // nothrow
66  inline void weak_release(); // nothrow
67  inline long use_count() const; // nothrow
68 
69  private:
72 
73  long use_count_; // #shared
74  long weak_count_; // #weak + (#shared != 0)
75  };
76 
77 
79 
81  : use_count_( 1 ), weak_count_( 1 )
82 {
83 }
84 
86 {
87 }
88 
90 {
91  delete this;
92 }
93 
95 {
96  ++use_count_;
97 }
98 
100 {
101  if( use_count_ == 0 )
102  {
103  return false;
104  }
105 
106  ++use_count_;
107  return true;
108 }
109 
111 {
112  if( --use_count_ == 0 )
113  {
114  dispose();
115  weak_release();
116  }
117 }
118 
120 {
121  ++weak_count_;
122 }
123 
125 {
126  if( --weak_count_ == 0 )
127  {
128  destroy();
129  }
130 }
131 
133 {
134  return use_count_;
135 }
136 
137 } // end namespace Internal
138 } // end namespace Teamcenter
139 
140 #endif // TEAMCENTER_BASE_UTILS_SHARED_PTR_COUNTED_BASE_HXX