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
100 WINPR_API void Queue_Lock(wQueue* queue);
101
106 WINPR_API void Queue_Unlock(wQueue* queue);
107
113 WINPR_API HANDLE Queue_Event(wQueue* queue);
114
122 WINPR_API wObject* Queue_Object(wQueue* queue);
123
128 WINPR_API void Queue_Clear(wQueue* queue);
129
137 WINPR_API BOOL Queue_Contains(wQueue* queue, const void* obj);
138
148 WINPR_API BOOL Queue_Enqueue(wQueue* queue, const void* obj);
149
157 WINPR_API void* Queue_Dequeue(wQueue* queue);
158
166 WINPR_API void* Queue_Peek(wQueue* queue);
167
175 WINPR_API void Queue_Discard(wQueue* queue);
176
181 WINPR_API void Queue_Free(wQueue* queue);
182
187 WINPR_ATTR_MALLOC(Queue_Free, 1)
188 WINPR_ATTR_NODISCARD
189 WINPR_API wQueue* Queue_New(BOOL synchronized, SSIZE_T capacity, SSIZE_T growthFactor);
190
191 /* System.Collections.Stack */
192
193 typedef struct s_wStack wStack;
194
195 WINPR_API size_t Stack_Count(wStack* stack);
196 WINPR_API BOOL Stack_IsSynchronized(wStack* stack);
197
198 WINPR_API wObject* Stack_Object(wStack* stack);
199
200 WINPR_API void Stack_Clear(wStack* stack);
201 WINPR_API BOOL Stack_Contains(wStack* stack, const void* obj);
202
203 WINPR_API void Stack_Push(wStack* stack, void* obj);
204 WINPR_API void* Stack_Pop(wStack* stack);
205
206 WINPR_API void* Stack_Peek(wStack* stack);
207
208 WINPR_API void Stack_Free(wStack* stack);
209
210 WINPR_ATTR_MALLOC(Stack_Free, 1)
211 WINPR_ATTR_NODISCARD
212 WINPR_API wStack* Stack_New(BOOL synchronized);
213
214 /* System.Collections.ArrayList */
215
216 typedef struct s_wArrayList wArrayList;
217
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);
224
225 WINPR_API void ArrayList_Lock(wArrayList* arrayList);
226 WINPR_API void ArrayList_Unlock(wArrayList* arrayList);
227
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);
230
231 WINPR_API wObject* ArrayList_Object(wArrayList* arrayList);
232
233 typedef BOOL (*ArrayList_ForEachFkt)(void* data, size_t index, va_list ap);
234
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);
237
238 WINPR_API void ArrayList_Clear(wArrayList* arrayList);
239 WINPR_API BOOL ArrayList_Contains(wArrayList* arrayList, const void* obj);
240
241#if defined(WITH_WINPR_DEPRECATED)
242 WINPR_DEPRECATED(WINPR_API int ArrayList_Add(wArrayList* arrayList, const void* obj));
243#endif
244
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);
247
248 WINPR_API BOOL ArrayList_Remove(wArrayList* arrayList, const void* obj);
249 WINPR_API BOOL ArrayList_RemoveAt(wArrayList* arrayList, size_t index);
250
251 WINPR_API SSIZE_T ArrayList_IndexOf(wArrayList* arrayList, const void* obj, SSIZE_T startIndex,
252 SSIZE_T count);
253 WINPR_API SSIZE_T ArrayList_LastIndexOf(wArrayList* arrayList, const void* obj,
254 SSIZE_T startIndex, SSIZE_T count);
255
256 WINPR_API void ArrayList_Free(wArrayList* arrayList);
257
258 WINPR_ATTR_MALLOC(ArrayList_Free, 1)
259 WINPR_ATTR_NODISCARD
260 WINPR_API wArrayList* ArrayList_New(BOOL synchronized);
261
262 /* System.Collections.DictionaryBase */
263
264 /* System.Collections.Specialized.ListDictionary */
265 typedef struct s_wListDictionary wListDictionary;
266
273 WINPR_API wObject* ListDictionary_KeyObject(wListDictionary* listDictionary);
274
281 WINPR_API wObject* ListDictionary_ValueObject(wListDictionary* listDictionary);
282
289 WINPR_API size_t ListDictionary_Count(wListDictionary* listDictionary);
290
295 WINPR_API void ListDictionary_Lock(wListDictionary* listDictionary);
300 WINPR_API void ListDictionary_Unlock(wListDictionary* listDictionary);
301
311 WINPR_API BOOL ListDictionary_Add(wListDictionary* listDictionary, const void* key,
312 const void* value);
313
321 WINPR_API void* ListDictionary_Take(wListDictionary* listDictionary, const void* key);
322
328 WINPR_API void ListDictionary_Remove(wListDictionary* listDictionary, const void* key);
329
337 WINPR_API void* ListDictionary_Take_Head(wListDictionary* listDictionary);
338
343 WINPR_API void ListDictionary_Remove_Head(wListDictionary* listDictionary);
344
349 WINPR_API void ListDictionary_Clear(wListDictionary* listDictionary);
350
358 WINPR_API BOOL ListDictionary_Contains(wListDictionary* listDictionary, const void* key);
359
368 WINPR_API size_t ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys);
369
378 WINPR_API void* ListDictionary_GetItemValue(wListDictionary* listDictionary, const void* key);
379
389 WINPR_API BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, const void* key,
390 const void* value);
391
396 WINPR_API void ListDictionary_Free(wListDictionary* listDictionary);
397
404 WINPR_ATTR_MALLOC(ListDictionary_Free, 1)
405 WINPR_ATTR_NODISCARD
406 WINPR_API wListDictionary* ListDictionary_New(BOOL synchronized);
407
408 /* System.Collections.Generic.LinkedList<T> */
409
410 typedef struct s_wLinkedList wLinkedList;
411
418 WINPR_API size_t LinkedList_Count(wLinkedList* list);
419
426 WINPR_API void* LinkedList_First(wLinkedList* list);
427
434 WINPR_API void* LinkedList_Last(wLinkedList* list);
435
443 WINPR_API BOOL LinkedList_Contains(wLinkedList* list, const void* value);
444
451 WINPR_API void LinkedList_Clear(wLinkedList* list);
452
461 WINPR_API BOOL LinkedList_AddFirst(wLinkedList* list, const void* value);
462
471 WINPR_API BOOL LinkedList_AddLast(wLinkedList* list, const void* value);
472
481 WINPR_API BOOL LinkedList_Remove(wLinkedList* list, const void* value);
482
489 WINPR_API void LinkedList_RemoveFirst(wLinkedList* list);
490
497 WINPR_API void LinkedList_RemoveLast(wLinkedList* list);
498
504 WINPR_API void LinkedList_Enumerator_Reset(wLinkedList* list);
505
512 WINPR_API void* LinkedList_Enumerator_Current(wLinkedList* list);
513
520 WINPR_API BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list);
521
526 WINPR_API void LinkedList_Free(wLinkedList* list);
527
532 WINPR_ATTR_MALLOC(LinkedList_Free, 1)
533 WINPR_ATTR_NODISCARD
534 WINPR_API wLinkedList* LinkedList_New(void);
535
542 WINPR_API wObject* LinkedList_Object(wLinkedList* list);
543
544 /* System.Collections.Generic.KeyValuePair<TKey,TValue> */
545
546 /* Countdown Event */
547
548 typedef struct CountdownEvent wCountdownEvent;
549
556 WINPR_API size_t CountdownEvent_CurrentCount(wCountdownEvent* countdown);
557
564 WINPR_API size_t CountdownEvent_InitialCount(wCountdownEvent* countdown);
565
572 WINPR_API BOOL CountdownEvent_IsSet(wCountdownEvent* countdown);
573
581 WINPR_API HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown);
582
589 WINPR_API void CountdownEvent_AddCount(wCountdownEvent* countdown, size_t signalCount);
590
598 WINPR_API BOOL CountdownEvent_Signal(wCountdownEvent* countdown, size_t signalCount);
599
605 WINPR_API void CountdownEvent_Reset(wCountdownEvent* countdown, size_t count);
606
611 WINPR_API void CountdownEvent_Free(wCountdownEvent* countdown);
612
619 WINPR_ATTR_MALLOC(CountdownEvent_Free, 1)
620 WINPR_ATTR_NODISCARD
621 WINPR_API wCountdownEvent* CountdownEvent_New(size_t initialCount);
622
623 /* Hash Table */
624
625 typedef UINT32 (*HASH_TABLE_HASH_FN)(const void* key);
626
627 typedef struct s_wHashTable wHashTable;
628
629 typedef BOOL (*HASH_TABLE_FOREACH_FN)(const void* key, void* value, void* arg);
630
631 WINPR_API size_t HashTable_Count(wHashTable* table);
632
633#if defined(WITH_WINPR_DEPRECATED)
634 WINPR_DEPRECATED(WINPR_API int HashTable_Add(wHashTable* table, const void* key,
635 const void* value));
636#endif
637
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);
648
649 WINPR_API UINT32 HashTable_PointerHash(const void* pointer);
650 WINPR_API BOOL HashTable_PointerCompare(const void* pointer1, const void* pointer2);
651
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);
656
657 WINPR_API void HashTable_Free(wHashTable* table);
658
659 WINPR_ATTR_MALLOC(HashTable_Free, 1)
660 WINPR_ATTR_NODISCARD
661 WINPR_API wHashTable* HashTable_New(BOOL synchronized);
662
663 WINPR_API void HashTable_Lock(wHashTable* table);
664 WINPR_API void HashTable_Unlock(wHashTable* table);
665
666 WINPR_API wObject* HashTable_KeyObject(wHashTable* table);
667 WINPR_API wObject* HashTable_ValueObject(wHashTable* table);
668
669 WINPR_API BOOL HashTable_SetHashFunction(wHashTable* table, HASH_TABLE_HASH_FN fn);
670
671 /* Utility function to setup hash table for strings */
672 WINPR_API BOOL HashTable_SetupForStringData(wHashTable* table, BOOL stringValues);
673
674 /* BufferPool */
675
676 typedef struct s_wBufferPool wBufferPool;
677
678 WINPR_API SSIZE_T BufferPool_GetPoolSize(wBufferPool* pool);
679 WINPR_API SSIZE_T BufferPool_GetBufferSize(wBufferPool* pool, const void* buffer);
680
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);
684
685 WINPR_API void BufferPool_Free(wBufferPool* pool);
686
687 WINPR_ATTR_MALLOC(BufferPool_Free, 1)
688 WINPR_ATTR_NODISCARD
689 WINPR_API wBufferPool* BufferPool_New(BOOL synchronized, SSIZE_T fixedSize, DWORD alignment);
690
691 /* ObjectPool */
692
693 typedef struct s_wObjectPool wObjectPool;
694
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);
698
699 WINPR_API wObject* ObjectPool_Object(wObjectPool* pool);
700
701 WINPR_API void ObjectPool_Free(wObjectPool* pool);
702
703 WINPR_ATTR_MALLOC(ObjectPool_Free, 1)
704 WINPR_ATTR_NODISCARD
705 WINPR_API wObjectPool* ObjectPool_New(BOOL synchronized);
706
707 /* Message Queue */
708
709 typedef struct s_wMessage wMessage;
710
711 typedef void (*MESSAGE_FREE_FN)(wMessage* message);
712
714 {
715 UINT32 id;
716 void* context;
717 void* wParam;
718 void* lParam;
719 UINT64 time;
720 MESSAGE_FREE_FN Free;
721 };
722
723 typedef struct s_wMessageQueue wMessageQueue;
724
725#define WMQ_QUIT 0xFFFFFFFF
726
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);
731
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,
734 void* lParam);
735 WINPR_API BOOL MessageQueue_PostQuit(wMessageQueue* queue, int nExitCode);
736
737 WINPR_API int MessageQueue_Get(wMessageQueue* queue, wMessage* message);
738 WINPR_API int MessageQueue_Peek(wMessageQueue* queue, wMessage* message, BOOL remove);
739
750 WINPR_API int MessageQueue_Clear(wMessageQueue* queue);
751
763 WINPR_API void MessageQueue_Free(wMessageQueue* queue);
764
777 WINPR_ATTR_MALLOC(MessageQueue_Free, 1)
778 WINPR_ATTR_NODISCARD
779 WINPR_API wMessageQueue* MessageQueue_New(const wObject* callback);
780
781 /* Message Pipe */
782
783 typedef struct
784 {
785 wMessageQueue* In;
786 wMessageQueue* Out;
787 } wMessagePipe;
788
789 WINPR_API void MessagePipe_PostQuit(wMessagePipe* pipe, int nExitCode);
790
791 WINPR_API void MessagePipe_Free(wMessagePipe* pipe);
792
793 WINPR_ATTR_MALLOC(MessagePipe_Free, 1)
794 WINPR_ATTR_NODISCARD
795 WINPR_API wMessagePipe* MessagePipe_New(void);
796
797 /* Publisher/Subscriber Pattern */
798
799 typedef struct
800 {
801 DWORD Size;
802 const char* Sender;
803 } wEventArgs;
804
805 typedef void (*pEventHandler)(void* context, const wEventArgs* e);
806
807#ifdef __cplusplus
808#define WINPR_EVENT_CAST(t, val) reinterpret_cast<t>(val)
809#else
810#define WINPR_EVENT_CAST(t, val) (t)(val)
811#endif
812
813#define MAX_EVENT_HANDLERS 32
814
815 typedef struct
816 {
817 const char* EventName;
818 wEventArgs EventArgs;
819 size_t EventHandlerCount;
820 pEventHandler EventHandlers[MAX_EVENT_HANDLERS];
821 } wEventType;
822
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
827
828#define DEFINE_EVENT_HANDLER(name) \
829 typedef void (*p##name##EventHandler)(void* context, const name##EventArgs* e)
830
831#define DEFINE_EVENT_RAISE(name) \
832 static inline int PubSub_On##name(wPubSub* pubSub, void* context, const name##EventArgs* e) \
833 { \
834 WINPR_ASSERT(e); \
835 return PubSub_OnEvent(pubSub, #name, context, &e->e); \
836 }
837
838#define DEFINE_EVENT_SUBSCRIBE(name) \
839 static inline int PubSub_Subscribe##name(wPubSub* pubSub, p##name##EventHandler EventHandler) \
840 { \
841 return PubSub_Subscribe(pubSub, #name, EventHandler); \
842 }
843
844#define DEFINE_EVENT_UNSUBSCRIBE(name) \
845 static inline int PubSub_Unsubscribe##name(wPubSub* pubSub, \
846 p##name##EventHandler EventHandler) \
847 { \
848 return PubSub_Unsubscribe(pubSub, #name, EventHandler); \
849 }
850
851#define DEFINE_EVENT_BEGIN(name) \
852 typedef struct \
853 { \
854 wEventArgs e;
855
856#define DEFINE_EVENT_END(name) \
857 } \
858 name##EventArgs; \
859 DEFINE_EVENT_HANDLER(name); \
860 DEFINE_EVENT_RAISE(name) \
861 DEFINE_EVENT_SUBSCRIBE(name) \
862 DEFINE_EVENT_UNSUBSCRIBE(name)
863
864#define DEFINE_EVENT_ENTRY(name) \
865 { \
866#name, { sizeof(name##EventArgs), NULL }, 0, \
867 { \
868 NULL \
869 } \
870 }
871
872 typedef struct s_wPubSub wPubSub;
873
874 WINPR_API void PubSub_Lock(wPubSub* pubSub);
875 WINPR_API void PubSub_Unlock(wPubSub* pubSub);
876
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);
880
881 WINPR_API int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, ...);
882 WINPR_API int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, ...);
883
884 WINPR_API int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context,
885 const wEventArgs* e);
886
887 WINPR_API void PubSub_Free(wPubSub* pubSub);
888
889 WINPR_ATTR_MALLOC(PubSub_Free, 1)
890 WINPR_ATTR_NODISCARD
891 WINPR_API wPubSub* PubSub_New(BOOL synchronized);
892
893#ifdef __cplusplus
894}
895#endif
896
897#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