OSSmartPtr.hpp
Go to the documentation of this file.
1 // Copyright (C) 2004, 2011 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // $Id: IpSmartPtr.hpp 2005 2011-06-06 12:55:16Z stefan $
6 //
7 // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
8 
9 // copied from IpReferenced.hpp so as to allow OS to be compiled
10 // stand-alone (without the Fortran implications inherent in Ipopt)
11 
12 #ifndef OSSMARTPTR_HPP
13 #define OSSMARTPTR_HPP
14 
15 #include "OSReferenced.hpp"
16 
155  template<class T>
156  class OSSmartPtr : public OSReferencer
157  {
158  public:
159 
163  OSSmartPtr();
164 
166  OSSmartPtr(const OSSmartPtr<T>& copy);
167 
169  OSSmartPtr(T* ptr);
170 
174  ~OSSmartPtr();
176 
181  T* operator->() const;
182 
185  T& operator*() const;
186 
189  OSSmartPtr<T>& operator=(T* rhs);
190 
195 
198  template <class U1, class U2>
199  friend
200  bool operator==(const OSSmartPtr<U1>& lhs, const OSSmartPtr<U2>& rhs);
201 
204  template <class U1, class U2>
205  friend
206  bool operator==(const OSSmartPtr<U1>& lhs, U2* raw_rhs);
207 
210  template <class U1, class U2>
211  friend
212  bool operator==(U1* lhs, const OSSmartPtr<U2>& raw_rhs);
213 
216  template <class U1, class U2>
217  friend
218  bool operator!=(const OSSmartPtr<U1>& lhs, const OSSmartPtr<U2>& rhs);
219 
222  template <class U1, class U2>
223  friend
224  bool operator!=(const OSSmartPtr<U1>& lhs, U2* raw_rhs);
225 
228  template <class U1, class U2>
229  friend
230  bool operator!=(U1* lhs, const OSSmartPtr<U2>& raw_rhs);
232 
245  template <class U>
246  friend
247  U* GetRawPtr(const OSSmartPtr<U>& smart_ptr);
248 
250  template <class U>
251  friend
252  OSSmartPtr<const U> ConstPtr(const OSSmartPtr<U>& smart_ptr);
253 
258  template <class U>
259  friend
260  bool IsValid(const OSSmartPtr<U>& smart_ptr);
261 
266  template <class U>
267  friend
268  bool IsNull(const OSSmartPtr<U>& smart_ptr);
270 
271  private:
275  T* ptr_;
276 
280  OSSmartPtr<T>& SetFromRawPtr_(T* rhs);
281 
286 
288  void ReleasePointer_();
290  };
291 
294  template <class U>
295  U* GetRawPtr(const OSSmartPtr<U>& smart_ptr);
296 
297  template <class U>
298  OSSmartPtr<const U> ConstPtr(const OSSmartPtr<U>& smart_ptr);
299 
300  template <class U>
301  bool IsNull(const OSSmartPtr<U>& smart_ptr);
302 
303  template <class U>
304  bool IsValid(const OSSmartPtr<U>& smart_ptr);
305 
306  template <class U1, class U2>
307  bool operator==(const OSSmartPtr<U1>& lhs, const OSSmartPtr<U2>& rhs);
308 
309  template <class U1, class U2>
310  bool operator==(const OSSmartPtr<U1>& lhs, U2* raw_rhs);
311 
312  template <class U1, class U2>
313  bool operator==(U1* lhs, const OSSmartPtr<U2>& raw_rhs);
314 
315  template <class U1, class U2>
316  bool operator!=(const OSSmartPtr<U1>& lhs, const OSSmartPtr<U2>& rhs);
317 
318  template <class U1, class U2>
319  bool operator!=(const OSSmartPtr<U1>& lhs, U2* raw_rhs);
320 
321  template <class U1, class U2>
322  bool operator!=(U1* lhs, const OSSmartPtr<U2>& raw_rhs);
323 
325 
326 
327  template <class T>
329  :
330  ptr_(0)
331  {
332 
333 #ifdef CHECK_SMARTPTR
334 
335  const OSReferencedObject* trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_
336  = ptr_;
337  trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = 0;
338 #endif
339 
340  }
341 
342 
343  template <class T>
345  :
346  ptr_(0)
347  {
348 
349 #ifdef CHECK_SMARTPTR
350 
351  const ReferencedObject* trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_
352  = ptr_;
353  trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = 0;
354 #endif
355 
356  (void) SetFromSmartPtr_(copy);
357  }
358 
359 
360  template <class T>
362  :
363  ptr_(0)
364  {
365 #ifdef CHECK_SMARTPTR
366 
367  const ReferencedObject* trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_
368  = ptr_;
369  trying_to_use_SmartPtr_with_an_object_that_does_not_inherit_from_ReferencedObject_ = 0;
370 #endif
371 
372  (void) SetFromRawPtr_(ptr);
373  }
374 
375  template <class T>
377  {
378  ReleasePointer_();
379  }
380 
381  template <class T>
383  {
384  return ptr_;
385  }
386 
387 
388  template <class T>
390  {
391  return *ptr_;
392  }
393 
394 
395  template <class T>
397  {
398  return SetFromRawPtr_(rhs);
399  }
400 
401 
402  template <class T>
404  {
405  return SetFromSmartPtr_(rhs);
406  }
407 
408 
409  template <class T>
411  {
412  // Release any old pointer
413  ReleasePointer_();
414 
415  if (rhs != 0) {
416  rhs->AddRef(this);
417  ptr_ = rhs;
418  }
419 
420  return *this;
421  }
422 
423  template <class T>
425  {
426  T* ptr = GetRawPtr(rhs);
427  /* AW: I changed this so that NULL is correctly copied from the
428  right hand side */
429  // if (ptr != NULL) {
430  // SetFromRawPtr_(ptr);
431  // }
432  SetFromRawPtr_(ptr);
433 
434  return (*this);
435  }
436 
437 
438  template <class T>
440  {
441  if (ptr_) {
442  ptr_->ReleaseRef(this);
443  if (ptr_->ReferenceCount() == 0) {
444  delete ptr_;
445  }
446  ptr_ = 0;
447  }
448  }
449 
450 
451  template <class U>
452  U* GetRawPtr(const OSSmartPtr<U>& smart_ptr)
453  {
454  return smart_ptr.ptr_;
455  }
456 
457  template <class U>
459  {
460  // compiler should implicitly cast
461  return GetRawPtr(smart_ptr);
462  }
463 
464  template <class U>
465  bool IsValid(const OSSmartPtr<U>& smart_ptr)
466  {
467  return !IsNull(smart_ptr);
468  }
469 
470  template <class U>
471  bool IsNull(const OSSmartPtr<U>& smart_ptr)
472  {
473  return (smart_ptr.ptr_ == 0);
474  }
475 
476 
477  template <class U1, class U2>
478  bool ComparePointers(const U1* lhs, const U2* rhs)
479  {
480  if (lhs == rhs) {
481  return true;
482  }
483 
484  // Even if lhs and rhs point to the same object
485  // with different interfaces U1 and U2, we cannot guarantee that
486  // the value of the pointers will be equivalent. We can
487  // guarantee this if we convert to void*
488  const void* v_lhs = static_cast<const void*>(lhs);
489  const void* v_rhs = static_cast<const void*>(rhs);
490  if (v_lhs == v_rhs) {
491  return true;
492  }
493 
494  // They must not be the same
495  return false;
496  }
497 
498  template <class U1, class U2>
499  bool operator==(const OSSmartPtr<U1>& lhs, const OSSmartPtr<U2>& rhs)
500  {
501  U1* raw_lhs = GetRawPtr(lhs);
502  U2* raw_rhs = GetRawPtr(rhs);
503  return ComparePointers(raw_lhs, raw_rhs);
504  }
505 
506  template <class U1, class U2>
507  bool operator==(const OSSmartPtr<U1>& lhs, U2* raw_rhs)
508  {
509  U1* raw_lhs = GetRawPtr(lhs);
510  return ComparePointers(raw_lhs, raw_rhs);
511  }
512 
513  template <class U1, class U2>
514  bool operator==(U1* raw_lhs, const OSSmartPtr<U2>& rhs)
515  {
516  const U2* raw_rhs = GetRawPtr(rhs);
517  return ComparePointers(raw_lhs, raw_rhs);
518  }
519 
520  template <class U1, class U2>
521  bool operator!=(const OSSmartPtr<U1>& lhs, const OSSmartPtr<U2>& rhs)
522  {
523  bool retValue = operator==(lhs, rhs);
524  return !retValue;
525  }
526 
527  template <class U1, class U2>
528  bool operator!=(const OSSmartPtr<U1>& lhs, U2* raw_rhs)
529  {
530  bool retValue = operator==(lhs, raw_rhs);
531  return !retValue;
532  }
533 
534  template <class U1, class U2>
535  bool operator!=(U1* raw_lhs, const OSSmartPtr<U2>& rhs)
536  {
537  bool retValue = operator==(raw_lhs, rhs);
538  return !retValue;
539  }
540 
541 #endif
542 
~OSSmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
Definition: OSSmartPtr.hpp:376
friend bool operator==(const OSSmartPtr< U1 > &lhs, const OSSmartPtr< U2 > &rhs)
Overloaded equality comparison operator, allows the user to compare the value of two OSSmartPtrs...
bool ComparePointers(const U1 *lhs, const U2 *rhs)
Definition: OSSmartPtr.hpp:478
bool IsValid(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:465
Template class for Smart Pointers.
Definition: OSSmartPtr.hpp:156
bool operator==(const BCP_obj_change &ch0, const BCP_obj_change &ch1)
OSSmartPtr< T > & SetFromSmartPtr_(const OSSmartPtr< T > &rhs)
Set the value of the internal raw pointer from an OSSmartPtr, releasing the previously referenced obj...
Definition: OSSmartPtr.hpp:424
OSSmartPtr< T > & operator=(T *rhs)
Overloaded equals operator, allows the user to set the value of the OSSmartPtr from a raw pointer...
Definition: OSSmartPtr.hpp:396
void ReleasePointer_()
Release the currently referenced object.
Definition: OSSmartPtr.hpp:439
friend bool IsNull(const OSSmartPtr< U > &smart_ptr)
Returns true if the OSSmartPtr is NULL.
ReferencedObject class.
bool IsNull(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:471
Definition: OSdtoa.cpp:354
OSSmartPtr< const U > ConstPtr(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:458
U * GetRawPtr(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:452
OSSmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
Definition: OSSmartPtr.hpp:410
T * ptr_
Actual raw pointer to the object.
Definition: OSSmartPtr.hpp:275
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
Definition: OSSmartPtr.hpp:389
bool operator!=(const BCP_obj_change &ch0, const BCP_obj_change &ch1)
static void
Definition: OSdtoa.cpp:1741
friend U * GetRawPtr(const OSSmartPtr< U > &smart_ptr)
Returns the raw pointer contained.
OSSmartPtr()
Default constructor, initialized to NULL.
Definition: OSSmartPtr.hpp:328
friend bool IsValid(const OSSmartPtr< U > &smart_ptr)
Returns true if the OSSmartPtr is NOT NULL.
friend OSSmartPtr< const U > ConstPtr(const OSSmartPtr< U > &smart_ptr)
Returns a const pointer.
Pseudo-class, from which everything has to inherit that wants to use be registered as a Referencer fo...
friend bool operator!=(const OSSmartPtr< U1 > &lhs, const OSSmartPtr< U2 > &rhs)
Overloaded in-equality comparison operator, allows the user to compare the value of two OSSmartPtrs...
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
Definition: OSSmartPtr.hpp:382