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);
100 WINPR_API
void Queue_Lock(wQueue* queue);
106 WINPR_API
void Queue_Unlock(wQueue* queue);
113 WINPR_API HANDLE Queue_Event(wQueue* queue);
122 WINPR_API
wObject* Queue_Object(wQueue* queue);
128 WINPR_API
void Queue_Clear(wQueue* queue);
137 WINPR_API BOOL Queue_Contains(wQueue* queue,
const void* obj);
148 WINPR_API BOOL Queue_Enqueue(wQueue* queue,
const void* obj);
157 WINPR_API
void* Queue_Dequeue(wQueue* queue);
166 WINPR_API
void* Queue_Peek(wQueue* queue);
175 WINPR_API
void Queue_Discard(wQueue* queue);
181 WINPR_API
void Queue_Free(wQueue* queue);
187 WINPR_ATTR_MALLOC(Queue_Free, 1)
189 WINPR_API wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor);
193 typedef struct s_wStack wStack;
195 WINPR_API
size_t Stack_Count(wStack* stack);
196 WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
198 WINPR_API
wObject* Stack_Object(wStack* stack);
200 WINPR_API
void Stack_Clear(wStack* stack);
201 WINPR_API BOOL Stack_Contains(wStack* stack, const
void* obj);
203 WINPR_API
void Stack_Push(wStack* stack,
void* obj);
204 WINPR_API
void* Stack_Pop(wStack* stack);
206 WINPR_API
void* Stack_Peek(wStack* stack);
208 WINPR_API
void Stack_Free(wStack* stack);
210 WINPR_ATTR_MALLOC(Stack_Free, 1)
212 WINPR_API wStack* Stack_New(BOOL synchronized);
216 typedef struct s_wArrayList wArrayList;
218 WINPR_API
size_t ArrayList_Capacity(wArrayList* arrayList);
219 WINPR_API
size_t ArrayList_Count(wArrayList* arrayList);
220 WINPR_API
size_t ArrayList_Items(wArrayList* arrayList, ULONG_PTR** ppItems);
221 WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList);
222 WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList);
223 WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList);
225 WINPR_API
void ArrayList_Lock(wArrayList* arrayList);
226 WINPR_API
void ArrayList_Unlock(wArrayList* arrayList);
228 WINPR_API
void* ArrayList_GetItem(wArrayList* arrayList,
size_t index);
229 WINPR_API BOOL ArrayList_SetItem(wArrayList* arrayList,
size_t index, const
void* obj);
231 WINPR_API
wObject* ArrayList_Object(wArrayList* arrayList);
233 typedef BOOL (*ArrayList_ForEachFkt)(
void* data,
size_t index, va_list ap);
235 WINPR_API BOOL ArrayList_ForEach(wArrayList* arrayList, ArrayList_ForEachFkt fkt, ...);
236 WINPR_API BOOL ArrayList_ForEachAP(wArrayList* arrayList, ArrayList_ForEachFkt fkt, va_list ap);
238 WINPR_API
void ArrayList_Clear(wArrayList* arrayList);
239 WINPR_API BOOL ArrayList_Contains(wArrayList* arrayList, const
void* obj);
241#if defined(WITH_WINPR_DEPRECATED)
242 WINPR_DEPRECATED(WINPR_API
int ArrayList_Add(wArrayList* arrayList,
const void* obj));
245 WINPR_API BOOL ArrayList_Append(wArrayList* arrayList,
const void* obj);
246 WINPR_API BOOL ArrayList_Insert(wArrayList* arrayList,
size_t index,
const void* obj);
248 WINPR_API BOOL ArrayList_Remove(wArrayList* arrayList,
const void* obj);
249 WINPR_API BOOL ArrayList_RemoveAt(wArrayList* arrayList,
size_t index);
251 WINPR_API SSIZE_T ArrayList_IndexOf(wArrayList* arrayList,
const void* obj, SSIZE_T startIndex,
253 WINPR_API SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList,
const void* obj,
254 SSIZE_T startIndex, SSIZE_T count);
256 WINPR_API
void ArrayList_Free(wArrayList* arrayList);
258 WINPR_ATTR_MALLOC(ArrayList_Free, 1)
260 WINPR_API wArrayList* ArrayList_New(BOOL synchronized);
265 typedef struct s_wListDictionary wListDictionary;
273 WINPR_API
wObject* ListDictionary_KeyObject(wListDictionary* listDictionary);
281 WINPR_API
wObject* ListDictionary_ValueObject(wListDictionary* listDictionary);
289 WINPR_API
size_t ListDictionary_Count(wListDictionary* listDictionary);
295 WINPR_API
void ListDictionary_Lock(wListDictionary* listDictionary);
300 WINPR_API
void ListDictionary_Unlock(wListDictionary* listDictionary);
311 WINPR_API BOOL ListDictionary_Add(wListDictionary* listDictionary, const
void* key,
321 WINPR_API
void* ListDictionary_Take(wListDictionary* listDictionary, const
void* key);
328 WINPR_API
void ListDictionary_Remove(wListDictionary* listDictionary, const
void* key);
337 WINPR_API
void* ListDictionary_Take_Head(wListDictionary* listDictionary);
343 WINPR_API
void ListDictionary_Remove_Head(wListDictionary* listDictionary);
349 WINPR_API
void ListDictionary_Clear(wListDictionary* listDictionary);
358 WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, const
void* key);
368 WINPR_API
size_t ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys);
378 WINPR_API
void* ListDictionary_GetItemValue(wListDictionary* listDictionary, const
void* key);
389 WINPR_API BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, const
void* key,
396 WINPR_API
void ListDictionary_Free(wListDictionary* listDictionary);
404 WINPR_ATTR_MALLOC(ListDictionary_Free, 1)
406 WINPR_API wListDictionary* ListDictionary_New(BOOL synchronized);
410 typedef struct s_wLinkedList wLinkedList;
418 WINPR_API
size_t LinkedList_Count(wLinkedList* list);
426 WINPR_API
void* LinkedList_First(wLinkedList* list);
434 WINPR_API
void* LinkedList_Last(wLinkedList* list);
443 WINPR_API BOOL LinkedList_Contains(wLinkedList* list, const
void* value);
451 WINPR_API
void LinkedList_Clear(wLinkedList* list);
461 WINPR_API BOOL LinkedList_AddFirst(wLinkedList* list, const
void* value);
471 WINPR_API BOOL LinkedList_AddLast(wLinkedList* list, const
void* value);
481 WINPR_API BOOL LinkedList_Remove(wLinkedList* list, const
void* value);
489 WINPR_API
void LinkedList_RemoveFirst(wLinkedList* list);
497 WINPR_API
void LinkedList_RemoveLast(wLinkedList* list);
504 WINPR_API
void LinkedList_Enumerator_Reset(wLinkedList* list);
512 WINPR_API
void* LinkedList_Enumerator_Current(wLinkedList* list);
520 WINPR_API BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list);
526 WINPR_API
void LinkedList_Free(wLinkedList* list);
532 WINPR_ATTR_MALLOC(LinkedList_Free, 1)
534 WINPR_API wLinkedList* LinkedList_New(
void);
542 WINPR_API
wObject* LinkedList_Object(wLinkedList* list);
548 typedef struct CountdownEvent wCountdownEvent;
556 WINPR_API
size_t CountdownEvent_CurrentCount(wCountdownEvent* countdown);
564 WINPR_API
size_t CountdownEvent_InitialCount(wCountdownEvent* countdown);
572 WINPR_API BOOL CountdownEvent_IsSet(wCountdownEvent* countdown);
581 WINPR_API HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown);
589 WINPR_API
void CountdownEvent_AddCount(wCountdownEvent* countdown,
size_t signalCount);
598 WINPR_API BOOL CountdownEvent_Signal(wCountdownEvent* countdown,
size_t signalCount);
605 WINPR_API
void CountdownEvent_Reset(wCountdownEvent* countdown,
size_t count);
611 WINPR_API
void CountdownEvent_Free(wCountdownEvent* countdown);
619 WINPR_ATTR_MALLOC(CountdownEvent_Free, 1)
621 WINPR_API wCountdownEvent* CountdownEvent_New(
size_t initialCount);
625 typedef UINT32 (*HASH_TABLE_HASH_FN)(const
void* key);
627 typedef struct s_wHashTable wHashTable;
629 typedef BOOL (*HASH_TABLE_FOREACH_FN)(const
void* key,
void* value,
void* arg);
631 WINPR_API
size_t HashTable_Count(wHashTable* table);
633#if defined(WITH_WINPR_DEPRECATED)
634 WINPR_DEPRECATED(WINPR_API
int HashTable_Add(wHashTable* table,
const void* key,
638 WINPR_API BOOL HashTable_Insert(wHashTable* table,
const void* key,
const void* value);
639 WINPR_API BOOL HashTable_Remove(wHashTable* table,
const void* key);
640 WINPR_API
void HashTable_Clear(wHashTable* table);
641 WINPR_API BOOL HashTable_Contains(wHashTable* table,
const void* key);
642 WINPR_API BOOL HashTable_ContainsKey(wHashTable* table,
const void* key);
643 WINPR_API BOOL HashTable_ContainsValue(wHashTable* table,
const void* value);
644 WINPR_API
void* HashTable_GetItemValue(wHashTable* table,
const void* key);
645 WINPR_API BOOL HashTable_SetItemValue(wHashTable* table,
const void* key,
const void* value);
646 WINPR_API
size_t HashTable_GetKeys(wHashTable* table, ULONG_PTR** ppKeys);
647 WINPR_API BOOL HashTable_Foreach(wHashTable* table, HASH_TABLE_FOREACH_FN fn, VOID* arg);
649 WINPR_API UINT32 HashTable_PointerHash(
const void* pointer);
650 WINPR_API BOOL HashTable_PointerCompare(
const void* pointer1,
const void* pointer2);
652 WINPR_API UINT32 HashTable_StringHash(
const void* key);
653 WINPR_API BOOL HashTable_StringCompare(
const void* string1,
const void* string2);
654 WINPR_API
void* HashTable_StringClone(
const void* str);
655 WINPR_API
void HashTable_StringFree(
void* str);
657 WINPR_API
void HashTable_Free(wHashTable* table);
659 WINPR_ATTR_MALLOC(HashTable_Free, 1)
661 WINPR_API wHashTable* HashTable_New(BOOL synchronized);
663 WINPR_API
void HashTable_Lock(wHashTable* table);
664 WINPR_API
void HashTable_Unlock(wHashTable* table);
666 WINPR_API
wObject* HashTable_KeyObject(wHashTable* table);
667 WINPR_API
wObject* HashTable_ValueObject(wHashTable* table);
669 WINPR_API BOOL HashTable_SetHashFunction(wHashTable* table, HASH_TABLE_HASH_FN fn);
672 WINPR_API BOOL HashTable_SetupForStringData(wHashTable* table, BOOL stringValues);
676 typedef struct s_wBufferPool wBufferPool;
678 WINPR_API SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool);
679 WINPR_API SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const
void* buffer);
681 WINPR_API
void* BufferPool_Take(wBufferPool* pool, SSIZE_T bufferSize);
682 WINPR_API BOOL BufferPool_Return(wBufferPool* pool,
void* buffer);
683 WINPR_API
void BufferPool_Clear(wBufferPool* pool);
685 WINPR_API
void BufferPool_Free(wBufferPool* pool);
687 WINPR_ATTR_MALLOC(BufferPool_Free, 1)
689 WINPR_API wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment);
693 typedef struct s_wObjectPool wObjectPool;
695 WINPR_API
void* ObjectPool_Take(wObjectPool* pool);
696 WINPR_API
void ObjectPool_Return(wObjectPool* pool,
void* obj);
697 WINPR_API
void ObjectPool_Clear(wObjectPool* pool);
699 WINPR_API
wObject* ObjectPool_Object(wObjectPool* pool);
701 WINPR_API
void ObjectPool_Free(wObjectPool* pool);
703 WINPR_ATTR_MALLOC(ObjectPool_Free, 1)
705 WINPR_API wObjectPool* ObjectPool_New(BOOL synchronized);
711 typedef
void (*MESSAGE_FREE_FN)(wMessage* message);
720 MESSAGE_FREE_FN Free;
723 typedef struct s_wMessageQueue wMessageQueue;
725#define WMQ_QUIT 0xFFFFFFFF
727 WINPR_API
wObject* MessageQueue_Object(wMessageQueue* queue);
728 WINPR_API HANDLE MessageQueue_Event(wMessageQueue* queue);
729 WINPR_API BOOL MessageQueue_Wait(wMessageQueue* queue);
730 WINPR_API
size_t MessageQueue_Size(wMessageQueue* queue);
732 WINPR_API BOOL MessageQueue_Dispatch(wMessageQueue* queue,
const wMessage* message);
733 WINPR_API BOOL MessageQueue_Post(wMessageQueue* queue,
void* context, UINT32 type,
void* wParam,
735 WINPR_API BOOL MessageQueue_PostQuit(wMessageQueue* queue,
int nExitCode);
737 WINPR_API
int MessageQueue_Get(wMessageQueue* queue, wMessage* message);
738 WINPR_API
int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove);
750 WINPR_API
int MessageQueue_Clear(wMessageQueue* queue);
763 WINPR_API
void MessageQueue_Free(wMessageQueue* queue);
777 WINPR_ATTR_MALLOC(MessageQueue_Free, 1)
779 WINPR_API wMessageQueue* MessageQueue_New(const
wObject* callback);
789 WINPR_API
void MessagePipe_PostQuit(
wMessagePipe* pipe,
int nExitCode);
793 WINPR_ATTR_MALLOC(MessagePipe_Free, 1)
805 typedef void (*pEventHandler)(
void* context,
const wEventArgs* e);
808#define WINPR_EVENT_CAST(t, val) reinterpret_cast<t>(val)
810#define WINPR_EVENT_CAST(t, val) (t)(val)
813#define MAX_EVENT_HANDLERS 32
817 const char* EventName;
819 size_t EventHandlerCount;
820 pEventHandler EventHandlers[MAX_EVENT_HANDLERS];
823#define EventArgsInit(_event_args, _sender) \
824 memset(_event_args, 0, sizeof(*_event_args)); \
825 (_event_args)->e.Size = sizeof(*_event_args); \
826 (_event_args)->e.Sender = _sender
828#define DEFINE_EVENT_HANDLER(name) \
829 typedef void (*p##name##EventHandler)(void* context, const name##EventArgs* e)
831#define DEFINE_EVENT_RAISE(name) \
832 static inline int PubSub_On##name(wPubSub* pubSub, void* context, const name##EventArgs* e) \
835 return PubSub_OnEvent(pubSub, #name, context, &e->e); \
838#define DEFINE_EVENT_SUBSCRIBE(name) \
839 static inline int PubSub_Subscribe##name(wPubSub* pubSub, p##name##EventHandler EventHandler) \
841 return PubSub_Subscribe(pubSub, #name, EventHandler); \
844#define DEFINE_EVENT_UNSUBSCRIBE(name) \
845 static inline int PubSub_Unsubscribe##name(wPubSub* pubSub, \
846 p##name##EventHandler EventHandler) \
848 return PubSub_Unsubscribe(pubSub, #name, EventHandler); \
851#define DEFINE_EVENT_BEGIN(name) \
856#define DEFINE_EVENT_END(name) \
859 DEFINE_EVENT_HANDLER(name); \
860 DEFINE_EVENT_RAISE(name) \
861 DEFINE_EVENT_SUBSCRIBE(name) \
862 DEFINE_EVENT_UNSUBSCRIBE(name)
864#define DEFINE_EVENT_ENTRY(name) \
866#name, { sizeof(name##EventArgs), NULL }, 0, \
872 typedef struct s_wPubSub wPubSub;
874 WINPR_API
void PubSub_Lock(wPubSub* pubSub);
875 WINPR_API
void PubSub_Unlock(wPubSub* pubSub);
877 WINPR_API
wEventType* PubSub_GetEventTypes(wPubSub* pubSub,
size_t* count);
878 WINPR_API
void PubSub_AddEventTypes(wPubSub* pubSub,
wEventType* events,
size_t count);
879 WINPR_API
wEventType* PubSub_FindEventType(wPubSub* pubSub,
const char* EventName);
881 WINPR_API
int PubSub_Subscribe(wPubSub* pubSub,
const char* EventName, ...);
882 WINPR_API
int PubSub_Unsubscribe(wPubSub* pubSub,
const char* EventName, ...);
884 WINPR_API
int PubSub_OnEvent(wPubSub* pubSub,
const char* EventName,
void* context,
887 WINPR_API
void PubSub_Free(wPubSub* pubSub);
889 WINPR_ATTR_MALLOC(PubSub_Free, 1)
891 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