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>
32 #include <winpr/crt.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);
58 OBJECT_NEW_FN fnObjectNew;
59 OBJECT_INIT_FN fnObjectInit;
60 OBJECT_UNINIT_FN fnObjectUninit;
61 OBJECT_FREE_FN fnObjectFree;
62 OBJECT_EQUALS_FN fnObjectEquals;
72 WINPR_API
void* winpr_ObjectStringClone(
const void* pvstr);
79 WINPR_API
void* winpr_ObjectWStringClone(
const void* pvstr);
85 WINPR_API
void winpr_ObjectStringFree(
void* pvstr);
89 typedef struct s_wQueue wQueue;
97 WINPR_API
size_t Queue_Count(wQueue* queue);
103 WINPR_API
void Queue_Lock(wQueue* queue);
109 WINPR_API
void Queue_Unlock(wQueue* queue);
116 WINPR_API HANDLE Queue_Event(wQueue* queue);
125 WINPR_API
wObject* Queue_Object(wQueue* queue);
131 WINPR_API
void Queue_Clear(wQueue* queue);
140 WINPR_API BOOL Queue_Contains(wQueue* queue,
const void* obj);
151 WINPR_API BOOL Queue_Enqueue(wQueue* queue,
const void* obj);
160 WINPR_API
void* Queue_Dequeue(wQueue* queue);
169 WINPR_API
void* Queue_Peek(wQueue* queue);
178 WINPR_API
void Queue_Discard(wQueue* queue);
184 WINPR_API
void Queue_Free(wQueue* queue);
190 WINPR_ATTR_MALLOC(Queue_Free, 1)
191 WINPR_API wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor);
195 typedef struct s_wStack wStack;
197 WINPR_API
size_t Stack_Count(wStack* stack);
198 WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
200 WINPR_API
wObject* Stack_Object(wStack* stack);
202 WINPR_API
void Stack_Clear(wStack* stack);
203 WINPR_API BOOL Stack_Contains(wStack* stack, const
void* obj);
205 WINPR_API
void Stack_Push(wStack* stack,
void* obj);
206 WINPR_API
void* Stack_Pop(wStack* stack);
208 WINPR_API
void* Stack_Peek(wStack* stack);
210 WINPR_API
void Stack_Free(wStack* stack);
212 WINPR_ATTR_MALLOC(Stack_Free, 1)
213 WINPR_API wStack* Stack_New(BOOL synchronized);
217 typedef struct s_wArrayList wArrayList;
219 WINPR_API
size_t ArrayList_Capacity(wArrayList* arrayList);
220 WINPR_API
size_t ArrayList_Count(wArrayList* arrayList);
221 WINPR_API
size_t ArrayList_Items(wArrayList* arrayList, ULONG_PTR** ppItems);
222 WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList);
223 WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList);
224 WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList);
226 WINPR_API
void ArrayList_Lock(wArrayList* arrayList);
227 WINPR_API
void ArrayList_Unlock(wArrayList* arrayList);
229 WINPR_API
void* ArrayList_GetItem(wArrayList* arrayList,
size_t index);
230 WINPR_API BOOL ArrayList_SetItem(wArrayList* arrayList,
size_t index, const
void* obj);
232 WINPR_API
wObject* ArrayList_Object(wArrayList* arrayList);
234 typedef BOOL (*ArrayList_ForEachFkt)(
void* data,
size_t index, va_list ap);
236 WINPR_API BOOL ArrayList_ForEach(wArrayList* arrayList, ArrayList_ForEachFkt fkt, ...);
237 WINPR_API BOOL ArrayList_ForEachAP(wArrayList* arrayList, ArrayList_ForEachFkt fkt, va_list ap);
239 WINPR_API
void ArrayList_Clear(wArrayList* arrayList);
240 WINPR_API BOOL ArrayList_Contains(wArrayList* arrayList, const
void* obj);
242 #if defined(WITH_WINPR_DEPRECATED)
243 WINPR_API WINPR_DEPRECATED(
int ArrayList_Add(wArrayList* arrayList,
const void* obj));
246 WINPR_API BOOL ArrayList_Append(wArrayList* arrayList,
const void* obj);
247 WINPR_API BOOL ArrayList_Insert(wArrayList* arrayList,
size_t index,
const void* obj);
249 WINPR_API BOOL ArrayList_Remove(wArrayList* arrayList,
const void* obj);
250 WINPR_API BOOL ArrayList_RemoveAt(wArrayList* arrayList,
size_t index);
252 WINPR_API SSIZE_T ArrayList_IndexOf(wArrayList* arrayList,
const void* obj, SSIZE_T startIndex,
254 WINPR_API SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList,
const void* obj,
255 SSIZE_T startIndex, SSIZE_T count);
257 WINPR_API
void ArrayList_Free(wArrayList* arrayList);
259 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)
405 WINPR_API wListDictionary* ListDictionary_New(BOOL synchronized);
409 typedef struct s_wLinkedList wLinkedList;
417 WINPR_API
size_t LinkedList_Count(wLinkedList* list);
425 WINPR_API
void* LinkedList_First(wLinkedList* list);
433 WINPR_API
void* LinkedList_Last(wLinkedList* list);
442 WINPR_API BOOL LinkedList_Contains(wLinkedList* list, const
void* value);
450 WINPR_API
void LinkedList_Clear(wLinkedList* list);
460 WINPR_API BOOL LinkedList_AddFirst(wLinkedList* list, const
void* value);
470 WINPR_API BOOL LinkedList_AddLast(wLinkedList* list, const
void* value);
480 WINPR_API BOOL LinkedList_Remove(wLinkedList* list, const
void* value);
488 WINPR_API
void LinkedList_RemoveFirst(wLinkedList* list);
496 WINPR_API
void LinkedList_RemoveLast(wLinkedList* list);
503 WINPR_API
void LinkedList_Enumerator_Reset(wLinkedList* list);
511 WINPR_API
void* LinkedList_Enumerator_Current(wLinkedList* list);
519 WINPR_API BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list);
525 WINPR_API
void LinkedList_Free(wLinkedList* list);
531 WINPR_ATTR_MALLOC(LinkedList_Free, 1)
532 WINPR_API wLinkedList* LinkedList_New(
void);
540 WINPR_API
wObject* LinkedList_Object(wLinkedList* list);
546 typedef struct CountdownEvent wCountdownEvent;
554 WINPR_API
size_t CountdownEvent_CurrentCount(wCountdownEvent* countdown);
562 WINPR_API
size_t CountdownEvent_InitialCount(wCountdownEvent* countdown);
570 WINPR_API BOOL CountdownEvent_IsSet(wCountdownEvent* countdown);
579 WINPR_API HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown);
587 WINPR_API
void CountdownEvent_AddCount(wCountdownEvent* countdown,
size_t signalCount);
596 WINPR_API BOOL CountdownEvent_Signal(wCountdownEvent* countdown,
size_t signalCount);
603 WINPR_API
void CountdownEvent_Reset(wCountdownEvent* countdown,
size_t count);
609 WINPR_API
void CountdownEvent_Free(wCountdownEvent* countdown);
617 WINPR_ATTR_MALLOC(CountdownEvent_Free, 1)
618 WINPR_API wCountdownEvent* CountdownEvent_New(
size_t initialCount);
622 typedef UINT32 (*HASH_TABLE_HASH_FN)(const
void* key);
624 typedef struct s_wHashTable wHashTable;
626 typedef BOOL (*HASH_TABLE_FOREACH_FN)(const
void* key,
void* value,
void* arg);
628 WINPR_API
size_t HashTable_Count(wHashTable* table);
630 #if defined(WITH_WINPR_DEPRECATED)
631 WINPR_API WINPR_DEPRECATED(
int HashTable_Add(wHashTable* table,
const void* key,
635 WINPR_API BOOL HashTable_Insert(wHashTable* table,
const void* key,
const void* value);
636 WINPR_API BOOL HashTable_Remove(wHashTable* table,
const void* key);
637 WINPR_API
void HashTable_Clear(wHashTable* table);
638 WINPR_API BOOL HashTable_Contains(wHashTable* table,
const void* key);
639 WINPR_API BOOL HashTable_ContainsKey(wHashTable* table,
const void* key);
640 WINPR_API BOOL HashTable_ContainsValue(wHashTable* table,
const void* value);
641 WINPR_API
void* HashTable_GetItemValue(wHashTable* table,
const void* key);
642 WINPR_API BOOL HashTable_SetItemValue(wHashTable* table,
const void* key,
const void* value);
643 WINPR_API
size_t HashTable_GetKeys(wHashTable* table, ULONG_PTR** ppKeys);
644 WINPR_API BOOL HashTable_Foreach(wHashTable* table, HASH_TABLE_FOREACH_FN fn, VOID* arg);
646 WINPR_API UINT32 HashTable_PointerHash(
const void* pointer);
647 WINPR_API BOOL HashTable_PointerCompare(
const void* pointer1,
const void* pointer2);
649 WINPR_API UINT32 HashTable_StringHash(
const void* key);
650 WINPR_API BOOL HashTable_StringCompare(
const void* string1,
const void* string2);
651 WINPR_API
void* HashTable_StringClone(
const void* str);
652 WINPR_API
void HashTable_StringFree(
void* str);
654 WINPR_API
void HashTable_Free(wHashTable* table);
656 WINPR_ATTR_MALLOC(HashTable_Free, 1)
657 WINPR_API wHashTable* HashTable_New(BOOL synchronized);
659 WINPR_API
void HashTable_Lock(wHashTable* table);
660 WINPR_API
void HashTable_Unlock(wHashTable* table);
662 WINPR_API
wObject* HashTable_KeyObject(wHashTable* table);
663 WINPR_API
wObject* HashTable_ValueObject(wHashTable* table);
665 WINPR_API BOOL HashTable_SetHashFunction(wHashTable* table, HASH_TABLE_HASH_FN fn);
668 WINPR_API BOOL HashTable_SetupForStringData(wHashTable* table, BOOL stringValues);
672 typedef struct s_wBufferPool wBufferPool;
674 WINPR_API SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool);
675 WINPR_API SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const
void* buffer);
677 WINPR_API
void* BufferPool_Take(wBufferPool* pool, SSIZE_T bufferSize);
678 WINPR_API BOOL BufferPool_Return(wBufferPool* pool,
void* buffer);
679 WINPR_API
void BufferPool_Clear(wBufferPool* pool);
681 WINPR_API
void BufferPool_Free(wBufferPool* pool);
683 WINPR_ATTR_MALLOC(BufferPool_Free, 1)
684 WINPR_API wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment);
688 typedef struct s_wObjectPool wObjectPool;
690 WINPR_API
void* ObjectPool_Take(wObjectPool* pool);
691 WINPR_API
void ObjectPool_Return(wObjectPool* pool,
void* obj);
692 WINPR_API
void ObjectPool_Clear(wObjectPool* pool);
694 WINPR_API
wObject* ObjectPool_Object(wObjectPool* pool);
696 WINPR_API
void ObjectPool_Free(wObjectPool* pool);
698 WINPR_ATTR_MALLOC(ObjectPool_Free, 1)
699 WINPR_API wObjectPool* ObjectPool_New(BOOL synchronized);
705 typedef
void (*MESSAGE_FREE_FN)(wMessage* message);
714 MESSAGE_FREE_FN Free;
717 typedef struct s_wMessageQueue wMessageQueue;
719 #define WMQ_QUIT 0xFFFFFFFF
721 WINPR_API
wObject* MessageQueue_Object(wMessageQueue* queue);
722 WINPR_API HANDLE MessageQueue_Event(wMessageQueue* queue);
723 WINPR_API BOOL MessageQueue_Wait(wMessageQueue* queue);
724 WINPR_API
size_t MessageQueue_Size(wMessageQueue* queue);
726 WINPR_API BOOL MessageQueue_Dispatch(wMessageQueue* queue,
const wMessage* message);
727 WINPR_API BOOL MessageQueue_Post(wMessageQueue* queue,
void* context, UINT32 type,
void* wParam,
729 WINPR_API BOOL MessageQueue_PostQuit(wMessageQueue* queue,
int nExitCode);
731 WINPR_API
int MessageQueue_Get(wMessageQueue* queue, wMessage* message);
732 WINPR_API
int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove);
744 WINPR_API
int MessageQueue_Clear(wMessageQueue* queue);
757 WINPR_API
void MessageQueue_Free(wMessageQueue* queue);
771 WINPR_ATTR_MALLOC(MessageQueue_Free, 1)
772 WINPR_API wMessageQueue* MessageQueue_New(const
wObject* callback);
782 WINPR_API
void MessagePipe_PostQuit(
wMessagePipe* pipe,
int nExitCode);
786 WINPR_ATTR_MALLOC(MessagePipe_Free, 1)
797 typedef void (*pEventHandler)(
void* context,
const wEventArgs* e);
800 #define WINPR_EVENT_CAST(t, val) reinterpret_cast<t>(val)
802 #define WINPR_EVENT_CAST(t, val) (t)(val)
805 #define MAX_EVENT_HANDLERS 32
809 const char* EventName;
811 size_t EventHandlerCount;
812 pEventHandler EventHandlers[MAX_EVENT_HANDLERS];
815 #define EventArgsInit(_event_args, _sender) \
816 memset(_event_args, 0, sizeof(*_event_args)); \
817 (_event_args)->e.Size = sizeof(*_event_args); \
818 (_event_args)->e.Sender = _sender
820 #define DEFINE_EVENT_HANDLER(name) \
821 typedef void (*p##name##EventHandler)(void* context, const name##EventArgs* e)
823 #define DEFINE_EVENT_RAISE(name) \
824 static INLINE int PubSub_On##name(wPubSub* pubSub, void* context, const name##EventArgs* e) \
827 return PubSub_OnEvent(pubSub, #name, context, &e->e); \
830 #define DEFINE_EVENT_SUBSCRIBE(name) \
831 static INLINE int PubSub_Subscribe##name(wPubSub* pubSub, p##name##EventHandler EventHandler) \
833 return PubSub_Subscribe(pubSub, #name, EventHandler); \
836 #define DEFINE_EVENT_UNSUBSCRIBE(name) \
837 static INLINE int PubSub_Unsubscribe##name(wPubSub* pubSub, \
838 p##name##EventHandler EventHandler) \
840 return PubSub_Unsubscribe(pubSub, #name, EventHandler); \
843 #define DEFINE_EVENT_BEGIN(name) \
848 #define DEFINE_EVENT_END(name) \
851 DEFINE_EVENT_HANDLER(name); \
852 DEFINE_EVENT_RAISE(name) \
853 DEFINE_EVENT_SUBSCRIBE(name) \
854 DEFINE_EVENT_UNSUBSCRIBE(name)
856 #define DEFINE_EVENT_ENTRY(name) \
858 #name, { sizeof(name##EventArgs), NULL }, 0, \
864 typedef struct s_wPubSub wPubSub;
866 WINPR_API
void PubSub_Lock(wPubSub* pubSub);
867 WINPR_API
void PubSub_Unlock(wPubSub* pubSub);
869 WINPR_API
wEventType* PubSub_GetEventTypes(wPubSub* pubSub,
size_t* count);
870 WINPR_API
void PubSub_AddEventTypes(wPubSub* pubSub,
wEventType* events,
size_t count);
871 WINPR_API
wEventType* PubSub_FindEventType(wPubSub* pubSub,
const char* EventName);
873 WINPR_API
int PubSub_Subscribe(wPubSub* pubSub,
const char* EventName, ...);
874 WINPR_API
int PubSub_Unsubscribe(wPubSub* pubSub,
const char* EventName, ...);
876 WINPR_API
int PubSub_OnEvent(wPubSub* pubSub,
const char* EventName,
void* context,
879 WINPR_API
void PubSub_Free(wPubSub* pubSub);
881 WINPR_ATTR_MALLOC(PubSub_Free, 1)
882 WINPR_API wPubSub* PubSub_New(BOOL synchronized);
This struct contains function pointer to initialize/free objects.