Jump to content
  • Advertisement
Sign in to follow this  
skwee

MAKE VERSION macro? How to do?

This topic is 4070 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello i want to do preprocessor MACRO like MAKEVERSION(a,b,c,d) and after i'll call it like this MAKEVERSION(0,2,3,56) i'll get a DWORD = 0.2.3.56 Is it possible? Thanks

Share this post


Link to post
Share on other sites
Advertisement

#define DW(x) ((unsigned long)(x))
#define MAKEVERSION(a,b,c,d) DW((DW(a) << 24) | (DW(b) << 16) | (DW(c) << 8) | (DW(d))
#undef DW

Share this post


Link to post
Share on other sites
Quote:
Original post by Minios
Hello i want to do preprocessor MACRO like MAKEVERSION(a,b,c,d)
and after i'll call it like this MAKEVERSION(0,2,3,56) i'll get a DWORD = 0.2.3.56
Is it possible?
Thanks
Not exactly, since a DWORD can only store one integer.
You can however, pack the values in so you'd get DWORD = 0x00020338, which represents what you want with the following:

#define MAKEVERSION(a, b, c, d) (DWORD)( (((a)&0xff)<<24) | (((a)&0xff)<<16) | (((a)&0xff)<<8) | ((a)&0xff))




EDIT: Screwed up the masks

Share this post


Link to post
Share on other sites
#define MAKEVERSION(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))

will give you

DWORD version = 0x0a0b0c0d

The only limit on this is that each number must be between 0 and 255.

To print out the version number as a string,

printf("Version: %d.%d.%d.%d", (version & 0xff000000) >> 24, (version & 0x00ff0000) >> 16, (version & 0x0000ff00) >> 8, version & 0x000000ff);

Share this post


Link to post
Share on other sites
Big Thanks!! :D
And how i can print it in this way: 0.2.3.56?
I mean when i;m doing printf i'm getting a long number like 106842335..
Thanks again.

Sorry didnt noticed Sly's comment
Thanks ppl :)

Share this post


Link to post
Share on other sites
typedef unsigned long dw;

dw v = MAKEVERSION(1,2,3,4);

dw a=(v >> 24) & 0xff;
dw b=(v >> 16) & 0xff;
dw c=(v >> 8) & 0xff;
dw d=v & 0xff;
printf("%u.%u.%u.%u.", a, b, c, d);

Share this post


Link to post
Share on other sites
Original post by Evil Steve

#define MAKEVERSION(a, b, c, d) (DWORD)( (((a)&0xff)<<24) | (((b)&0xff)<<16) | (((c)&0xff)<<8) | ((d)&0xff))


[/quote]
Fixed it for you.

Share this post


Link to post
Share on other sites
Since you mention DWORDS, it's likely you're talking about C++.


typedef unsigned char ver_t;

template < ver_t A, ver_t B, ver_t C, ver_t D >
struct version
{
DWORD operator()() const
{
return (A << 24) | (B<<16) | (C<<8) | (D);
}

ver_t Version() const { return A; }
ver_t Major() const { return B; }
ver_t Minor() const { return C; }
ver_t Build() const { return D; }

void print() const
{
std::cout << static_cast<int>(A) << '.'
<< static_cast<int>(B) << '.'
<< static_cast<int>(C) << '.'
<< static_cast<int>(D);
}
};

typedef version<0,2,3,56> BuildVersion;
typedef version<1,0,0,22> ProtocolVersion;

....



You can also change the types of individual A,B,C,D to be different from 8-bit int, perhaps 16-bit for D, to allow more builds.

But the best thing is, there is no overhead, there is no storage, and no #defines.

You can optionally make all methods static.

Excercises left for the reader:
- overload << operator
- overload comparison operators
etc...

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
Quote:
Original post by Evil Steve
*** Source Snippet Removed ***

Fixed it for you.

Whoops, damn copy & paste errors...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!