My Project
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>
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
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:
276
281
286
288 void ReleasePointer_();
290 };
291
294 template <class U>
295 U* GetRawPtr(const OSSmartPtr<U>& smart_ptr);
296
297 template <class U>
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
bool ComparePointers(const U1 *lhs, const U2 *rhs)
U * GetRawPtr(const OSSmartPtr< U > &smart_ptr)
bool IsValid(const OSSmartPtr< U > &smart_ptr)
bool operator==(const OSSmartPtr< U1 > &lhs, const OSSmartPtr< U2 > &rhs)
bool IsNull(const OSSmartPtr< U > &smart_ptr)
OSSmartPtr< const U > ConstPtr(const OSSmartPtr< U > &smart_ptr)
bool operator!=(const OSSmartPtr< U1 > &lhs, const OSSmartPtr< U2 > &rhs)
ReferencedObject class.
Pseudo-class, from which everything has to inherit that wants to use be registered as a Referencer fo...
Template class for Smart Pointers.
void ReleasePointer_()
Release the currently referenced object.
T & operator*() const
Overloaded dereference operator, allows the user to dereference the contained pointer.
friend U * GetRawPtr(const OSSmartPtr< U > &smart_ptr)
Returns the raw pointer contained.
OSSmartPtr< T > & operator=(T *rhs)
Overloaded equals operator, allows the user to set the value of the OSSmartPtr from a raw pointer.
T * ptr_
Actual raw pointer to the object.
OSSmartPtr< T > & SetFromSmartPtr_(const OSSmartPtr< T > &rhs)
Set the value of the internal raw pointer from an OSSmartPtr, releasing the previously referenced obj...
~OSSmartPtr()
Destructor, automatically decrements the reference count, deletes the object if necessary.
T * operator->() const
Overloaded arrow operator, allows the user to call methods using the contained pointer.
friend bool IsValid(const OSSmartPtr< U > &smart_ptr)
Returns true if the OSSmartPtr is NOT NULL.
OSSmartPtr< T > & SetFromRawPtr_(T *rhs)
Set the value of the internal raw pointer from another raw pointer, releasing the previously referenc...
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.
OSSmartPtr()
Default constructor, initialized to NULL.
friend bool IsNull(const OSSmartPtr< U > &smart_ptr)
Returns true if the OSSmartPtr is NULL.
friend OSSmartPtr< const U > ConstPtr(const OSSmartPtr< U > &smart_ptr)
Returns a const pointer.
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.
Definition OSdtoa.cpp:354