some macros I wrote

Programming, for all ages and all languages.
Post Reply
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

some macros I wrote

Post 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))
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: some macros I wrote

Post 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*.
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Post 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

Code: Select all

static_cast<unsigned char>(x)
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
einsteinjunior
Member
Member
Posts: 90
Joined: Tue Sep 11, 2007 6:42 am

Post 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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: some macros I wrote

Post 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)
Last edited by neon on Sat Mar 29, 2008 12:01 pm, edited 1 time in total.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Krox
Posts: 23
Joined: Fri Mar 21, 2008 3:52 am
Location: Germany

Post 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 :D
21 is only half the truth...
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Post 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
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Post by nekros »

The casts are for things like

Code: Select all

char *texmem = cast_charp(0xB8000);
instead of:

Code: Select all

char *texmem = (char*) 0xB8000;
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Post by jzgriffin »

Code: Select all

ushort *vidmem = (ushort *)0x0B8000;
looks better than that, personally. But if function macros float your boat, ok. :-p
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Post by nekros »

I'm talking about when you have casts in parameters to functions all those () get really confusing
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Post 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__);
Post Reply