See what's going on with flipcode!




This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Faking Templates In C
  Submitted by



Ok, I know this is old news to most people here on this site, but some people may benefit from it. The reason why I wanted to do this is because I'm currently abroad and only have access to the free lcc-win32 compiler and I badly needed a vector-"template". I didn't just want a vector with void ptrs. It's nothing fancy or anything, but the day may come when somebody needs it, here goes:

#define CREATE_VECTOR_TYPE_H(type) \
typedef struct _##type##_Vector{ \
  type *pArray; \
  type illegal; \
  int size; \
  int len; \
} type##_Vector; \
void type##_InitVector(type##_Vector *pV, type illegal); \
void type##_InitVectorEx(type##_Vector *pV, int size, type illegal); \
void type##_ClearVector(type##_Vector *pV); \
void type##_DeleteAll(type##_Vector *pV); \
void type##_EraseVector(type##_Vector *pV); \
int type##_AddElem(type##_Vector *pV, type Data); \
type type##_SetElemAt(type##_Vector *pV, int pos, type data); \
type type##_GetElemAt(type##_Vector *pV, int pos);

#define CREATE_VECTOR_TYPE_C(type) \ void type##_InitVector(type##_Vector *pV, type illegal) \ { \ type##_InitVectorEx(pV, DEF_SIZE, illegal); \ } \ void type##_InitVectorEx(type##_Vector *pV, int size, type illegal) \ { \ pV-len = 0; \ pV-illegal = illegal; \ pV-pArray = malloc(sizeof(type) * size); \ pV-size = size; \ } \ void type##_ClearVector(type##_Vector *pV) \ { \ memset(pV-pArray, 0, sizeof(type) * pV-size); \ pV-len = 0; \ } \ void type##_EraseVector(type##_Vector *pV) \ { \ if(pV-pArray != NULL) \ free(pV-pArray); \ pV-len = 0; \ pV-size = 0; \ pV-pArray = NULL; \ } \ int type##_AddElem(type##_Vector *pV, type Data) \ { \ type *pTmp; \ if(pV-len = pV-size) \ { \ pTmp = malloc(sizeof(type) * pV-size * 2); \ if(pTmp == NULL) \ return -1; \ memcpy(pTmp, pV-pArray, sizeof(type) * pV-size); \ free(pV-pArray); \ pV-pArray = pTmp; \ pV-size *= 2; \ } \ pV-pArray[pV-len] = Data; \ return pV-len++; \ } \ type type##_SetElemAt(type##_Vector *pV, int pos, type data) \ { \ type old = pV-illegal; \ if(pos = 0 && pos <= pV-len) \ { \ old = pV-pArray[pos]; \ pV-pArray[pos] = data; \ } \ return old; \ } \ type type##_GetElemAt(type##_Vector *pV, int pos) \ { \ if(pos = 0 && pos <= pV-len) \ return pV-pArray[pos]; \ return pV-illegal; \ }



These two macros will create the vector for the type you specify. In the header file you write:

CREATE_VECTOR_TYPE_H(int) 



And in the source-file:

CREATE_VECTOR_TYPE_C(int)
// then you use the functions like
int_Vector iv;
int_InitVector(&iv, -1); // -1 is the illegal
// value of this vector, int_GetElemAt()
// returns this if the element doesn't exist.
int_AddElem(&iv, 10); 



The project I'm working on is a small script-language with a VM, currently I have only implemented the VM and an assembler to compile code for it and now I'm working on a preprocessor, both for the assembler code and the language.

/Andreas Magnusson


The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.