Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cOSHandleSet.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cOSHandleSet_H
7 #define _INC_cOSHandleSet_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cOSHandle.h"
13 #include "cArray.h"
14 
15 namespace Gray
16 {
18  {
24 
25  public:
26  static const int k_nHandeMax =
27 #ifdef _WIN32
28  MAXIMUM_WAIT_OBJECTS
29 #else
30  FD_SETSIZE
31 #endif
32  ;
33 
34  private:
35 #ifdef _WIN32
36  cArrayVal<HANDLE> m_fds;
37 #elif defined(__linux__)
38  HANDLE m_hHandleMax;
39  fd_set m_fds;
40 #else
41 #error NOOS
42 #endif
43 
44  private:
45  void InitHandles() noexcept
46  {
47 #ifdef __linux__
48  m_hHandleMax = 0;
49  FD_ZERO(&m_fds);
50 #endif
51  }
52 
53  public:
54  cOSHandleSet(void) noexcept
55  {
56  InitHandles();
57  }
58  cOSHandleSet(HANDLE h)
59  {
60  // a handle set with a single handle.
61 #ifdef __linux__
62  m_hHandleMax = h;
63  FD_ZERO(&fds);
64  FD_SET(h, &m_fds);
65 #else
66  InitHandles();
67  AddHandle(h);
68 #endif
69  }
71  {
72  InitHandles();
73  SetCopy(nss);
74  }
76  {
77  }
78 
79  void operator = (const cOSHandleSet& nss)
80  {
81  SetCopy(nss);
82  }
83  void SetCopy(const cOSHandleSet& nss)
84  {
85 #ifdef __linux__
86  m_hHandleMax = nss.m_hHandleMax;
87  // FD_COPY(pfds,&m_fds);
88  cMem::Copy(&m_fds,&nss.m_fds,sizeof(m_fds));
89 #else
90  m_fds = nss.m_fds;
91 #endif
92  }
93 
94  bool AddHandle(HANDLE h)
95  {
99 
100  if (h == INVALID_HANDLE_VALUE)
101  return false;
102 #ifdef __linux__
103  if ( h > m_hHandleMax )
104  m_hHandleMax = h;
105  FD_SET(h,&m_fds);
106 #else
107  // Only add up to
108  if (h == HANDLE_NULL) // cOSHandle
109  return false;
110  if (m_fds.GetSize() >= MAXIMUM_WAIT_OBJECTS)
111  return false;
112  m_fds.Add(h);
113 #endif
114  return true;
115  }
116  void RemoveHandle(HANDLE h)
117  {
118  if (h == INVALID_HANDLE_VALUE)
119  return;
120 #ifdef __linux__
121  FD_CLR(h,&m_fds);
122 #else
123  m_fds.RemoveArg(h);
124 #endif
125  }
127  {
128 #ifdef __linux__
129  InitHandles();
130 #else
131  m_fds.RemoveAll();
132 #endif
133  }
134 
135  HRESULT WaitForObjects(TIMESYSD_t dwMilliseconds, bool bWaitForAll = false);
136  };
137 };
138 #endif
#define GRAYCORE_LINK
Definition: GrayCore.h:47
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
void RemoveAll()
Clean up.
Definition: cArray.h:230
ITERATE_t Add(ARG_TYPE newElement)
Definition: cArray.h:199
ITERATE_t GetSize() const noexcept
Definition: cArray.h:137
bool RemoveArg(ARG_TYPE arg)
Definition: cArray.h:650
Definition: cArray.h:914
Definition: cOSHandleSet.h:18
void RemoveHandle(HANDLE h)
Definition: cOSHandleSet.h:116
~cOSHandleSet()
Definition: cOSHandleSet.h:75
void ClearHandles()
Definition: cOSHandleSet.h:126
cOSHandleSet(void) noexcept
Definition: cOSHandleSet.h:54
void SetCopy(const cOSHandleSet &nss)
Definition: cOSHandleSet.h:83
cOSHandleSet(HANDLE h)
Definition: cOSHandleSet.h:58
cOSHandleSet(const cOSHandleSet &nss)
Definition: cOSHandleSet.h:70
bool AddHandle(HANDLE h)
Definition: cOSHandleSet.h:94
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
static void Copy(void *pDst, const void *pSrc, size_t nSizeBlock) noexcept
Definition: cMem.h:132