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);
69 WINPR_API
void* winpr_ObjectStringClone(
const void* pvstr);
76 WINPR_API
void* winpr_ObjectWStringClone(
const void* pvstr);
82 WINPR_API
void winpr_ObjectStringFree(
void* pvstr);
86 typedef struct s_wQueue wQueue;
94 WINPR_API
size_t Queue_Count(wQueue* queue);
102 WINPR_API
size_t Queue_Capacity(wQueue* queue);
108 WINPR_API
void Queue_Lock(wQueue* queue);
114 WINPR_API
void Queue_Unlock(wQueue* queue);
121 WINPR_API HANDLE Queue_Event(wQueue* queue);
130 WINPR_API
wObject* Queue_Object(wQueue* queue);
136 WINPR_API
void Queue_Clear(wQueue* queue);
145 WINPR_API BOOL Queue_Contains(wQueue* queue,
const void* obj);
156 WINPR_API BOOL Queue_Enqueue(wQueue* queue,
const void* obj);
165 WINPR_API
void* Queue_Dequeue(wQueue* queue);
174 WINPR_API
void* Queue_Peek(wQueue* queue);
183 WINPR_API
void Queue_Discard(wQueue* queue);
189 WINPR_API
void Queue_Free(wQueue* queue);
203 WINPR_ATTR_MALLOC(Queue_Free, 1)
205 WINPR_API wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor);
209 typedef struct s_wStack wStack;
211 WINPR_API
size_t Stack_Count(wStack* stack);
212 WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
214 WINPR_API
wObject* Stack_Object(wStack* stack);
216 WINPR_API
void Stack_Clear(wStack* stack);
217 WINPR_API BOOL Stack_Contains(wStack* stack, const
void* obj);
219 WINPR_API
void Stack_Push(wStack* stack,
void* obj);
220 WINPR_API
void* Stack_Pop(wStack* stack);
222 WINPR_API
void* Stack_Peek(wStack* stack);
224 WINPR_API
void Stack_Free(wStack* stack);
226 WINPR_ATTR_MALLOC(Stack_Free, 1)
228 WINPR_API wStack* Stack_New(BOOL synchronized);
232 typedef struct s_wArrayList wArrayList;
234 WINPR_API
size_t ArrayList_Capacity(wArrayList* arrayList);
235 WINPR_API
size_t ArrayList_Count(wArrayList* arrayList);
236 WINPR_API
size_t ArrayList_Items(wArrayList* arrayList, ULONG_PTR** ppItems);
237 WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList);
238 WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList);
239 WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList);
241 WINPR_API
void ArrayList_Lock(wArrayList* arrayList);
242 WINPR_API
void ArrayList_Unlock(wArrayList* arrayList);
244 WINPR_API
void* ArrayList_GetItem(wArrayList* arrayList,
size_t index);
245 WINPR_API BOOL ArrayList_SetItem(wArrayList* arrayList,
size_t index, const
void* obj);
247 WINPR_API
wObject* ArrayList_Object(wArrayList* arrayList);
249 typedef BOOL (*ArrayList_ForEachFkt)(
void* data,
size_t index, va_list ap);
251 WINPR_API BOOL ArrayList_ForEach(wArrayList* arrayList, ArrayList_ForEachFkt fkt, ...);
252 WINPR_API BOOL ArrayList_ForEachAP(wArrayList* arrayList, ArrayList_ForEachFkt fkt, va_list ap);
254 WINPR_API
void ArrayList_Clear(wArrayList* arrayList);
255 WINPR_API BOOL ArrayList_Contains(wArrayList* arrayList, const
void* obj);
257#if defined(WITH_WINPR_DEPRECATED)
258 WINPR_DEPRECATED(WINPR_API
int ArrayList_Add(wArrayList* arrayList,
const void* obj));
261 WINPR_API BOOL ArrayList_Append(wArrayList* arrayList,
const void* obj);
262 WINPR_API BOOL ArrayList_Insert(wArrayList* arrayList,
size_t index,
const void* obj);
264 WINPR_API BOOL ArrayList_Remove(wArrayList* arrayList,
const void* obj);
265 WINPR_API BOOL ArrayList_RemoveAt(wArrayList* arrayList,
size_t index);
267 WINPR_API SSIZE_T ArrayList_IndexOf(wArrayList* arrayList,
const void* obj, SSIZE_T startIndex,
269 WINPR_API SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList,
const void* obj,
270 SSIZE_T startIndex, SSIZE_T count);
272 WINPR_API
void ArrayList_Free(wArrayList* arrayList);
274 WINPR_ATTR_MALLOC(ArrayList_Free, 1)
276 WINPR_API wArrayList* ArrayList_New(BOOL synchronized);
281 typedef struct s_wListDictionary wListDictionary;
289 WINPR_API
wObject* ListDictionary_KeyObject(wListDictionary* listDictionary);
297 WINPR_API
wObject* ListDictionary_ValueObject(wListDictionary* listDictionary);
305 WINPR_API
size_t ListDictionary_Count(wListDictionary* listDictionary);
311 WINPR_API
void ListDictionary_Lock(wListDictionary* listDictionary);
316 WINPR_API
void ListDictionary_Unlock(wListDictionary* listDictionary);
327 WINPR_API BOOL ListDictionary_Add(wListDictionary* listDictionary, const
void* key,
337 WINPR_API
void* ListDictionary_Take(wListDictionary* listDictionary, const
void* key);
344 WINPR_API
void ListDictionary_Remove(wListDictionary* listDictionary, const
void* key);
353 WINPR_API
void* ListDictionary_Take_Head(wListDictionary* listDictionary);
359 WINPR_API
void ListDictionary_Remove_Head(wListDictionary* listDictionary);
365 WINPR_API
void ListDictionary_Clear(wListDictionary* listDictionary);
374 WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, const
void* key);
384 WINPR_API
size_t ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys);
394 WINPR_API
void* ListDictionary_GetItemValue(wListDictionary* listDictionary, const
void* key);
405 WINPR_API BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, const
void* key,
412 WINPR_API
void ListDictionary_Free(wListDictionary* listDictionary);
420 WINPR_ATTR_MALLOC(ListDictionary_Free, 1)
422 WINPR_API wListDictionary* ListDictionary_New(BOOL synchronized);
426 typedef struct s_wLinkedList wLinkedList;
434 WINPR_API
size_t LinkedList_Count(wLinkedList* list);
442 WINPR_API
void* LinkedList_First(wLinkedList* list);
450 WINPR_API
void* LinkedList_Last(wLinkedList* list);
459 WINPR_API BOOL LinkedList_Contains(wLinkedList* list, const
void* value);
467 WINPR_API
void LinkedList_Clear(wLinkedList* list);
477 WINPR_API BOOL LinkedList_AddFirst(wLinkedList* list, const
void* value);
487 WINPR_API BOOL LinkedList_AddLast(wLinkedList* list, const
void* value);
497 WINPR_API BOOL LinkedList_Remove(wLinkedList* list, const
void* value);
505 WINPR_API
void LinkedList_RemoveFirst(wLinkedList* list);
513 WINPR_API
void LinkedList_RemoveLast(wLinkedList* list);
520 WINPR_API
void LinkedList_Enumerator_Reset(wLinkedList* list);
528 WINPR_API
void* LinkedList_Enumerator_Current(wLinkedList* list);
536 WINPR_API BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list);
542 WINPR_API
void LinkedList_Free(wLinkedList* list);
548 WINPR_ATTR_MALLOC(LinkedList_Free, 1)
550 WINPR_API wLinkedList* LinkedList_New(
void);
558 WINPR_API
wObject* LinkedList_Object(wLinkedList* list);
564 typedef struct CountdownEvent wCountdownEvent;
572 WINPR_API
size_t CountdownEvent_CurrentCount(wCountdownEvent* countdown);
580 WINPR_API
size_t CountdownEvent_InitialCount(wCountdownEvent* countdown);
588 WINPR_API BOOL CountdownEvent_IsSet(wCountdownEvent* countdown);
597 WINPR_API HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown);
605 WINPR_API
void CountdownEvent_AddCount(wCountdownEvent* countdown,
size_t signalCount);
614 WINPR_API BOOL CountdownEvent_Signal(wCountdownEvent* countdown,
size_t signalCount);
621 WINPR_API
void CountdownEvent_Reset(wCountdownEvent* countdown,
size_t count);
627 WINPR_API
void CountdownEvent_Free(wCountdownEvent* countdown);
635 WINPR_ATTR_MALLOC(CountdownEvent_Free, 1)
637 WINPR_API wCountdownEvent* CountdownEvent_New(
size_t initialCount);
641 typedef UINT32 (*HASH_TABLE_HASH_FN)(const
void* key);
643 typedef struct s_wHashTable wHashTable;
645 typedef BOOL (*HASH_TABLE_FOREACH_FN)(const
void* key,
void* value,
void* arg);
647 WINPR_API
size_t HashTable_Count(wHashTable* table);
649#if defined(WITH_WINPR_DEPRECATED)
650 WINPR_DEPRECATED(WINPR_API
int HashTable_Add(wHashTable* table,
const void* key,
654 WINPR_API BOOL HashTable_Insert(wHashTable* table,
const void* key,
const void* value);
655 WINPR_API BOOL HashTable_Remove(wHashTable* table,
const void* key);
656 WINPR_API
void HashTable_Clear(wHashTable* table);
657 WINPR_API BOOL HashTable_Contains(wHashTable* table,
const void* key);
658 WINPR_API BOOL HashTable_ContainsKey(wHashTable* table,
const void* key);
659 WINPR_API BOOL HashTable_ContainsValue(wHashTable* table,
const void* value);
660 WINPR_API
void* HashTable_GetItemValue(wHashTable* table,
const void* key);
661 WINPR_API BOOL HashTable_SetItemValue(wHashTable* table,
const void* key,
const void* value);
662 WINPR_API
size_t HashTable_GetKeys(wHashTable* table, ULONG_PTR** ppKeys);
663 WINPR_API BOOL HashTable_Foreach(wHashTable* table, HASH_TABLE_FOREACH_FN fn, VOID* arg);
665 WINPR_API UINT32 HashTable_PointerHash(
const void* pointer);
666 WINPR_API BOOL HashTable_PointerCompare(
const void* pointer1,
const void* pointer2);
668 WINPR_API UINT32 HashTable_StringHash(
const void* key);
669 WINPR_API BOOL HashTable_StringCompare(
const void* string1,
const void* string2);
670 WINPR_API
void* HashTable_StringClone(
const void* str);
671 WINPR_API
void HashTable_StringFree(
void* str);
673 WINPR_API
void HashTable_Free(wHashTable* table);
675 WINPR_ATTR_MALLOC(HashTable_Free, 1)
677 WINPR_API wHashTable* HashTable_New(BOOL synchronized);
679 WINPR_API
void HashTable_Lock(wHashTable* table);
680 WINPR_API
void HashTable_Unlock(wHashTable* table);
682 WINPR_API
wObject* HashTable_KeyObject(wHashTable* table);
683 WINPR_API
wObject* HashTable_ValueObject(wHashTable* table);
685 WINPR_API BOOL HashTable_SetHashFunction(wHashTable* table, HASH_TABLE_HASH_FN fn);
688 WINPR_API BOOL HashTable_SetupForStringData(wHashTable* table, BOOL stringValues);
692 typedef struct s_wBufferPool wBufferPool;
694 WINPR_API SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool);
695 WINPR_API SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const
void* buffer);
697 WINPR_API
void* BufferPool_Take(wBufferPool* pool, SSIZE_T bufferSize);
698 WINPR_API BOOL BufferPool_Return(wBufferPool* pool,
void* buffer);
699 WINPR_API
void BufferPool_Clear(wBufferPool* pool);
701 WINPR_API
void BufferPool_Free(wBufferPool* pool);
703 WINPR_ATTR_MALLOC(BufferPool_Free, 1)
705 WINPR_API wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment);
709 typedef struct s_wObjectPool wObjectPool;
711 WINPR_API
void* ObjectPool_Take(wObjectPool* pool);
712 WINPR_API
void ObjectPool_Return(wObjectPool* pool,
void* obj);
713 WINPR_API
void ObjectPool_Clear(wObjectPool* pool);
715 WINPR_API
wObject* ObjectPool_Object(wObjectPool* pool);
717 WINPR_API
void ObjectPool_Free(wObjectPool* pool);
719 WINPR_ATTR_MALLOC(ObjectPool_Free, 1)
721 WINPR_API wObjectPool* ObjectPool_New(BOOL synchronized);
727 typedef
void (*MESSAGE_FREE_FN)(wMessage* message);
736 MESSAGE_FREE_FN Free;
739 typedef struct s_wMessageQueue wMessageQueue;
741#define WMQ_QUIT 0xFFFFFFFF
743 WINPR_API
wObject* MessageQueue_Object(wMessageQueue* queue);
744 WINPR_API HANDLE MessageQueue_Event(wMessageQueue* queue);
745 WINPR_API BOOL MessageQueue_Wait(wMessageQueue* queue);
753 WINPR_API
size_t MessageQueue_Size(wMessageQueue* queue);
761 WINPR_API
size_t MessageQueue_Capacity(wMessageQueue* queue);
763 WINPR_API BOOL MessageQueue_Dispatch(wMessageQueue* queue,
const wMessage* message);
764 WINPR_API BOOL MessageQueue_Post(wMessageQueue* queue,
void* context, UINT32 type,
void* wParam,
766 WINPR_API BOOL MessageQueue_PostQuit(wMessageQueue* queue,
int nExitCode);
768 WINPR_API
int MessageQueue_Get(wMessageQueue* queue, wMessage* message);
769 WINPR_API
int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove);
781 WINPR_API
int MessageQueue_Clear(wMessageQueue* queue);
794 WINPR_API
void MessageQueue_Free(wMessageQueue* queue);
808 WINPR_ATTR_MALLOC(MessageQueue_Free, 1)
810 WINPR_API wMessageQueue* MessageQueue_New(const
wObject* callback);
820 WINPR_API
void MessagePipe_PostQuit(
wMessagePipe* pipe,
int nExitCode);
824 WINPR_ATTR_MALLOC(MessagePipe_Free, 1)
836 typedef void (*pEventHandler)(
void* context,
const wEventArgs* e);
839#define WINPR_EVENT_CAST(t, val) reinterpret_cast<t>(val)
841#define WINPR_EVENT_CAST(t, val) (t)(val)
844#define MAX_EVENT_HANDLERS 32
848 const char* EventName;
850 size_t EventHandlerCount;
851 pEventHandler EventHandlers[MAX_EVENT_HANDLERS];
854#define EventArgsInit(_event_args, _sender) \
855 memset(_event_args, 0, sizeof(*_event_args)); \
856 (_event_args)->e.Size = sizeof(*_event_args); \
857 (_event_args)->e.Sender = _sender
859#define DEFINE_EVENT_HANDLER(name) \
860 typedef void (*p##name##EventHandler)(void* context, const name##EventArgs* e)
862#define DEFINE_EVENT_RAISE(name) \
863 static inline int PubSub_On##name(wPubSub* pubSub, void* context, const name##EventArgs* e) \
866 return PubSub_OnEvent(pubSub, #name, context, &e->e); \
869#define DEFINE_EVENT_SUBSCRIBE(name) \
870 static inline int PubSub_Subscribe##name(wPubSub* pubSub, p##name##EventHandler EventHandler) \
872 return PubSub_Subscribe(pubSub, #name, EventHandler); \
875#define DEFINE_EVENT_UNSUBSCRIBE(name) \
876 static inline int PubSub_Unsubscribe##name(wPubSub* pubSub, \
877 p##name##EventHandler EventHandler) \
879 return PubSub_Unsubscribe(pubSub, #name, EventHandler); \
882#define DEFINE_EVENT_BEGIN(name) \
887#define DEFINE_EVENT_END(name) \
890 DEFINE_EVENT_HANDLER(name); \
891 DEFINE_EVENT_RAISE(name) \
892 DEFINE_EVENT_SUBSCRIBE(name) \
893 DEFINE_EVENT_UNSUBSCRIBE(name)
895#define DEFINE_EVENT_ENTRY(name) \
897 #name, { sizeof(name##EventArgs), nullptr }, 0, \
903 typedef struct s_wPubSub wPubSub;
905 WINPR_API
void PubSub_Lock(wPubSub* pubSub);
906 WINPR_API
void PubSub_Unlock(wPubSub* pubSub);
908 WINPR_API
wEventType* PubSub_GetEventTypes(wPubSub* pubSub,
size_t* count);
909 WINPR_API
void PubSub_AddEventTypes(wPubSub* pubSub,
wEventType* events,
size_t count);
910 WINPR_API
wEventType* PubSub_FindEventType(wPubSub* pubSub,
const char* EventName);
912 WINPR_API
int PubSub_Subscribe(wPubSub* pubSub,
const char* EventName, ...);
913 WINPR_API
int PubSub_Unsubscribe(wPubSub* pubSub,
const char* EventName, ...);
915 WINPR_API
int PubSub_OnEvent(wPubSub* pubSub,
const char* EventName,
void* context,
918 WINPR_API
void PubSub_Free(wPubSub* pubSub);
920 WINPR_ATTR_MALLOC(PubSub_Free, 1)
922 WINPR_API wPubSub* PubSub_New(BOOL synchronized);
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
OBJECT_EQUALS_FN fnObjectEquals
OBJECT_UNINIT_FN fnObjectUninit
OBJECT_INIT_FN fnObjectInit
OBJECT_NEW_FN fnObjectNew