libsheepyCSmallContainer.h (9420B)
1 // MIT License 2 // 3 // Copyright (c) 2026 Remy Noulin 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 #pragma once 23 24 // Class smallContainer 25 typedef struct smallContainer smallContainert; 26 27 /** 28 * help text for this class 29 * It is public declaration so that child classes can add their help text easily: 30 * ret "Child help text \n\n" helpTextSmallContainer; 31 */ 32 #define helpTextSmallContainer "TODO smallContainer help brief, class description /*, definitions,*/ methods, examples" 33 34 // for object inheriting smallContainer, cast to smallContainer to be able to use this class functions and generics 35 #define cCo(self) ( (smallContainert*) self ) 36 37 typedef void (*freeSmallContainerFt) (smallContainert *self); 38 typedef void (*terminateSmallContainerFt) (smallContainert **self); 39 typedef char* (*toStringSmallContainerFt) (smallContainert *self); 40 typedef smallContainert* (*duplicateSmallContainerFt) (smallContainert *self); 41 42 /** 43 * free container 44 */ 45 typedef void (*finishSmallContainerFt) (smallContainert **self); 46 47 typedef const char* (*helpSmallContainerFt) (smallContainert *self); 48 49 // smallContainer functions 50 /** 51 * prototype for user provided free function 52 * 53 * \param 54 * data pointer to buffer in container 55 */ 56 typedef void (*dataFreeSmallContainerFt) (void *data); 57 58 /** 59 * set function to free container data in the class 60 * 61 * All smallContainert objects have access to this function 62 * 63 * \param 64 * free user provided function to free the data in the container 65 */ 66 typedef smallContainert* (*setClassDataFreeSmallContainerFt) (smallContainert *self, dataFreeSmallContainerFt free); 67 68 /** 69 * set function to free container data in self 70 * 71 * \param 72 * free user provided function to free the data in the container 73 */ 74 typedef smallContainert* (*setObjectDataFreeSmallContainerFt) (smallContainert *self, dataFreeSmallContainerFt free); 75 76 /** 77 * prototype for user provided toString function 78 * 79 * \param 80 * data pointer to buffer in container 81 */ 82 typedef char* (*dataToStringSmallContainerFt) (void *data); 83 84 /** 85 * set function to stringify container data in the class 86 * 87 * \param 88 * toString user provided function to stringify the data in the container 89 */ 90 typedef smallContainert* (*setClassDataToStringSmallContainerFt) (smallContainert *self, dataToStringSmallContainerFt toString); 91 92 /** 93 * set function to stringify container data in self 94 * 95 * \param 96 * toString user provided function to stringify the data in the container 97 */ 98 typedef smallContainert* (*setObjectDataToStringSmallContainerFt) (smallContainert *self, dataToStringSmallContainerFt toString); 99 100 /** 101 * prototype for user provided duplicate function 102 * 103 * \param 104 * data pointer to buffer in container 105 */ 106 typedef void* (*dataDuplicateSmallContainerFt) (void *data); 107 108 /** 109 * set function to duplicate container data in the class 110 * 111 * \param 112 * duplicate user provided function to duplicate the data in the container 113 */ 114 typedef smallContainert* (*setClassDataDuplicateSmallContainerFt) (smallContainert *self, dataDuplicateSmallContainerFt duplicate); 115 116 /** 117 * set function to duplicate container data in self 118 * 119 * \param 120 * duplicate user provided function to duplicate the data in the container 121 */ 122 typedef smallContainert* (*setObjectDataDuplicateSmallContainerFt) (smallContainert *self, dataDuplicateSmallContainerFt duplicate); 123 124 /** 125 * free self but not the data inside 126 */ 127 typedef void (*smashSmallContainerFt) (smallContainert **self); 128 129 /** 130 * get a copy of smallContainer 131 * 132 * The returned smallContainer needs to be freed 133 * 134 * \return 135 * char* 136 */ 137 typedef void* (*getSmallContainerFt) (smallContainert *self); 138 139 /** 140 * set smallContainer 141 * 142 * \param 143 * string buffer to store in object (the buffer is duplicated) 144 */ 145 typedef smallContainert* (*setSmallContainerFt) (smallContainert *self, void *data); 146 147 /** 148 * class functions 149 * allocated once for all objects 150 * 151 * freed with finalizeSmallContainer or finalizeLibsheepy 152 */ 153 154 /** 155 * use this define in child classes and add the new function after this class functions 156 * 157 * in this define, add the methods after <finishClassTemplateFt finish;> 158 * 159 * Example: 160 * #define RINGFUNCTIONST \ 161 * CLASSTEMPLATEFUNCTIONST; \ 162 * setSizeRingFt setSize 163 */ 164 #define SMALLCONTAINERFUNCTIONST \ 165 helpSmallContainerFt help;\ 166 dataFreeSmallContainerFt dataFree;\ 167 setClassDataFreeSmallContainerFt setClassDataFree;\ 168 setObjectDataFreeSmallContainerFt setObjectDataFree;\ 169 dataToStringSmallContainerFt dataToString;\ 170 setClassDataToStringSmallContainerFt setClassDataToString;\ 171 setObjectDataToStringSmallContainerFt setObjectDataToString;\ 172 dataDuplicateSmallContainerFt dataDuplicate;\ 173 setClassDataDuplicateSmallContainerFt setClassDataDuplicate;\ 174 setObjectDataDuplicateSmallContainerFt setObjectDataDuplicate;\ 175 getSmallContainerFt get;\ 176 setSmallContainerFt set 177 178 typedef struct { 179 freeSmallContainerFt free; 180 terminateSmallContainerFt terminate; 181 toStringSmallContainerFt toString; 182 duplicateSmallContainerFt duplicate; 183 smashSmallContainerFt smash; 184 finishSmallContainerFt finish; 185 SMALLCONTAINERFUNCTIONST; 186 } smallContainerFunctionst; 187 188 /** 189 * class 190 */ 191 struct smallContainer { 192 const char *type; 193 smallContainerFunctionst *f; 194 195 /** pointer to data in smallContainer */ 196 sContainert *data; 197 dataFreeSmallContainerFt dataFree; 198 dataToStringSmallContainerFt dataToString; 199 dataDuplicateSmallContainerFt dataDuplicate; 200 }; 201 202 // smallContainer 203 204 #define createSmallContainer(obj) ;smallContainert obj; initiateSmallContainer(&obj) 205 #define createAllocateSmallContainer(obj) ;smallContainert *obj; initiateAllocateSmallContainer(&obj) 206 207 void initiateSmallContainer(smallContainert *self); 208 void initiateAllocateSmallContainer(smallContainert **self); 209 void finalizeRecycleSmallContainer(void *arg UNUSED); 210 void finalizeSmallContainer(void); 211 212 // initialize class methods, call registerMethodsSmallContainer from classes inheriting this class 213 void registerMethodsSmallContainer(smallContainerFunctionst *f); 214 215 smallContainert* allocSmallContainer(void *data); 216 217 // terminate smallContainert val when it is out of scope 218 void cleanUpSmallContainerTerminateG(smallContainert **val); 219 220 // free smallContainert local val when it is out of scope 221 void cleanUpSmallContainerFreeLocalG(smallContainert *val); 222 223 // free smallContainert val when it is out of scope 224 void cleanUpSmallContainerFreeG(smallContainert **val); 225 226 // finish smallContainert val when it is out of scope 227 void cleanUpSmallContainerFinishG(smallContainert **val); 228 229 /** 230 * declare pointer name with type smallContainert and terminate name when it is out of scope 231 */ 232 #define cleanSmallContainerP(name) smallContainert *name CLEANUP(cleanUpSmallContainerTerminateG) 233 234 /** 235 * allocate smallContainer (pointer) and clean up when it is out of scope 236 */ 237 #define cleanAllocateSmallContainer(obj) ;cleanSmallContainerP(obj); initiateAllocateSmallContainer(&obj) 238 239 /** 240 * declare local object name with type smallContainert and free name when it is out of scope 241 */ 242 #define cleanSmallContainer(name) smallContainert name CLEANUP(cleanUpSmallContainerFreeLocalG); initiateSmallContainer(&name) 243 244 /** 245 * declare pointer name with type smallContainert and free name when it is out of scope 246 */ 247 #define cleanFreeSmallContainer(name) smallContainert *name CLEANUP(cleanUpSmallContainerFreeG) 248 249 /** 250 * declare pointer name with Type smallContainert and finish name when it is out of scope 251 */ 252 #define cleanFinishSmallContainerP(name) smallContainert *name CLEANUP(cleanUpSmallContainerFinishG) 253 254 smallContainert* duplicateSmallContainerG (smallContainert *self); 255 256 void freeSmallContainerG(smallContainert *self); 257 258 void* getSmallContainerG(smallContainert *self, void* retType UNUSED, int64_t index UNUSED); 259 260 smallContainert* setSmallContainerG(smallContainert *self, void *data); 261 262 // end class smallContainer