Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cHandlePtr.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cHandlePtr_H
8 #define _INC_cHandlePtr_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cNonCopyable.h"
14 
15 namespace Gray
16 {
17 #define HANDLEPTR_NULL nullptr
18 
19  template< typename _TYPE_HAND = void* >
20  class cHandlePtr : protected cNonCopyable
21  {
30 
31  private:
32  _TYPE_HAND m_h;
33 
34  protected:
36  {
38  if (!isValidHandle())
39  return;
40  CloseHandle(m_h);
41  }
42 
43  public:
45  static void inline CloseHandle(_TYPE_HAND h); // Don't use/define a default implementation! This should fail at compile time if type is not implemented explicitly.
46 
47  explicit inline cHandlePtr(_TYPE_HAND h = nullptr) noexcept
48  : m_h(h)
49  {
50  }
51  inline ~cHandlePtr()
52  {
54  }
55 
56  bool isValidHandle() const noexcept
57  {
58  return m_h != nullptr;
59  }
60 
61  operator _TYPE_HAND () const noexcept
62  {
63  return m_h;
64  }
65  _TYPE_HAND operator -> () const noexcept
66  {
67  return m_h;
68  }
69 
70  _TYPE_HAND get_Handle() const noexcept
71  {
72  return m_h;
73  }
74  _TYPE_HAND& ref_Handle() noexcept
75  {
76  return m_h;
77  }
78 
79  void CloseHandle()
80  {
81  if (!isValidHandle())
82  return;
83  _TYPE_HAND h = m_h;
84  m_h = nullptr;
85  CloseHandle(h);
86  }
87  void AttachHandle(_TYPE_HAND h)
88  {
89  if (m_h != h)
90  {
92  m_h = h;
93  }
94  }
95  _TYPE_HAND DetachHandle() noexcept
96  {
97  _TYPE_HAND h = m_h;
98  m_h = nullptr;
99  return h;
100  }
101  };
102 }
103 #endif // _INC_cHandlePtr_H
Definition: cHandlePtr.h:21
bool isValidHandle() const noexcept
Definition: cHandlePtr.h:56
_TYPE_HAND get_Handle() const noexcept
Definition: cHandlePtr.h:70
_TYPE_HAND DetachHandle() noexcept
Definition: cHandlePtr.h:95
cHandlePtr(_TYPE_HAND h=nullptr) noexcept
Definition: cHandlePtr.h:47
~cHandlePtr()
Definition: cHandlePtr.h:51
void CloseHandleLast()
Definition: cHandlePtr.h:35
_TYPE_HAND & ref_Handle() noexcept
Definition: cHandlePtr.h:74
_TYPE_HAND operator->() const noexcept
Definition: cHandlePtr.h:65
static void CloseHandle(_TYPE_HAND h)
MUST Implement versions of this for each _TYPE_HAND.
void CloseHandle()
Definition: cHandlePtr.h:79
void AttachHandle(_TYPE_HAND h)
Definition: cHandlePtr.h:87
Definition: cNonCopyable.h:17
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14