PtrRef.h
1 //##################################################################################################
2 //
3 // Ceetron Desktop Components
4 // Component: Core
5 //
6 // --------------------------------------------------------------------------------------------
7 // Copyright (C) 2011, Ceetron AS
8 // This is UNPUBLISHED PROPRIETARY SOURCE CODE of Ceetron AS. The contents of this file may
9 // not be disclosed to third parties, copied or duplicated in any form, in whole or in part,
10 // without the prior written permission of Ceetron AS.
11 //##################################################################################################
12 
13 #pragma once
14 
15 #include "CeeCore/Base.h"
16 
17 namespace cee {
18 
19 
20 //==================================================================================================
21 //
22 // Smart pointer class
23 //
24 //==================================================================================================
25 template <typename T>
26 class PtrRef
27 {
28 public:
29  PtrRef(T* object = NULL);
30  PtrRef(const PtrRef& other);
31  template<typename T2> PtrRef(const PtrRef<T2>& other);
32  ~PtrRef();
33 
34  PtrRef& operator=(T* rhs);
35  PtrRef& operator=(PtrRef rhs);
36  template<typename T2> PtrRef& operator=(const PtrRef<T2>& rhs);
37  inline T* operator->();
38  inline const T* operator->() const;
39  inline T& operator*();
40  inline const T& operator*() const;
41  bool operator<(const PtrRef& rhs) const;
42 
43  inline const T* get() const;
44  inline T* get();
45 
46  void swap(PtrRef& other);
47 
48  T* detach();
49 
51  operator unspecified_bool_type() const;
52 
53 private:
54  T* m_object;
55 };
56 
57 template<typename T1, typename T2> inline bool operator==(const PtrRef<T1>& a, const PtrRef<T2>& b) { return a.get() == b.get(); }
58 template<typename T1, typename T2> inline bool operator!=(const PtrRef<T1>& a, const PtrRef<T2>& b) { return a.get() != b.get(); }
59 template<typename T1, typename T2> inline bool operator==(const PtrRef<T1>& a, T2* b) { return a.get() == b; }
60 template<typename T1, typename T2> inline bool operator!=(const PtrRef<T1>& a, T2* b) { return a.get() != b; }
61 template<typename T1, typename T2> inline bool operator==(T1* a, const PtrRef<T2>& b) { return a == b.get(); }
62 template<typename T1, typename T2> inline bool operator!=(T1* a, const PtrRef<T2>& b) { return a != b.get(); }
63 
66 template<typename T> inline void swap(PtrRef<T>& a, PtrRef<T>& b) { a.swap(b); }
67 }
68 
69 #include "CeeCore/PtrRef.inl"
T * detach()
Detach the PtrRef from the internal object WITHOUT calling release on the object. ...
Definition: PtrRef.inl:289
Namespace cee contains all functionality and structures under the Core component. ...
Definition: AppAssert.cpp:18
void swap(PtrRef &other)
Exchanges the contents of the two smart pointers.
Definition: PtrRef.inl:272
PtrRef & operator=(T *rhs)
Assigns from raw pointer.
Definition: PtrRef.inl:111
bool operator!=(const PtrRef< T1 > &a, const PtrRef< T2 > &b)
Returns true if the internal pointers of refs a and b are different.
Definition: PtrRef.h:58
Smart pointer class used for handling reference counted objects (that derive from Object)...
Definition: PtrRef.h:26
bool operator==(const PtrRef< T1 > &a, const PtrRef< T2 > &b)
Returns true if the internal pointers of refs a and b are equal.
Definition: PtrRef.h:57
T & operator*()
Dereference operator returning a modifiable reference to the associated object.
Definition: PtrRef.inl:185
PtrRef(T *object=NULL)
Constructs an object from naked pointer.
Definition: PtrRef.inl:35
void swap(PtrRef< T > &a, PtrRef< T > &b)
Swap contents of a and b. Matches signature of std::swap().
Definition: PtrRef.h:66
T *PtrRef::* unspecified_bool_type
Helper type to implement the safe-bool idiom.
Definition: PtrRef.h:50
bool operator<(const PtrRef &rhs) const
Does less than comparison of two PtrRef objects.
Definition: PtrRef.inl:233
const T * get() const
Returns naked const pointer.
Definition: PtrRef.inl:217
T * operator->()
Returns the naked pointer this object is associated with.
Definition: PtrRef.inl:157