FreeRDP
Loading...
Searching...
No Matches
collections.h
1
20#ifndef WINPR_COLLECTIONS_H
21#define WINPR_COLLECTIONS_H
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27
28#include <winpr/winpr.h>
29#include <winpr/wtypes.h>
30#include <winpr/assert.h>
31
32#include <winpr/crt.h>
33#include <winpr/synch.h>
34#include <winpr/stream.h>
35
36#ifdef __cplusplus
37extern "C"
38{
39#endif
40
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);
46
51 typedef struct
52 {
53 OBJECT_NEW_FN fnObjectNew;
54 OBJECT_INIT_FN
56 OBJECT_UNINIT_FN
58 OBJECT_FREE_FN fnObjectFree;
59 OBJECT_EQUALS_FN fnObjectEquals;
60 } wObject;
61
62 /* utility function with compatible arguments for string data */
63
69 WINPR_API void* winpr_ObjectStringClone(const void* pvstr);
70
76 WINPR_API void* winpr_ObjectWStringClone(const void* pvstr);
77
82 WINPR_API void winpr_ObjectStringFree(void* pvstr);
83
84 /* System.Collections.Queue */
85
86 typedef struct s_wQueue wQueue;
87
94 WINPR_API size_t Queue_Count(wQueue* queue);
95
102 WINPR_API size_t Queue_Capacity(wQueue* queue);
103
108 WINPR_API void Queue_Lock(wQueue* queue);
109
114 WINPR_API void Queue_Unlock(wQueue* queue);
115
121 WINPR_API HANDLE Queue_Event(wQueue* queue);
122
130 WINPR_API wObject* Queue_Object(wQueue* queue);
131
136 WINPR_API void Queue_Clear(wQueue* queue);
137
145 WINPR_API BOOL Queue_Contains(wQueue* queue, const void* obj);
146
156 WINPR_API BOOL Queue_Enqueue(wQueue* queue, const void* obj);
157
165 WINPR_API void* Queue_Dequeue(wQueue* queue);
166
174 WINPR_API void* Queue_Peek(wQueue* queue);
175
183 WINPR_API void Queue_Discard(wQueue* queue);
184
189 WINPR_API void Queue_Free(wQueue* queue);
190
203 WINPR_ATTR_MALLOC(Queue_Free, 1)
204 WINPR_ATTR_NODISCARD
205 WINPR_API wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor);
206
207 /* System.Collections.Stack */
208
209 typedef struct s_wStack wStack;
210
211 WINPR_API size_t Stack_Count(wStack* stack);
212 WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
213
214 WINPR_API wObject* Stack_Object(wStack* stack);
215
216 WINPR_API void Stack_Clear(wStack* stack);
217 WINPR_API BOOL Stack_Contains(wStack* stack, const void* obj);
218
219 WINPR_API void Stack_Push(wStack* stack, void* obj);
220 WINPR_API void* Stack_Pop(wStack* stack);
221
222 WINPR_API void* Stack_Peek(wStack* stack);
223
224 WINPR_API void Stack_Free(wStack* stack);
225
226 WINPR_ATTR_MALLOC(Stack_Free, 1)
227 WINPR_ATTR_NODISCARD
228 WINPR_API wStack* Stack_New(BOOL synchronized);
229
230 /* System.Collections.ArrayList */
231
232 typedef struct s_wArrayList wArrayList;
233
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);
240
241 WINPR_API void ArrayList_Lock(wArrayList* arrayList);
242 WINPR_API void ArrayList_Unlock(wArrayList* arrayList);
243
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);
246
247 WINPR_API wObject* ArrayList_Object(wArrayList* arrayList);
248
249 typedef BOOL (*ArrayList_ForEachFkt)(void* data, size_t index, va_list ap);
250
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);
253
254 WINPR_API void ArrayList_Clear(wArrayList* arrayList);
255 WINPR_API BOOL ArrayList_Contains(wArrayList* arrayList, const void* obj);
256
257#if defined(WITH_WINPR_DEPRECATED)
258 WINPR_DEPRECATED(WINPR_API int ArrayList_Add(wArrayList* arrayList, const void* obj));
259#endif
260
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);
263
264 WINPR_API BOOL ArrayList_Remove(wArrayList* arrayList, const void* obj);
265 WINPR_API BOOL ArrayList_RemoveAt(wArrayList* arrayList, size_t index);
266
267 WINPR_API SSIZE_T ArrayList_IndexOf(wArrayList* arrayList, const void* obj, SSIZE_T startIndex,
268 SSIZE_T count);
269 WINPR_API SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList, const void* obj,
270 SSIZE_T startIndex, SSIZE_T count);
271
272 WINPR_API void ArrayList_Free(wArrayList* arrayList);
273
274 WINPR_ATTR_MALLOC(ArrayList_Free, 1)
275 WINPR_ATTR_NODISCARD
276 WINPR_API wArrayList* ArrayList_New(BOOL synchronized);
277
278 /* System.Collections.DictionaryBase */
279
280 /* System.Collections.Specialized.ListDictionary */
281 typedef struct s_wListDictionary wListDictionary;
282
289 WINPR_API wObject* ListDictionary_KeyObject(wListDictionary* listDictionary);
290
297 WINPR_API wObject* ListDictionary_ValueObject(wListDictionary* listDictionary);
298
305 WINPR_API size_t ListDictionary_Count(wListDictionary* listDictionary);
306
311 WINPR_API void ListDictionary_Lock(wListDictionary* listDictionary);
316 WINPR_API void ListDictionary_Unlock(wListDictionary* listDictionary);
317
327 WINPR_API BOOL ListDictionary_Add(wListDictionary* listDictionary, const void* key,
328 const void* value);
329
337 WINPR_API void* ListDictionary_Take(wListDictionary* listDictionary, const void* key);
338
344 WINPR_API void ListDictionary_Remove(wListDictionary* listDictionary, const void* key);
345
353 WINPR_API void* ListDictionary_Take_Head(wListDictionary* listDictionary);
354
359 WINPR_API void ListDictionary_Remove_Head(wListDictionary* listDictionary);
360
365 WINPR_API void ListDictionary_Clear(wListDictionary* listDictionary);
366
374 WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, const void* key);
375
384 WINPR_API size_t ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys);
385
394 WINPR_API void* ListDictionary_GetItemValue(wListDictionary* listDictionary, const void* key);
395
405 WINPR_API BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, const void* key,
406 const void* value);
407
412 WINPR_API void ListDictionary_Free(wListDictionary* listDictionary);
413
420 WINPR_ATTR_MALLOC(ListDictionary_Free, 1)
421 WINPR_ATTR_NODISCARD
422 WINPR_API wListDictionary* ListDictionary_New(BOOL synchronized);
423
424 /* System.Collections.Generic.LinkedList<T> */
425
426 typedef struct s_wLinkedList wLinkedList;
427
434 WINPR_API size_t LinkedList_Count(wLinkedList* list);
435
442 WINPR_API void* LinkedList_First(wLinkedList* list);
443
450 WINPR_API void* LinkedList_Last(wLinkedList* list);
451
459 WINPR_API BOOL LinkedList_Contains(wLinkedList* list, const void* value);
460
467 WINPR_API void LinkedList_Clear(wLinkedList* list);
468
477 WINPR_API BOOL LinkedList_AddFirst(wLinkedList* list, const void* value);
478
487 WINPR_API BOOL LinkedList_AddLast(wLinkedList* list, const void* value);
488
497 WINPR_API BOOL LinkedList_Remove(wLinkedList* list, const void* value);
498
505 WINPR_API void LinkedList_RemoveFirst(wLinkedList* list);
506
513 WINPR_API void LinkedList_RemoveLast(wLinkedList* list);
514
520 WINPR_API void LinkedList_Enumerator_Reset(wLinkedList* list);
521
528 WINPR_API void* LinkedList_Enumerator_Current(wLinkedList* list);
529
536 WINPR_API BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list);
537
542 WINPR_API void LinkedList_Free(wLinkedList* list);
543
548 WINPR_ATTR_MALLOC(LinkedList_Free, 1)
549 WINPR_ATTR_NODISCARD
550 WINPR_API wLinkedList* LinkedList_New(void);
551
558 WINPR_API wObject* LinkedList_Object(wLinkedList* list);
559
560 /* System.Collections.Generic.KeyValuePair<TKey,TValue> */
561
562 /* Countdown Event */
563
564 typedef struct CountdownEvent wCountdownEvent;
565
572 WINPR_API size_t CountdownEvent_CurrentCount(wCountdownEvent* countdown);
573
580 WINPR_API size_t CountdownEvent_InitialCount(wCountdownEvent* countdown);
581
588 WINPR_API BOOL CountdownEvent_IsSet(wCountdownEvent* countdown);
589
597 WINPR_API HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown);
598
605 WINPR_API void CountdownEvent_AddCount(wCountdownEvent* countdown, size_t signalCount);
606
614 WINPR_API BOOL CountdownEvent_Signal(wCountdownEvent* countdown, size_t signalCount);
615
621 WINPR_API void CountdownEvent_Reset(wCountdownEvent* countdown, size_t count);
622
627 WINPR_API void CountdownEvent_Free(wCountdownEvent* countdown);
628
635 WINPR_ATTR_MALLOC(CountdownEvent_Free, 1)
636 WINPR_ATTR_NODISCARD
637 WINPR_API wCountdownEvent* CountdownEvent_New(size_t initialCount);
638
639 /* Hash Table */
640
641 typedef UINT32 (*HASH_TABLE_HASH_FN)(const void* key);
642
643 typedef struct s_wHashTable wHashTable;
644
645 typedef BOOL (*HASH_TABLE_FOREACH_FN)(const void* key, void* value, void* arg);
646
647 WINPR_API size_t HashTable_Count(wHashTable* table);
648
649#if defined(WITH_WINPR_DEPRECATED)
650 WINPR_DEPRECATED(WINPR_API int HashTable_Add(wHashTable* table, const void* key,
651 const void* value));
652#endif
653
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);
664
665 WINPR_API UINT32 HashTable_PointerHash(const void* pointer);
666 WINPR_API BOOL HashTable_PointerCompare(const void* pointer1, const void* pointer2);
667
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);
672
673 WINPR_API void HashTable_Free(wHashTable* table);
674
675 WINPR_ATTR_MALLOC(HashTable_Free, 1)
676 WINPR_ATTR_NODISCARD
677 WINPR_API wHashTable* HashTable_New(BOOL synchronized);
678
679 WINPR_API void HashTable_Lock(wHashTable* table);
680 WINPR_API void HashTable_Unlock(wHashTable* table);
681
682 WINPR_API wObject* HashTable_KeyObject(wHashTable* table);
683 WINPR_API wObject* HashTable_ValueObject(wHashTable* table);
684
685 WINPR_API BOOL HashTable_SetHashFunction(wHashTable* table, HASH_TABLE_HASH_FN fn);
686
687 /* Utility function to setup hash table for strings */
688 WINPR_API BOOL HashTable_SetupForStringData(wHashTable* table, BOOL stringValues);
689
690 /* BufferPool */
691
692 typedef struct s_wBufferPool wBufferPool;
693
694 WINPR_API SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool);
695 WINPR_API SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const void* buffer);
696
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);
700
701 WINPR_API void BufferPool_Free(wBufferPool* pool);
702
703 WINPR_ATTR_MALLOC(BufferPool_Free, 1)
704 WINPR_ATTR_NODISCARD
705 WINPR_API wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment);
706
707 /* ObjectPool */
708
709 typedef struct s_wObjectPool wObjectPool;
710
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);
714
715 WINPR_API wObject* ObjectPool_Object(wObjectPool* pool);
716
717 WINPR_API void ObjectPool_Free(wObjectPool* pool);
718
719 WINPR_ATTR_MALLOC(ObjectPool_Free, 1)
720 WINPR_ATTR_NODISCARD
721 WINPR_API wObjectPool* ObjectPool_New(BOOL synchronized);
722
723 /* Message Queue */
724
725 typedef struct s_wMessage wMessage;
726
727 typedef void (*MESSAGE_FREE_FN)(wMessage* message);
728
730 {
731 UINT32 id;
732 void* context;
733 void* wParam;
734 void* lParam;
735 UINT64 time;
736 MESSAGE_FREE_FN Free;
737 };
738
739 typedef struct s_wMessageQueue wMessageQueue;
740
741#define WMQ_QUIT 0xFFFFFFFF
742
743 WINPR_API wObject* MessageQueue_Object(wMessageQueue* queue);
744 WINPR_API HANDLE MessageQueue_Event(wMessageQueue* queue);
745 WINPR_API BOOL MessageQueue_Wait(wMessageQueue* queue);
746
753 WINPR_API size_t MessageQueue_Size(wMessageQueue* queue);
754
761 WINPR_API size_t MessageQueue_Capacity(wMessageQueue* queue);
762
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,
765 void* lParam);
766 WINPR_API BOOL MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode);
767
768 WINPR_API int MessageQueue_Get(wMessageQueue* queue, wMessage* message);
769 WINPR_API int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove);
770
781 WINPR_API int MessageQueue_Clear(wMessageQueue* queue);
782
794 WINPR_API void MessageQueue_Free(wMessageQueue* queue);
795
808 WINPR_ATTR_MALLOC(MessageQueue_Free, 1)
809 WINPR_ATTR_NODISCARD
810 WINPR_API wMessageQueue* MessageQueue_New(const wObject* callback);
811
812 /* Message Pipe */
813
814 typedef struct
815 {
816 wMessageQueue* In;
817 wMessageQueue* Out;
818 } wMessagePipe;
819
820 WINPR_API void MessagePipe_PostQuit(wMessagePipe* pipe, int nExitCode);
821
822 WINPR_API void MessagePipe_Free(wMessagePipe* pipe);
823
824 WINPR_ATTR_MALLOC(MessagePipe_Free, 1)
825 WINPR_ATTR_NODISCARD
826 WINPR_API wMessagePipe* MessagePipe_New(void);
827
828 /* Publisher/Subscriber Pattern */
829
830 typedef struct
831 {
832 DWORD Size;
833 const char* Sender;
834 } wEventArgs;
835
836 typedef void (*pEventHandler)(void* context, const wEventArgs* e);
837
838#ifdef __cplusplus
839#define WINPR_EVENT_CAST(t, val) reinterpret_cast<t>(val)
840#else
841#define WINPR_EVENT_CAST(t, val) (t)(val)
842#endif
843
844#define MAX_EVENT_HANDLERS 32
845
846 typedef struct
847 {
848 const char* EventName;
849 wEventArgs EventArgs;
850 size_t EventHandlerCount;
851 pEventHandler EventHandlers[MAX_EVENT_HANDLERS];
852 } wEventType;
853
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
858
859#define DEFINE_EVENT_HANDLER(name) \
860 typedef void (*p##name##EventHandler)(void* context, const name##EventArgs* e)
861
862#define DEFINE_EVENT_RAISE(name) \
863 static inline int PubSub_On##name(wPubSub* pubSub, void* context, const name##EventArgs* e) \
864 { \
865 WINPR_ASSERT(e); \
866 return PubSub_OnEvent(pubSub, #name, context, &e->e); \
867 }
868
869#define DEFINE_EVENT_SUBSCRIBE(name) \
870 static inline int PubSub_Subscribe##name(wPubSub* pubSub, p##name##EventHandler EventHandler) \
871 { \
872 return PubSub_Subscribe(pubSub, #name, EventHandler); \
873 }
874
875#define DEFINE_EVENT_UNSUBSCRIBE(name) \
876 static inline int PubSub_Unsubscribe##name(wPubSub* pubSub, \
877 p##name##EventHandler EventHandler) \
878 { \
879 return PubSub_Unsubscribe(pubSub, #name, EventHandler); \
880 }
881
882#define DEFINE_EVENT_BEGIN(name) \
883 typedef struct \
884 { \
885 wEventArgs e;
886
887#define DEFINE_EVENT_END(name) \
888 } \
889 name##EventArgs; \
890 DEFINE_EVENT_HANDLER(name); \
891 DEFINE_EVENT_RAISE(name) \
892 DEFINE_EVENT_SUBSCRIBE(name) \
893 DEFINE_EVENT_UNSUBSCRIBE(name)
894
895#define DEFINE_EVENT_ENTRY(name) \
896 { \
897 #name, { sizeof(name##EventArgs), nullptr }, 0, \
898 { \
899 nullptr \
900 } \
901 }
902
903 typedef struct s_wPubSub wPubSub;
904
905 WINPR_API void PubSub_Lock(wPubSub* pubSub);
906 WINPR_API void PubSub_Unlock(wPubSub* pubSub);
907
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);
911
912 WINPR_API int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, ...);
913 WINPR_API int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, ...);
914
915 WINPR_API int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context,
916 const wEventArgs* e);
917
918 WINPR_API void PubSub_Free(wPubSub* pubSub);
919
920 WINPR_ATTR_MALLOC(PubSub_Free, 1)
921 WINPR_ATTR_NODISCARD
922 WINPR_API wPubSub* PubSub_New(BOOL synchronized);
923
924#ifdef __cplusplus
925}
926#endif
927
928#endif /* WINPR_COLLECTIONS_H */
This struct contains function pointer to initialize/free objects.
Definition collections.h:52
OBJECT_FREE_FN fnObjectFree
Definition collections.h:58
OBJECT_EQUALS_FN fnObjectEquals
Definition collections.h:59
OBJECT_UNINIT_FN fnObjectUninit
Definition collections.h:57
OBJECT_INIT_FN fnObjectInit
Definition collections.h:55
OBJECT_NEW_FN fnObjectNew
Definition collections.h:53