20#ifndef WINPR_COLLECTIONS_H
21#define WINPR_COLLECTIONS_H
28#include <winpr/winpr.h>
29#include <winpr/wtypes.h>
30#include <winpr/assert.h>
33#include <winpr/synch.h>
34#include <winpr/stream.h>
41 typedef void* (*OBJECT_NEW_FN)(
const void* val);
42 typedef void (*OBJECT_INIT_FN)(
void* obj);
43 typedef void (*OBJECT_UNINIT_FN)(
void* obj);
44 typedef void (*OBJECT_FREE_FN)(
void* obj);
45 typedef BOOL (*OBJECT_EQUALS_FN)(
const void* objA,
const void* objB);
53 WINPR_ATTR_NODISCARD OBJECT_NEW_FN
60 WINPR_ATTR_NODISCARD OBJECT_EQUALS_FN
72 WINPR_API
void* winpr_ObjectStringClone(
const void* pvstr);
80 WINPR_API
void* winpr_ObjectWStringClone(
const void* pvstr);
86 WINPR_API
void winpr_ObjectStringFree(
void* pvstr);
90 typedef struct s_wQueue wQueue;
99 WINPR_API
size_t Queue_Count(wQueue* queue);
108 WINPR_API
size_t Queue_Capacity(wQueue* queue);
114 WINPR_API
void Queue_Lock(wQueue* queue);
120 WINPR_API
void Queue_Unlock(wQueue* queue);
128 WINPR_API HANDLE Queue_Event(wQueue* queue);
138 WINPR_API
wObject* Queue_Object(wQueue* queue);
144 WINPR_API
void Queue_Clear(wQueue* queue);
154 WINPR_API BOOL Queue_Contains(wQueue* queue,
const void* obj);
166 WINPR_API BOOL Queue_Enqueue(wQueue* queue,
const void* obj);
176 WINPR_API
void* Queue_Dequeue(wQueue* queue);
186 WINPR_API
void* Queue_Peek(wQueue* queue);
195 WINPR_API
void Queue_Discard(wQueue* queue);
201 WINPR_API
void Queue_Free(wQueue* queue);
215 WINPR_ATTR_MALLOC(Queue_Free, 1)
216 WINPR_API wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor);
220 typedef struct s_wStack wStack;
223 WINPR_API
size_t Stack_Count(wStack* stack);
226 WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
229 WINPR_API
wObject* Stack_Object(wStack* stack);
231 WINPR_API
void Stack_Clear(wStack* stack);
234 WINPR_API BOOL Stack_Contains(wStack* stack, const
void* obj);
236 WINPR_API
void Stack_Push(wStack* stack,
void* obj);
239 WINPR_API
void* Stack_Pop(wStack* stack);
242 WINPR_API
void* Stack_Peek(wStack* stack);
244 WINPR_API
void Stack_Free(wStack* stack);
246 WINPR_ATTR_MALLOC(Stack_Free, 1)
247 WINPR_API wStack* Stack_New(BOOL synchronized);
251 typedef struct s_wArrayList wArrayList;
254 WINPR_API
size_t ArrayList_Capacity(wArrayList* arrayList);
257 WINPR_API
size_t ArrayList_Count(wArrayList* arrayList);
260 WINPR_API
size_t ArrayList_Items(wArrayList* arrayList, ULONG_PTR** ppItems);
263 WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList);
266 WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList);
269 WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList);
271 WINPR_API
void ArrayList_Lock(wArrayList* arrayList);
272 WINPR_API
void ArrayList_Unlock(wArrayList* arrayList);
275 WINPR_API
void* ArrayList_GetItem(wArrayList* arrayList,
size_t index);
278 WINPR_API BOOL ArrayList_SetItem(wArrayList* arrayList,
size_t index, const
void* obj);
281 WINPR_API
wObject* ArrayList_Object(wArrayList* arrayList);
283 typedef BOOL (*ArrayList_ForEachFkt)(
void* data,
size_t index, va_list ap);
286 WINPR_API BOOL ArrayList_ForEach(wArrayList* arrayList, ArrayList_ForEachFkt fkt, ...);
289 WINPR_API BOOL ArrayList_ForEachAP(wArrayList* arrayList, ArrayList_ForEachFkt fkt, va_list ap);
291 WINPR_API
void ArrayList_Clear(wArrayList* arrayList);
294 WINPR_API BOOL ArrayList_Contains(wArrayList* arrayList, const
void* obj);
296#if defined(WITH_WINPR_DEPRECATED)
297 WINPR_DEPRECATED(WINPR_ATTR_NODISCARD WINPR_API
int ArrayList_Add(wArrayList* arrayList,
301 WINPR_API BOOL ArrayList_Append(wArrayList* arrayList,
const void* obj);
304 WINPR_API BOOL ArrayList_Insert(wArrayList* arrayList,
size_t index,
const void* obj);
306 WINPR_API BOOL ArrayList_Remove(wArrayList* arrayList,
const void* obj);
308 WINPR_API BOOL ArrayList_RemoveAt(wArrayList* arrayList,
size_t index);
311 WINPR_API SSIZE_T ArrayList_IndexOf(wArrayList* arrayList,
const void* obj, SSIZE_T startIndex,
315 WINPR_API SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList,
const void* obj,
316 SSIZE_T startIndex, SSIZE_T count);
318 WINPR_API
void ArrayList_Free(wArrayList* arrayList);
320 WINPR_ATTR_MALLOC(ArrayList_Free, 1)
321 WINPR_API wArrayList* ArrayList_New(BOOL synchronized);
326 typedef struct s_wListDictionary wListDictionary;
335 WINPR_API
wObject* ListDictionary_KeyObject(wListDictionary* listDictionary);
344 WINPR_API
wObject* ListDictionary_ValueObject(wListDictionary* listDictionary);
353 WINPR_API
size_t ListDictionary_Count(wListDictionary* listDictionary);
359 WINPR_API
void ListDictionary_Lock(wListDictionary* listDictionary);
364 WINPR_API
void ListDictionary_Unlock(wListDictionary* listDictionary);
376 WINPR_API BOOL ListDictionary_Add(wListDictionary* listDictionary, const
void* key,
387 WINPR_API
void* ListDictionary_Take(wListDictionary* listDictionary, const
void* key);
394 WINPR_API
void ListDictionary_Remove(wListDictionary* listDictionary, const
void* key);
404 WINPR_API
void* ListDictionary_Take_Head(wListDictionary* listDictionary);
410 WINPR_API
void ListDictionary_Remove_Head(wListDictionary* listDictionary);
416 WINPR_API
void ListDictionary_Clear(wListDictionary* listDictionary);
426 WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, const
void* key);
437 WINPR_API
size_t ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys);
448 WINPR_API
void* ListDictionary_GetItemValue(wListDictionary* listDictionary, const
void* key);
460 WINPR_API BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, const
void* key,
467 WINPR_API
void ListDictionary_Free(wListDictionary* listDictionary);
475 WINPR_ATTR_MALLOC(ListDictionary_Free, 1)
476 WINPR_API wListDictionary* ListDictionary_New(BOOL synchronized);
480 typedef struct s_wLinkedList wLinkedList;
489 WINPR_API
size_t LinkedList_Count(wLinkedList* list);
498 WINPR_API
void* LinkedList_First(wLinkedList* list);
507 WINPR_API
void* LinkedList_Last(wLinkedList* list);
517 WINPR_API BOOL LinkedList_Contains(wLinkedList* list, const
void* value);
525 WINPR_API
void LinkedList_Clear(wLinkedList* list);
536 WINPR_API BOOL LinkedList_AddFirst(wLinkedList* list, const
void* value);
547 WINPR_API BOOL LinkedList_AddLast(wLinkedList* list, const
void* value);
557 WINPR_API BOOL LinkedList_Remove(wLinkedList* list, const
void* value);
565 WINPR_API
void LinkedList_RemoveFirst(wLinkedList* list);
573 WINPR_API
void LinkedList_RemoveLast(wLinkedList* list);
580 WINPR_API
void LinkedList_Enumerator_Reset(wLinkedList* list);
589 WINPR_API
void* LinkedList_Enumerator_Current(wLinkedList* list);
598 WINPR_API BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list);
604 WINPR_API
void LinkedList_Free(wLinkedList* list);
610 WINPR_ATTR_MALLOC(LinkedList_Free, 1)
611 WINPR_API wLinkedList* LinkedList_New(
void);
620 WINPR_API
wObject* LinkedList_Object(wLinkedList* list);
626 typedef struct CountdownEvent wCountdownEvent;
635 WINPR_API
size_t CountdownEvent_CurrentCount(wCountdownEvent* countdown);
644 WINPR_API
size_t CountdownEvent_InitialCount(wCountdownEvent* countdown);
653 WINPR_API BOOL CountdownEvent_IsSet(wCountdownEvent* countdown);
663 WINPR_API HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown);
671 WINPR_API
void CountdownEvent_AddCount(wCountdownEvent* countdown,
size_t signalCount);
680 WINPR_API BOOL CountdownEvent_Signal(wCountdownEvent* countdown,
size_t signalCount);
687 WINPR_API
void CountdownEvent_Reset(wCountdownEvent* countdown,
size_t count);
693 WINPR_API
void CountdownEvent_Free(wCountdownEvent* countdown);
701 WINPR_ATTR_MALLOC(CountdownEvent_Free, 1)
702 WINPR_API wCountdownEvent* CountdownEvent_New(
size_t initialCount);
706 typedef UINT32 (*HASH_TABLE_HASH_FN)(const
void* key);
708 typedef struct s_wHashTable wHashTable;
710 typedef BOOL (*HASH_TABLE_FOREACH_FN)(const
void* key,
void* value,
void* arg);
713 WINPR_API
size_t HashTable_Count(wHashTable* table);
715#if defined(WITH_WINPR_DEPRECATED)
716 WINPR_DEPRECATED(WINPR_ATTR_NODISCARD WINPR_API
int HashTable_Add(wHashTable* table,
722 WINPR_API BOOL HashTable_Insert(wHashTable* table,
const void* key,
const void* value);
724 WINPR_API BOOL HashTable_Remove(wHashTable* table,
const void* key);
726 WINPR_API
void HashTable_Clear(wHashTable* table);
729 WINPR_API BOOL HashTable_Contains(wHashTable* table,
const void* key);
732 WINPR_API BOOL HashTable_ContainsKey(wHashTable* table,
const void* key);
735 WINPR_API BOOL HashTable_ContainsValue(wHashTable* table,
const void* value);
738 WINPR_API
void* HashTable_GetItemValue(wHashTable* table,
const void* key);
741 WINPR_API BOOL HashTable_SetItemValue(wHashTable* table,
const void* key,
const void* value);
744 WINPR_API
size_t HashTable_GetKeys(wHashTable* table, ULONG_PTR** ppKeys);
747 WINPR_API BOOL HashTable_Foreach(wHashTable* table, HASH_TABLE_FOREACH_FN fn, VOID* arg);
750 WINPR_API UINT32 HashTable_PointerHash(
const void* pointer);
753 WINPR_API BOOL HashTable_PointerCompare(
const void* pointer1,
const void* pointer2);
756 WINPR_API UINT32 HashTable_StringHash(
const void* key);
759 WINPR_API BOOL HashTable_StringCompare(
const void* string1,
const void* string2);
762 WINPR_API
void* HashTable_StringClone(
const void* str);
764 WINPR_API
void HashTable_StringFree(
void* str);
766 WINPR_API
void HashTable_Free(wHashTable* table);
768 WINPR_ATTR_MALLOC(HashTable_Free, 1)
769 WINPR_API wHashTable* HashTable_New(BOOL synchronized);
771 WINPR_API
void HashTable_Lock(wHashTable* table);
772 WINPR_API
void HashTable_Unlock(wHashTable* table);
775 WINPR_API
wObject* HashTable_KeyObject(wHashTable* table);
778 WINPR_API
wObject* HashTable_ValueObject(wHashTable* table);
781 WINPR_API BOOL HashTable_SetHashFunction(wHashTable* table, HASH_TABLE_HASH_FN fn);
785 WINPR_API BOOL HashTable_SetupForStringData(wHashTable* table, BOOL stringValues);
789 typedef struct s_wBufferPool wBufferPool;
792 WINPR_API SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool);
795 WINPR_API SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const
void* buffer);
798 WINPR_API
void* BufferPool_Take(wBufferPool* pool, SSIZE_T bufferSize);
800 WINPR_API BOOL BufferPool_Return(wBufferPool* pool,
void* buffer);
802 WINPR_API
void BufferPool_Clear(wBufferPool* pool);
804 WINPR_API
void BufferPool_Free(wBufferPool* pool);
806 WINPR_ATTR_MALLOC(BufferPool_Free, 1)
807 WINPR_API wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment);
811 typedef struct s_wObjectPool wObjectPool;
815 WINPR_API
void* ObjectPool_Take(wObjectPool* pool);
816 WINPR_API
void ObjectPool_Return(wObjectPool* pool,
void* obj);
817 WINPR_API
void ObjectPool_Clear(wObjectPool* pool);
820 WINPR_API
wObject* ObjectPool_Object(wObjectPool* pool);
822 WINPR_API
void ObjectPool_Free(wObjectPool* pool);
824 WINPR_ATTR_MALLOC(ObjectPool_Free, 1)
825 WINPR_API wObjectPool* ObjectPool_New(BOOL synchronized);
831 typedef
void (*MESSAGE_FREE_FN)(wMessage* message);
840 MESSAGE_FREE_FN Free;
843 typedef struct s_wMessageQueue wMessageQueue;
845#define WMQ_QUIT 0xFFFFFFFF
848 WINPR_API
wObject* MessageQueue_Object(wMessageQueue* queue);
851 WINPR_API HANDLE MessageQueue_Event(wMessageQueue* queue);
854 WINPR_API BOOL MessageQueue_Wait(wMessageQueue* queue);
863 WINPR_API
size_t MessageQueue_Size(wMessageQueue* queue);
872 WINPR_API
size_t MessageQueue_Capacity(wMessageQueue* queue);
875 WINPR_API BOOL MessageQueue_Dispatch(wMessageQueue* queue,
const wMessage* message);
878 WINPR_API BOOL MessageQueue_Post(wMessageQueue* queue,
void* context, UINT32 type,
void* wParam,
881 WINPR_API BOOL MessageQueue_PostQuit(wMessageQueue* queue,
int nExitCode);
884 WINPR_API
int MessageQueue_Get(wMessageQueue* queue, wMessage* message);
887 WINPR_API
int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove);
899 WINPR_API
int MessageQueue_Clear(wMessageQueue* queue);
912 WINPR_API
void MessageQueue_Free(wMessageQueue* queue);
926 WINPR_ATTR_MALLOC(MessageQueue_Free, 1)
927 WINPR_API wMessageQueue* MessageQueue_New(const
wObject* callback);
937 WINPR_API
void MessagePipe_PostQuit(
wMessagePipe* pipe,
int nExitCode);
941 WINPR_ATTR_MALLOC(MessagePipe_Free, 1)
952 typedef void (*pEventHandler)(
void* context,
const wEventArgs* e);
955#define WINPR_EVENT_CAST(t, val) reinterpret_cast<t>(val)
957#define WINPR_EVENT_CAST(t, val) (t)(val)
960#define MAX_EVENT_HANDLERS 32
964 const char* EventName;
966 size_t EventHandlerCount;
967 pEventHandler EventHandlers[MAX_EVENT_HANDLERS];
970#define EventArgsInit(_event_args, _sender) \
971 memset(_event_args, 0, sizeof(*_event_args)); \
972 (_event_args)->e.Size = sizeof(*_event_args); \
973 (_event_args)->e.Sender = _sender
975#define DEFINE_EVENT_HANDLER(name) \
976 typedef void (*p##name##EventHandler)(void* context, const name##EventArgs* e)
978#define DEFINE_EVENT_RAISE(name) \
979 WINPR_ATTR_NODISCARD static inline int PubSub_On##name(wPubSub* pubSub, void* context, \
980 const name##EventArgs* e) \
983 return PubSub_OnEvent(pubSub, #name, context, &e->e); \
986#define DEFINE_EVENT_SUBSCRIBE(name) \
987 WINPR_ATTR_NODISCARD static inline int PubSub_Subscribe##name( \
988 wPubSub* pubSub, p##name##EventHandler EventHandler) \
990 return PubSub_Subscribe(pubSub, #name, EventHandler); \
993#define DEFINE_EVENT_UNSUBSCRIBE(name) \
994 static inline int PubSub_Unsubscribe##name(wPubSub* pubSub, \
995 p##name##EventHandler EventHandler) \
997 return PubSub_Unsubscribe(pubSub, #name, EventHandler); \
1000#define DEFINE_EVENT_BEGIN(name) \
1005#define DEFINE_EVENT_END(name) \
1008 DEFINE_EVENT_HANDLER(name); \
1009 DEFINE_EVENT_RAISE(name) \
1010 DEFINE_EVENT_SUBSCRIBE(name) \
1011 DEFINE_EVENT_UNSUBSCRIBE(name)
1013#define DEFINE_EVENT_ENTRY(name) \
1015 #name, { sizeof(name##EventArgs), nullptr }, 0, \
1021 typedef struct s_wPubSub wPubSub;
1023 WINPR_API
void PubSub_Lock(wPubSub* pubSub);
1024 WINPR_API
void PubSub_Unlock(wPubSub* pubSub);
1026 WINPR_ATTR_NODISCARD
1027 WINPR_API
wEventType* PubSub_GetEventTypes(wPubSub* pubSub,
size_t* count);
1029 WINPR_API
void PubSub_AddEventTypes(wPubSub* pubSub,
wEventType* events,
size_t count);
1031 WINPR_ATTR_NODISCARD
1032 WINPR_API
wEventType* PubSub_FindEventType(wPubSub* pubSub,
const char* EventName);
1034 WINPR_ATTR_NODISCARD
1035 WINPR_API
int PubSub_Subscribe(wPubSub* pubSub,
const char* EventName, ...);
1037 WINPR_API
int PubSub_Unsubscribe(wPubSub* pubSub,
const char* EventName, ...);
1039 WINPR_ATTR_NODISCARD
1040 WINPR_API
int PubSub_OnEvent(wPubSub* pubSub,
const char* EventName,
void* context,
1043 WINPR_API
void PubSub_Free(wPubSub* pubSub);
1045 WINPR_ATTR_MALLOC(PubSub_Free, 1)
1046 WINPR_API wPubSub* PubSub_New(BOOL synchronized);
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
OBJECT_UNINIT_FN fnObjectUninit
WINPR_ATTR_NODISCARD OBJECT_EQUALS_FN fnObjectEquals
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew
OBJECT_INIT_FN fnObjectInit