libsheepyCSmallInt.h (14341B)
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 #include <stdint.h> 25 26 // Class smallInt 27 typedef struct smallInt smallIntt; 28 29 /** 30 * help text for this class 31 * It is public declaration so that child classes can add their help text easily: 32 * ret "Child help text \n\n" helpTextSmallInt; 33 */ 34 #define helpTextSmallInt "TODO smallInt help brief, class description /*, definitions,*/ methods, examples" 35 36 // for object inheriting smallInt, cast to smallInt to be able to use this class functions and generics 37 #define cIn(self) ( (smallIntt*) self ) 38 39 typedef void (*freeSmallIntFt) (smallIntt *self); 40 typedef void (*terminateSmallIntFt) (smallIntt **self); 41 typedef char* (*toStringSmallIntFt) (smallIntt *self); 42 typedef smallIntt* (*duplicateSmallIntFt) (smallIntt *self); 43 44 // smallInt functions 45 /** 46 * free self but not the smallInt buffer 47 * 48 * Useful when smallInt buffer is shared or 49 * when the buffer holds a static smallInt 50 */ 51 typedef void (*smashSmallIntFt) (smallIntt **self); 52 53 /** 54 * free container 55 */ 56 typedef void (*finishSmallIntFt) (smallIntt **self); 57 58 typedef const char* (*helpSmallIntFt) (smallIntt *self); 59 60 /** 61 * get int 62 * 63 * \return 64 * int 65 */ 66 typedef int64_t (*getSmallIntFt) (smallIntt *self); 67 68 /** 69 * set int 70 * 71 * \param 72 * int to store in object 73 */ 74 typedef smallIntt* (*setSmallIntFt) (smallIntt *self, int64_t value); 75 76 typedef smallIntt* (*setBoolSmallIntFt) (smallIntt* self, bool p2); 77 typedef smallIntt* (*setDoubleSmallIntFt) (smallIntt* self, double p2); 78 typedef smallIntt* (*setInt32SmallIntFt) (smallIntt* self, int32_t p2); 79 typedef smallIntt* (*setUint32SmallIntFt) (smallIntt* self, uint32_t p2); 80 typedef smallIntt* (*setUint64SmallIntFt) (smallIntt* self, uint64_t p2); 81 typedef smallIntt* (*setSSmallIntFt) (smallIntt* self, const char* p2); 82 typedef smallIntt* (*setSmallBoolSmallIntFt) (smallIntt* self, smallBoolt* p2); 83 typedef smallIntt* (*setSmallDoubleSmallIntFt)(smallIntt* self, smallDoublet* p2); 84 typedef smallIntt* (*setSmallIntSmallIntFt) (smallIntt* self, smallIntt* p2); 85 typedef smallIntt* (*setSmallJsonSmallIntFt) (smallIntt* self, smallJsont* p2); 86 typedef smallIntt* (*setSmallStringSmallIntFt)(smallIntt* self, smallStringt* p2); 87 88 /** 89 * get a pointer to int 90 * 91 * \return 92 * int 93 */ 94 typedef int64_t* (*getPSmallIntFt) (smallIntt *self); 95 96 typedef bool (*equalSmallIntChaFt) (smallIntt* self, char p2); 97 typedef bool (*equalSmallIntCharFt) (smallIntt* self, const char * p2); 98 typedef bool (*equalSmallIntBaseFt) (smallIntt* self, baset* p2); 99 typedef bool (*equalSmallIntBoolFt) (smallIntt* self, bool p2); 100 typedef bool (*equalSmallIntDoubleFt) (smallIntt* self, double p2); 101 typedef bool (*equalSmallIntInt64Ft) (smallIntt* self, int64_t p2); 102 typedef bool (*equalSmallIntInt32Ft) (smallIntt* self, int32_t p2); 103 typedef bool (*equalSmallIntUint32Ft) (smallIntt* self, uint32_t p2); 104 typedef bool (*equalSmallIntUint64Ft) (smallIntt* self, uint64_t p2); 105 typedef bool (*equalSmallIntSmallBoolFt) (smallIntt* self, smallBoolt* p2); 106 typedef bool (*equalSmallIntSmallBytesFt) (smallIntt* self, smallBytest* p2); 107 typedef bool (*equalSmallIntSmallDoubleFt)(smallIntt* self, smallDoublet* p2); 108 typedef bool (*equalSmallIntFt) (smallIntt* self, smallIntt* p2); 109 typedef bool (*equalSmallIntSmallJsonFt) (smallIntt* self, smallJsont* p2); 110 typedef bool (*equalSmallIntSmallStringFt)(smallIntt* self, smallStringt* p2); 111 112 /** 113 * read file to int 114 * 115 * \param 116 * filePath: path to file 117 * \return 118 * 0 data in file 119 * -1 an error occured 120 */ 121 typedef smallIntt* (*readFileSmallIntFt) (smallIntt *self, const char *filePath); 122 typedef smallIntt* (*readFileSmallJsonSmallIntFt) (smallIntt *self, smallJsont *filePath); 123 typedef smallIntt* (*readFileSmallStringSmallIntFt)(smallIntt *self, smallStringt *filePath); 124 typedef smallIntt* (*readStreamSmallIntFt) (smallIntt *self, FILE *fp); 125 126 /** 127 * write int to file 128 * 129 * \param 130 * filePath path to file 131 * \return 132 * 1 success 133 * 0 error 134 */ 135 typedef int (*writeFileSmallIntFt) (smallIntt *self, const char *filePath); 136 typedef int (*writeFileSmallJsonSmallIntFt) (smallIntt *self, smallJsont *filePath); 137 typedef int (*writeFileSmallStringSmallIntFt)(smallIntt *self, smallStringt *filePath); 138 typedef int (*writeStreamSmallIntFt) (smallIntt *self, FILE *fp); 139 140 /** 141 * append int to filePath 142 * 143 * \param 144 * filePath 145 * \return 146 * true success 147 * false failed, filePath 148 */ 149 typedef int (*appendFileSmallIntFt)(smallIntt *self, const char *filePath); 150 typedef int (*appendFileSmallStringSmallIntFt)(smallIntt *self, smallStringt *filePath); 151 152 /** 153 * class functions 154 * allocated once for all objects 155 * 156 * freed with finalizeSmallInt or finalizeLibsheepy 157 */ 158 159 /** 160 * use this define in child classes and add the new function after this class functions 161 * 162 * in this define, add the methods after <finishClassTemplateFt finish;> 163 * 164 * Example: 165 * #define RINGFUNCTIONST \ 166 * CLASSTEMPLATEFUNCTIONST; \ 167 * setSizeRingFt setSize 168 */ 169 #define SMALLINTFUNCTIONST \ 170 helpSmallIntFt help;\ 171 getSmallIntFt get;\ 172 setSmallIntFt set;\ 173 setBoolSmallIntFt setBool;\ 174 setDoubleSmallIntFt setDouble;\ 175 setInt32SmallIntFt setInt32;\ 176 setUint32SmallIntFt setUint32;\ 177 setUint64SmallIntFt setUint64;\ 178 setSSmallIntFt setS;\ 179 setSmallBoolSmallIntFt setSmallBool;\ 180 setSmallDoubleSmallIntFt setSmallDouble;\ 181 setSmallIntSmallIntFt setSmallInt;\ 182 setSmallJsonSmallIntFt setSmallJson;\ 183 setSmallStringSmallIntFt setSmallString;\ 184 getPSmallIntFt getP;\ 185 equalSmallIntChaFt equalCha;\ 186 equalSmallIntCharFt equalChar;\ 187 equalSmallIntBaseFt equalBase;\ 188 equalSmallIntBoolFt equalBool;\ 189 equalSmallIntDoubleFt equalDouble;\ 190 equalSmallIntInt64Ft equalInt64;\ 191 equalSmallIntInt32Ft equalInt32;\ 192 equalSmallIntUint32Ft equalUint32;\ 193 equalSmallIntUint64Ft equalUint64;\ 194 equalSmallIntSmallBoolFt equalSmallBool;\ 195 equalSmallIntSmallBytesFt equalSmallBytes;\ 196 equalSmallIntSmallDoubleFt equalSmallDouble;\ 197 equalSmallIntFt equal;\ 198 equalSmallIntSmallJsonFt equalSmallJson;\ 199 equalSmallIntSmallStringFt equalSmallString;\ 200 readFileSmallIntFt readFile;\ 201 readFileSmallJsonSmallIntFt readFileSmallJson;\ 202 readFileSmallStringSmallIntFt readFileSmallString;\ 203 readStreamSmallIntFt readStream;\ 204 writeFileSmallIntFt writeFile;\ 205 writeFileSmallJsonSmallIntFt writeFileSmallJson;\ 206 writeFileSmallStringSmallIntFt writeFileSmallString;\ 207 writeStreamSmallIntFt writeStream;\ 208 appendFileSmallIntFt appendFile;\ 209 appendFileSmallStringSmallIntFt appendFileSmallString 210 211 212 typedef struct { 213 freeSmallIntFt free; 214 terminateSmallIntFt terminate; 215 toStringSmallIntFt toString; 216 duplicateSmallIntFt duplicate; 217 smashSmallIntFt smash; 218 finishSmallIntFt finish; 219 SMALLINTFUNCTIONST; 220 } smallIntFunctionst; 221 222 /** 223 * class 224 */ 225 struct smallInt { 226 const char *type; 227 smallIntFunctionst *f; 228 229 sIntt *value; 230 }; 231 232 // smallInt 233 234 #define createSmallInt(obj) ;smallIntt obj; initiateSmallInt(&obj) 235 #define createAllocateSmallInt(obj) ;smallIntt *obj; initiateAllocateSmallInt(&obj) 236 237 void initiateSmallInt(smallIntt *self); 238 void initiateAllocateSmallInt(smallIntt **self); 239 void finalizeRecycleSmallInt(void *arg UNUSED); 240 void finalizeSmallInt(void); 241 242 // initialize class methods, call registerMethodsSmallInt from classes inheriting this class 243 void registerMethodsSmallInt(smallIntFunctionst *f); 244 245 smallIntt* allocSmallInt(int64_t value); 246 247 // terminate smallIntt val when it is out of scope 248 void cleanUpSmallIntTerminateG(smallIntt **val); 249 250 // free smallIntt local val when it is out of scope 251 void cleanUpSmallIntFreeLocalG(smallIntt *val); 252 253 // free smallIntt val when it is out of scope 254 void cleanUpSmallIntFreeG(smallIntt **val); 255 256 // finish smallIntt val when it is out of scope 257 void cleanUpSmallIntFinishG(smallIntt **val); 258 259 /** 260 * declare pointer name with type smallIntt and terminate name when it is out of scope 261 */ 262 #define cleanSmallIntP(name) smallIntt *name CLEANUP(cleanUpSmallIntTerminateG) 263 264 /** 265 * allocate smallInt (pointer) and clean up when it is out of scope 266 */ 267 #define cleanAllocateSmallInt(obj) ;cleanSmallIntP(obj); initiateAllocateSmallInt(&obj) 268 269 /** 270 * declare local object name with type smallIntt and free name when it is out of scope 271 */ 272 #define cleanSmallInt(name) smallIntt name CLEANUP(cleanUpSmallIntFreeLocalG); initiateSmallInt(&name) 273 274 /** 275 * declare pointer name with type smallIntt and free name when it is out of scope 276 */ 277 #define cleanFreeSmallInt(name) smallIntt *name CLEANUP(cleanUpSmallIntFreeG) 278 279 /** 280 * declare pointer name with Type smallIntt and finish name when it is out of scope 281 */ 282 #define cleanFinishSmallIntP(name) smallIntt *name CLEANUP(cleanUpSmallIntFinishG) 283 284 smallIntt* duplicateSmallIntG (smallIntt *self); 285 286 void freeSmallIntG (smallIntt *self); 287 288 int64_t getIntSmallIntG (smallIntt *self, int64_t retType UNUSED, int64_t index UNUSED); 289 int64_t* getIntPSmallIntG (smallIntt *self, int64_t* retType UNUSED, int64_t index UNUSED); 290 int32_t getInt32SmallIntG (smallIntt *self, int32_t retType UNUSED, int64_t index UNUSED); 291 int32_t* getInt32PSmallIntG (smallIntt *self, int32_t* retType UNUSED, int64_t index UNUSED); 292 uint64_t getUintSmallIntG (smallIntt *self, uint64_t retType UNUSED, int64_t index UNUSED); 293 uint64_t* getUintPSmallIntG (smallIntt *self, uint64_t* retType UNUSED, int64_t index UNUSED); 294 uint32_t getUint32SmallIntG (smallIntt *self, uint32_t retType UNUSED, int64_t index UNUSED); 295 uint32_t* getUint32PSmallIntG (smallIntt *self, uint32_t* retType UNUSED, int64_t index UNUSED); 296 297 smallIntt* setBoolSmallIntG (smallIntt* self, bool p2); 298 smallIntt* setDoubleSmallIntG (smallIntt* self, double p2); 299 smallIntt* setSmallIntG (smallIntt* self, int64_t p2); 300 smallIntt* setInt32SmallIntG (smallIntt* self, int32_t p2); 301 smallIntt* setUint32SmallIntG (smallIntt* self, uint32_t p2); 302 smallIntt* setUint64SmallIntG (smallIntt* self, uint64_t p2); 303 smallIntt* setSSmallIntG (smallIntt* self, const char* p2); 304 smallIntt* setSmallBoolSmallIntG (smallIntt* self, smallBoolt* p2); 305 smallIntt* setSmallDoubleSmallIntG (smallIntt* self, smallDoublet* p2); 306 smallIntt* setSmallIntSmallIntG (smallIntt* self, smallIntt* p2); 307 smallIntt* setSmallJsonSmallIntG (smallIntt* self, smallJsont* p2); 308 smallIntt* setSmallStringSmallIntG (smallIntt* self, smallStringt* p2); 309 310 bool equalSmallIntChaG (smallIntt* self, char p2); 311 bool equalSmallIntCharG (smallIntt* self, const char * p2); 312 bool equalSmallIntBaseG (smallIntt* self, baset* p2); 313 bool equalSmallIntBoolG (smallIntt* self, bool p2); 314 bool equalSmallIntDoubleG (smallIntt* self, double p2); 315 bool equalSmallIntInt64G (smallIntt* self, int64_t p2); 316 bool equalSmallIntInt32G (smallIntt* self, int32_t p2); 317 bool equalSmallIntUint32G (smallIntt* self, uint32_t p2); 318 bool equalSmallIntUint64G (smallIntt* self, uint64_t p2); 319 bool equalSmallIntSmallBoolG (smallIntt* self, smallBoolt* p2); 320 bool equalSmallIntSmallBytesG (smallIntt* self, smallBytest* p2); 321 bool equalSmallIntSmallDoubleG(smallIntt* self, smallDoublet* p2); 322 bool equalSmallIntFG (smallIntt* self, smallIntt* p2); 323 bool equalSmallIntSmallJsonG (smallIntt* self, smallJsont* p2); 324 bool equalSmallIntSmallStringG(smallIntt* self, smallStringt* p2); 325 326 smallIntt* readFileSmallIntG (smallIntt *self, const char *filePath); 327 smallIntt* readFileSmallJsonSmallIntG (smallIntt *self, smallJsont *filePath); 328 smallIntt* readFileSmallStringSmallIntG(smallIntt *self, smallStringt *filePath); 329 smallIntt* readStreamSmallIntG (smallIntt *self, FILE *fp); 330 int writeFileSmallIntG (smallIntt *self, const char *filePath); 331 int writeFileSmallJsonSmallIntG (smallIntt *self, smallJsont *filePath); 332 int writeFileSmallStringSmallIntG(smallIntt *self, smallStringt *filePath); 333 int writeStreamSmallIntG (smallIntt *self, FILE *fp); 334 int appendFileSmallIntFG (smallIntt *self, const char *filePath); //TODO remove F - oGMacros generates a macro making the compilation fail 335 int appendFileSmallStringSmallIntG(smallIntt *self, smallStringt *filePath); 336 // end class smallInt