Teamcenter C++ API Reference  2312
SharedCount.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 - comment code about WeakPtr as we arent supporting weak_ptr currently.
27 - changes in constructor to remove NX specific error handling.
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 #ifndef TEAMCENTER_BASE_UTILS_SHARED_COUNT_HXX
39 #define TEAMCENTER_BASE_UTILS_SHARED_COUNT_HXX
40 
41 #include <memory> // std::auto_ptr
42 #include <functional> // std::less
43 #include <base_utils/SharedPtrCountedImpl.hxx>
44 
45 namespace Teamcenter
46 {
47 namespace Internal
48 {
49  class WeakCount;
50 
52  {
53  friend class WeakCount;
54  public:
55  inline SharedCount();
56  inline SharedCount( SharedCount const & r );
57 
58 
59  template< class Y > inline SharedCount( Y * p ): pi_( 0 )
60  {
61  // Take ownership of pointer before newing up refcount,
62  // in case we run out of memory.
63  std::auto_ptr< Y > guard( p );
64  pi_ = new Teamcenter::Internal::SharedPtrCountedBaseImpl_p< Y >( p );
65  guard.release();
66  }
67 
69  template< class Y > inline SharedCount( std::auto_ptr< Y > & r )
70  : pi_( new Teamcenter::Internal::SharedPtrCountedBaseImpl_p< Y >( r.get() ) )
71  {
72  r.release();
73  }
74 
75 #if 0 // commented as custom deallocator isnt supported.
76  template< class P, class D > SharedCount( P p, D d );
77 #endif
78 
79 #if 0 // commented as we dont support weak_ptr. Might need some changes to code for Tc.
80  BASE_UTILS_API explicit SharedCount( WeakCount const & r );
81 #endif
82  inline ~SharedCount(); // nothrow
83 
84  inline SharedCount& operator= ( SharedCount const & r ); // nothrow
85  inline void swap( SharedCount & r ); // nothrow
86 
87  inline long use_count() const; // nothrow
88 
90  inline bool unique() const; // nothrow
91 
92  //lint -e1761 Line is wrongly confusing these operators as hiding other ones.
93  friend inline bool operator==( SharedCount const & a, SharedCount const & b );
94 
95  //lint -e1761 Line is wrongly confusing these operators as hiding other ones.
96  friend inline bool operator<( SharedCount const & a, SharedCount const & b );
97 
98  private:
99  Teamcenter::Internal::SharedPtrCountedBase * pi_; // pointer to reference count
100 
101  }; // class SharedCount
102 
104 
105  inline SharedCount::SharedCount(): pi_( 0 )
106  {
107  }
108 
109  inline SharedCount::~SharedCount() // nothrow
110  {
111  if( pi_ != NULL )
112  {
113  pi_->release();
114  }
115 
116  }
117 
119  : pi_( r.pi_ ) //lint !e1554
120  {
121  if( pi_ != NULL )
122  {
123  pi_->add_ref_copy();
124  }
125  }
126 
127 #if 0 // commenting as we do not support custom deallocator. Code might need changes for Tc erorr handling.
128  template< class P, class D > SharedCount::SharedCount( P p, D d ): pi_( 0 )
129  {
130  ERROR_PROTECT
131  pi_ = new Teamcenter::Internal::SharedPtrCountedBaseImpl_pd< P, D >( p, d );
132  ERROR_RECOVER
133  d( p ); // delete p
134  ERROR_reraise();
135  ERROR_END
136  }
137 #endif
138 
139 #if 0 // Commented as we do not support weak_ptr at the moment. might need some changes for Tc error handling.
140  inline SharedCount::SharedCount( Teamcenter::Internal::WeakCount const & r ): pi_( r.pi_ )
141  {
142  if ( pi_ == NULL || !pi_->add_ref_lock() )
143  {
144  ERROR_severe( ERROR_line, 0, "bad Teamcenter::BadWeakPtr()" );
145  }
146  }
147 #endif
148 
150  {
151  if( &rhs != this )
152  {
153  SharedPtrCountedBase* tmp = rhs.pi_;
154 
155  if( tmp != NULL )
156  {
157  tmp->add_ref_copy();
158  }
159 
160  if( pi_ != NULL )
161  {
162  pi_->release();
163  }
164 
165  pi_ = tmp;
166  }
167 
168  return *this;
169  }
170 
171  inline void SharedCount::swap( SharedCount & other ) // nothrow
172  {
173  std::swap( pi_, other.pi_ );
174  }
175 
176  inline long SharedCount::use_count() const // nothrow
177  {
178  return pi_ != NULL ? pi_->use_count(): 0;
179  }
180 
181  inline bool SharedCount::unique() const // nothrow
182  {
183  return use_count() == 1;
184  }
185 
186 #if 0 // custom deallocator isnt supported
187  inline void * SharedCount::get_deleter( std::type_info const & ti ) const
188  {
189  return pi_? pi_->get_deleter( ti ): 0;
190  }
191 #endif
192  inline bool operator==( SharedCount const & a, SharedCount const & b )
193  {
194  return a.pi_ == b.pi_;
195  }
196 
197  inline bool operator<( SharedCount const & a, SharedCount const & b )
198  {
199  return std::less< SharedPtrCountedBase* >()( a.pi_, b.pi_ );
200  }
201 
202 
203 #if 0 // WeakCount is used for weak_ptr. As weak_ptr isnt supported yet, commenting the code.
204  // consider moving this class to a separate file.
205 
206  class WeakCount
207  {
208  private:
210 
211  friend class SharedCount;
212 
213  public:
215  WeakCount(): pi_( 0 ) // nothrow
216  {
217  }
219  WeakCount( SharedCount const & r ): pi_( r.pi_ ) //lint !e1931 // can be used for conversion // nothrow
220  {
221  if( pi_ != NULL ) pi_->weak_add_ref();
222  }
224  WeakCount( WeakCount const & r ): pi_( r.pi_ ) //lint !e1554 // nothrow
225  {
226  if( pi_ != NULL ) pi_->weak_add_ref();
227  }
228 
229  ~WeakCount() // nothrow
230  {
231  if( pi_ != NULL ) pi_->weak_release();
232  // <BJS> 20-Jun-2008
233  // Because we want shared pointers to be as efficient as possible this ifdef
234  // is to show we know what is happening to qaz yet not affect performance
235 #ifdef _lint
236  pi_ = NULL;
237 #endif
238  }
239 
240  WeakCount & operator= ( SharedCount const & r ) // nothrow
241  {
243  if( tmp != NULL ) tmp->weak_add_ref();
244  if( pi_ != NULL ) pi_->weak_release();
245  pi_ = tmp;
246 
247  return *this;
248  }
249 
250  WeakCount & operator= ( WeakCount const & r ) // nothrow
251  {
252  if ( &r != this )
253  {
255  if( tmp != NULL ) tmp->weak_add_ref();
256  if( pi_ != NULL ) pi_->weak_release();
257  pi_ = tmp;
258  }
259 
260  return *this;
261  }
262 
263  void swap( WeakCount & r ) // nothrow
264  {
266  r.pi_ = pi_;
267  pi_ = tmp;
268  }
269 
270  long use_count() const // nothrow
271  {
272  return pi_ != NULL ? pi_->use_count(): 0;
273  }
274 
275  friend inline bool operator==( WeakCount const & a, WeakCount const & b )
276  {
277  return a.pi_ == b.pi_;
278  }
279 
280  friend inline bool operator<( WeakCount const & a, WeakCount const & b )
281  {
282  return std::less< Teamcenter::Internal::SharedPtrCountedBase * >()( a.pi_, b.pi_ );
283  }
284  }; // class WeakCount
285 #endif
286 
287 } // end namespace Internal
288 } // end namespace Teamcenter
289 
290 #endif