feat: added Drop trait
This commit is contained in:
		
							parent
							
								
									aee9aec828
								
							
						
					
					
						commit
						640ed84c1d
					
				
							
								
								
									
										6
									
								
								drop.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								drop.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,6 @@
 | 
				
			||||||
 | 
					#include "drop.h"
 | 
				
			||||||
 | 
					#include "stdlib.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void default_drop(void* data) {
 | 
				
			||||||
 | 
					    free(data);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										33
									
								
								drop.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								drop.h
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,33 @@
 | 
				
			||||||
 | 
					#ifndef _fencer_drop_h
 | 
				
			||||||
 | 
					#define _fencer_drop_h
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "typeclass_helpers.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct {
 | 
				
			||||||
 | 
					    void (*const drop)(void* self);
 | 
				
			||||||
 | 
					} IDrop;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct {
 | 
				
			||||||
 | 
					    void* data;
 | 
				
			||||||
 | 
					    IDrop const* tc;
 | 
				
			||||||
 | 
					} Drop;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define impl_Drop_for(T, drop_f)\
 | 
				
			||||||
 | 
					Drop T##_as_Drop(T* x) {\
 | 
				
			||||||
 | 
					    TC_FN_TYPECHECK(void, drop_f, T*);\
 | 
				
			||||||
 | 
					    static IDrop const tc = {\
 | 
				
			||||||
 | 
					        .drop = (void(*const)(void*)) drop_f,\
 | 
				
			||||||
 | 
					    };\
 | 
				
			||||||
 | 
					    return (Drop){.tc = &tc, .data = x};\
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern void default_drop(void*);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define impl_default_Drop_for(T)\
 | 
				
			||||||
 | 
					Drop T##_as_Drop(T* x) {\
 | 
				
			||||||
 | 
					    static IDrop const tc = {\
 | 
				
			||||||
 | 
					        .drop = default_drop,\
 | 
				
			||||||
 | 
					    };\
 | 
				
			||||||
 | 
					    return (Drop){.tc = &tc, .data = x};\
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif // !_fencer_drop_h
 | 
				
			||||||
		Loading…
	
		Reference in a new issue