You.i Engine
YiScopedPtr.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_SCOPED_PTR_H_
3 #define _YI_SCOPED_PTR_H_
4 
6 #include "utility/YiError.h"
7 
8 #include <memory>
9 
150 template<typename YI_PTR_TYPE, typename YI_DELETE_FUNCTOR = typename std::unique_ptr<YI_PTR_TYPE>::deleter_type>
152 {
153  template<typename YI_OTHER_PTR_TYPE, typename YI_OTHER_DELETER>
154  friend class CYIScopedPtr;
155 
156 public:
162  inline explicit CYIScopedPtr(YI_PTR_TYPE *pPtr = nullptr) :
163  m_pUnique(pPtr, YI_DELETE_FUNCTOR())
164  {
165  }
166 
173  inline CYIScopedPtr(YI_PTR_TYPE *pPtr, const YI_DELETE_FUNCTOR &deleter) :
174  m_pUnique(pPtr, deleter)
175  {
176  }
177 
183  inline CYIScopedPtr(CYIScopedPtr &&other) :
184  m_pUnique(std::move(other.m_pUnique))
185  {
186  }
187 
193  inline CYIScopedPtr(std::unique_ptr<YI_PTR_TYPE, YI_DELETE_FUNCTOR> &&other) :
194  m_pUnique(std::move(other))
195  {
196  }
197 
204  inline CYIScopedPtr(YI_PTR_TYPE *pPtr, YI_DELETE_FUNCTOR &&deleter) :
205  m_pUnique(pPtr, std::move(deleter))
206  {
207  }
208 
214  template<typename YI_PTR_OTHER_TYPE, typename YI_OTHER_DELETER>
216  m_pUnique(std::move(other.m_pUnique))
217  {
218  }
219 
223  inline ~CYIScopedPtr()
224  {
225  }
226 
253  inline void operator=(YI_PTR_TYPE *pPtr)
254  {
255  m_pUnique.reset(pPtr);
256  }
257 
263  {
264  m_pUnique = std::move(other.m_pUnique);
265  return *this;
266  }
267 
272  inline CYIScopedPtr &operator=(std::unique_ptr<YI_PTR_TYPE, YI_DELETE_FUNCTOR> &&other)
273  {
274  m_pUnique = std::move(other);
275  return *this;
276  }
277 
282  template<typename YI_PTR_OTHER_TYPE, typename YI_OTHER_DELETER>
284  {
285  m_pUnique = std::move(other.m_pUnique);
286  return *this;
287  }
288 
304  inline YI_PTR_TYPE &operator*() const
305  {
306  return m_pUnique.operator*();
307  }
308 
324  inline YI_PTR_TYPE *operator->() const
325  {
326  YI_ASSERT(GetPointer(), "CYIScopedPtr", "Attempted to dereference a null scoped pointer.");
327  return m_pUnique.operator->();
328  }
329 
352  inline bool operator!() const
353  {
354  return GetPointer() == nullptr;
355  }
356 
378  inline operator bool() const
379  {
380  return GetPointer() != nullptr;
381  }
382 
383  template<typename YI_PTR_OTHER_TYPE, typename YI_OTHER_DELETER>
384  inline operator std::unique_ptr<YI_PTR_OTHER_TYPE, YI_OTHER_DELETER>()
385  {
386  return std::move(m_pUnique);
387  }
388 
417  inline void Reset(YI_PTR_TYPE *pPtr = nullptr)
418  {
419  operator=(pPtr);
420  }
421 
440  inline YI_PTR_TYPE *Get() const
441  {
442  return GetPointer();
443  }
444 
468  inline YI_PTR_TYPE *Take()
469  {
470  return m_pUnique.release();
471  }
472 
495  inline bool IsNull() const
496  {
497  return GetPointer() == nullptr;
498  }
499 
531  {
532  m_pUnique.swap(rPtr.m_pUnique);
533  }
534 
535 private:
536  inline YI_PTR_TYPE *GetPointer() const
537  {
538  return m_pUnique.get();
539  }
540 
541  std::unique_ptr<YI_PTR_TYPE, YI_DELETE_FUNCTOR> m_pUnique;
542 
544 };
545 
547 
548 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
549 inline bool
551 {
552  return rLhs.Get() == rRhs.Get();
553 }
554 
555 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
556 inline bool
558 {
559  return rLhs.Get() != rRhs.Get();
560 }
561 
562 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE>
563 inline bool
564 operator==(const CYIScopedPtr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const YI_R_PTR_TYPE *pRhs)
565 {
566  return rLhs.Get() == pRhs;
567 }
568 
569 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE>
570 inline bool
571 operator!=(const CYIScopedPtr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const YI_R_PTR_TYPE *pRhs)
572 {
573  return rLhs.Get() != pRhs;
574 }
575 
576 template<typename YI_L_PTR_TYPE, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
577 inline bool
578 operator==(const YI_L_PTR_TYPE *pLhs, const CYIScopedPtr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
579 {
580  return pLhs == rRhs.Get();
581 }
582 
583 template<typename YI_L_PTR_TYPE, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
584 inline bool
585 operator!=(const YI_L_PTR_TYPE *pLhs, const CYIScopedPtr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
586 {
587  return pLhs != rRhs.Get();
588 }
589 
590 #if __cplusplus >= 201103L || defined(YI_UWP) || defined(YI_WIN32)
591 
592 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR>
593 inline bool
594 operator==(const CYIScopedPtr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const std::nullptr_t &pRhs)
595 {
596  return rLhs.Get() == pRhs;
597 }
598 
599 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR>
600 inline bool
601 operator!=(const CYIScopedPtr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const std::nullptr_t &pRhs)
602 {
603  return rLhs.Get() != pRhs;
604 }
605 
606 template<typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
607 inline bool
608 operator==(const std::nullptr_t &pLhs, const CYIScopedPtr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
609 {
610  return pLhs == rRhs.Get();
611 }
612 
613 template<typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
614 inline bool
615 operator!=(const std::nullptr_t &pLhs, const CYIScopedPtr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
616 {
617  return pLhs != rRhs.Get();
618 }
619 
620 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
621 inline bool
622 operator==(const CYIScopedPtr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const std::unique_ptr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
623 {
624  return rLhs.Get() == rRhs.get();
625 }
626 
627 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
628 inline bool
629 operator!=(const CYIScopedPtr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const std::unique_ptr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
630 {
631  return rLhs.Get() != rRhs.get();
632 }
633 
634 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
635 inline bool
636 operator==(const std::unique_ptr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const CYIScopedPtr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
637 {
638  return rLhs.get() == rRhs.Get();
639 }
640 
641 template<typename YI_L_PTR_TYPE, typename YI_L_DELETE_FUNCTOR, typename YI_R_PTR_TYPE, typename YI_R_DELETE_FUNCTOR>
642 inline bool
643 operator!=(const std::unique_ptr<YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR> &rLhs, const CYIScopedPtr<YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR> &rRhs)
644 {
645  return rLhs.get() != rRhs.Get();
646 }
647 
648 #endif // C++11 checks
649 
651 #endif /* _YI_SCOPED_PTR_H_ */
YI_PTR_TYPE * Get() const
Definition: YiScopedPtr.h:440
bool operator!=(const CYIScopedPtr< YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR > &rLhs, const CYIScopedPtr< YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR > &rRhs)
Definition: YiScopedPtr.h:557
#define YI_DISALLOW_COPY_AND_ASSIGN(TypeName)
Delete the copy constructor and assignment operator (and consequently the move constructor as well) ...
Definition: YiPredef.h:114
CYIScopedPtr & operator=(std::unique_ptr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &&other)
Definition: YiScopedPtr.h:272
void Swap(CYIScopedPtr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &rPtr)
Definition: YiScopedPtr.h:530
YI_PTR_TYPE & operator*() const
Definition: YiScopedPtr.h:304
CYIScopedPtr(YI_PTR_TYPE *pPtr, const YI_DELETE_FUNCTOR &deleter)
Definition: YiScopedPtr.h:173
void Reset(YI_PTR_TYPE *pPtr=nullptr)
Definition: YiScopedPtr.h:417
CYIScopedPtr & operator=(CYIScopedPtr< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > &&other)
Definition: YiScopedPtr.h:283
CYIScopedPtr(CYIScopedPtr< YI_PTR_OTHER_TYPE, YI_OTHER_DELETER > &&other)
Definition: YiScopedPtr.h:215
CYIScopedPtr(YI_PTR_TYPE *pPtr, YI_DELETE_FUNCTOR &&deleter)
Definition: YiScopedPtr.h:204
STL namespace.
CYIScopedPtr(std::unique_ptr< YI_PTR_TYPE, YI_DELETE_FUNCTOR > &&other)
Definition: YiScopedPtr.h:193
~CYIScopedPtr()
Definition: YiScopedPtr.h:223
CYIScopedPtr(CYIScopedPtr &&other)
Definition: YiScopedPtr.h:183
CYIScopedPtr(YI_PTR_TYPE *pPtr=nullptr)
Definition: YiScopedPtr.h:162
The CYIScopedPtr is a smart pointer that takes sole ownership of a dynamically allocated object...
Definition: YiScopedPtr.h:151
YI_PTR_TYPE * operator->() const
Definition: YiScopedPtr.h:324
YI_PTR_TYPE * Take()
Definition: YiScopedPtr.h:468
void operator=(YI_PTR_TYPE *pPtr)
Definition: YiScopedPtr.h:253
CYIScopedPtr & operator=(CYIScopedPtr &&other)
Definition: YiScopedPtr.h:262
#define YI_ASSERT(condition, tag, msg,...)
Platform-independent assertion macro.
Definition: YiError.h:37
bool operator==(const CYIScopedPtr< YI_L_PTR_TYPE, YI_L_DELETE_FUNCTOR > &rLhs, const CYIScopedPtr< YI_R_PTR_TYPE, YI_R_DELETE_FUNCTOR > &rRhs)
Definition: YiScopedPtr.h:550
bool IsNull() const
Definition: YiScopedPtr.h:495
bool operator!() const
Definition: YiScopedPtr.h:352