Page 1 of 1
some macros I wrote
Posted: Sat Mar 29, 2008 9:27 am
by nekros
I wrote a bunch of macros to make type casting in C look better
Code: Select all
#define cast_uchar(x) ((unsigned char) (x))
#define cast_ucharp(x) ((unsigned char*) (x))
#define cast_char(x) ((char) (x))
#define cast_charp(x) ((char*) (x))
#define cast_ushort(x) ((unsigned short) (x))
#define cast_ushortp(x) ((unsigned short*) (x))
#define cast_short(x) ((short) (x))
#define cast_shortp(x) ((short*) (x))
#define cast_uint(x) ((unsigned int) (x))
#define cast_uintp(x) ((unsigned int*) (x))
#define cast_int(x) ((int) (x))
#define cast_intp(x) ((int*) (x))
#define cast_voidp(x) ((void*) (x))
Re: some macros I wrote
Posted: Sat Mar 29, 2008 11:11 am
by Candy
nekros wrote:I wrote a bunch of macros to make type casting in C look better
Code: Select all
#define cast_uchar(x) ((unsigned char) (x))
#define cast_ucharp(x) ((unsigned char*) (x))
#define cast_char(x) ((char) (x))
#define cast_charp(x) ((char*) (x))
#define cast_ushort(x) ((unsigned short) (x))
#define cast_ushortp(x) ((unsigned short*) (x))
#define cast_short(x) ((short) (x))
#define cast_shortp(x) ((short*) (x))
#define cast_uint(x) ((unsigned int) (x))
#define cast_uintp(x) ((unsigned int*) (x))
#define cast_int(x) ((int) (x))
#define cast_intp(x) ((int*) (x))
#define cast_voidp(x) ((void*) (x))
What about static_cast<char>(x) ?
I think your style is not clearer at all. It is not stanard, it uses the assumption that a "p" suffix is a pointer and a "u" prefix is unsigned (although this seems to have caught on with C# people). What does it add over the standard legible method?
This is about as horrible as Microsofts LPSZ*.
Posted: Sat Mar 29, 2008 11:14 am
by nekros
I just did this because it was really ugly to have (unsigned char*) and crap everywhere it was more obvious, I was not trying to go according to any standard.
Also, I wanted to type less, I didn't want to have to do something like
Posted: Sat Mar 29, 2008 11:49 am
by einsteinjunior
Please,he said he was writting the macros for the C programming language.
static_cast<char>(x) is used in C++.
Hope thats clear.
Re: some macros I wrote
Posted: Sat Mar 29, 2008 11:57 am
by neon
Candy wrote:I think your style is not clearer at all. It is not stanard, it uses the assumption that a "p" suffix is a pointer and a "u" prefix is unsigned (although this seems to have caught on with C# people). What does it add over the standard legible method?
QFE.
Your macros adds nothing at all to what the language itself provides mechanisms for. It is simply unnecessary code that creates more problems then good. What significance does your code have over standard C methods?
Its redundant.
nekros wrote:I just did this because it was really ugly to have (unsigned char*) and crap everywhere it was more obvious, I was not trying to go according to any standard.
The best recommendation is to not use casts at all. If you really have casts everywhere, it should point out that the design is flawed. (This is not the case if it is in locations where it is required; not because the data types are different in size)
Posted: Sat Mar 29, 2008 12:01 pm
by Krox
yeah, (unsigned long*) is ugly, but my solution was a little different:
Code: Select all
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned long long int QWORD;
typedef unsigned char UINT8;
typedef unsigned short UINT16;
etc...
This way, the cast doesnt look like a function but is shorter anyway. Furthermore, the exact size got much clearer. Remember that for example the size of "long" depends on weather the compiler is set up for 32 or 64 bit mode.
For the aesthetical question: The capslock looks bad, I know. But not worse than the _t notation. But thats a matter of taste I think
Posted: Sat Mar 29, 2008 12:19 pm
by jzgriffin
I used #define for no apparent reason.
Code: Select all
#define uchar unsigned char
#define ushort unsigned short
#define uint unsigned int
#define ulong unsigned long
Posted: Sat Mar 29, 2008 1:15 pm
by nekros
The casts are for things like
Code: Select all
char *texmem = cast_charp(0xB8000);
instead of:
Posted: Sat Mar 29, 2008 1:18 pm
by jzgriffin
Code: Select all
ushort *vidmem = (ushort *)0x0B8000;
looks better than that, personally. But if function macros float your boat, ok. :-p
Posted: Sat Mar 29, 2008 1:31 pm
by nekros
I'm talking about when you have casts in parameters to functions all those () get really confusing
Posted: Sat Mar 29, 2008 2:37 pm
by neon
Jeremiah Griffin wrote:I used #define for no apparent reason.
Abstracting the data types for portability serves a purpose and is recommended. The OP's macros create an abstraction that serves no purpose.
In any case, data type abstraction has nothing to do with abstracting type casting.
(With regards to standard C++, #define is coinsidered bad programming practice hence should not be used.)
I'm talking about when you have casts in parameters to functions all those () get really confusing
...Using function macros still require using ()'s.
Posted: Mon Mar 31, 2008 12:59 pm
by jzgriffin
Here's my new ones, after a rewrite:
Code: Select all
typedef unsigned char byte; /* Byte */
typedef unsigned short word; /* Word */
typedef unsigned int dword; /* Double word */
typedef unsigned long long qword; /* Quad word */
And, since this is a
macro thread:
Code: Select all
#define PANIC(msg) panic(msg, __FILE__, __LINE__)
#define ASSERT(assert) if(!(assert)) panica(#assert, __FILE__, __LINE__);